about summary refs log tree commit diff
path: root/home/profiles/base/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'home/profiles/base/scripts')
-rwxr-xr-xhome/profiles/base/scripts/airport.nix90
1 files changed, 90 insertions, 0 deletions
diff --git a/home/profiles/base/scripts/airport.nix b/home/profiles/base/scripts/airport.nix
new file mode 100755
index 0000000..f123557
--- /dev/null
+++ b/home/profiles/base/scripts/airport.nix
@@ -0,0 +1,90 @@
+''
+  # airport.sh - shared file store
+  #
+  # # Environment variables
+  # `AIRPORT_HOST` - host to rsync to
+  # `AIRPORT_USER` - username to connect with
+  # `AIRPORT_DIR`  - defaults to `~/airport`, directory to use
+
+  set -euo pipefail
+
+  die () {
+      echo >&2 "$@"
+      exit 1
+  }
+
+  confirm() {
+      read -r -p "''${1:-Proceed?} [y/N] " response
+      case "$response" in
+          [yY][eE][sS]|[yY])
+              true
+              ;;
+          *)
+              false
+              ;;
+      esac
+  }
+
+  delay() {
+      DELAY_SECONDS=''${2:-5}
+      echo "''${1:-This script will perform some potentionally destructive actions.}"
+      echo "Waiting $DELAY_SECONDS second(s) before proceeding..."
+      sleep $DELAY_SECONDS
+  }
+
+  bin_exists() {
+      [ -z $1 ] && die "FATAL: called bin_exists() without arguments (expected 1)"
+      if command -v $1 &> /dev/null
+      then
+          true
+      else
+          false
+      fi
+  }
+
+  [ -f $HOME/.airport-env ] && source $HOME/.airport-env
+
+  HOST=''${AIRPORT_HOST:?"AIRPORT_HOST needs to be set"}
+  USER=''${AIRPORT_USER:?"AIRPORT_USER needs to be set"}
+  DIR=''${AIRPORT_DIR:-"$HOME/airport"}
+
+  [ "$#" -eq 0 ] && die "FATAL: expected exactly 1 argument, $# provided"
+
+  if [ ! -d $DIR ]
+  then
+      confirm "$DIR doesn't exist, create and proceed?" || die "FATAL: $DIR doesn't exist"
+      mkdir -p $DIR
+  fi
+
+  bin_exists "rsync" || die "FATAL: couldn't find rsync"
+
+  case "$1" in
+      send)
+          delay "Airport will run 'rsync -au' to $DIR to SEND"
+          rsync -au --stats --info=progress2 --mkpath $DIR $USER@$HOST:airport
+      ;;
+
+      recv)
+          delay "Airport will run 'rsync -au' to $DIR to RECV"
+          rsync -au --stats --info=progress2 $USER@$HOST:airport $DIR
+      ;;
+
+      configure)
+          delay "Will write configuration to $HOME/.airport-env"
+          cat >$HOME/.airport-env <<EOF
+AIRPORT_USER=$USER
+AIRPORT_HOST=$HOST
+AIRPORT_DIR=$DIR
+EOF
+      ;;
+
+      *) printf "Unknown subcommand $1, supported subcommands:\n"
+          printf '\tsend\n'
+          printf '\trecv\n'
+          printf '\tconfigure'
+          die
+      ;;
+  esac
+
+  exit 0
+''