'' # 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 <