about summary refs log tree commit diff
path: root/home/profiles/base
diff options
context:
space:
mode:
authorsefidel <contact@sefidel.net>2023-07-27 18:05:29 +0900
committersefidel <contact@sefidel.net>2023-07-27 18:05:51 +0900
commit905af590ac0ccce728d2fb127e25bbde4fb6cc02 (patch)
treeef4562672572dcff1fe2e7c8d6046141afc03bcc /home/profiles/base
parent7af21a9cdeed5aa0557989c18bce9359a0478d54 (diff)
downloadnixrc-905af590ac0ccce728d2fb127e25bbde4fb6cc02.tar.gz
nixrc-905af590ac0ccce728d2fb127e25bbde4fb6cc02.zip
feat(home/base): add airport script
Diffstat (limited to 'home/profiles/base')
-rw-r--r--home/profiles/base/default.nix1
-rwxr-xr-xhome/profiles/base/scripts/airport.nix90
2 files changed, 91 insertions, 0 deletions
diff --git a/home/profiles/base/default.nix b/home/profiles/base/default.nix
index df2e672..43b56dd 100644
--- a/home/profiles/base/default.nix
+++ b/home/profiles/base/default.nix
@@ -53,6 +53,7 @@ in
       pkgs.gcc
 
       (pkgs.writeShellScriptBin "0x0" (import ./scripts/0x0.nix))
+      (pkgs.writeShellScriptBin "airport" (import ./scripts/airport.nix))
     ];
 
 
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
+''