'' # 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 # `AIRPORT_SAFEOP` - 'safe operations' mode, which doesn't delete files 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"} SAFEOP=''${AIRPORT_SAFEOP:-1} [ "$#" -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" opts=(-au --stats --info=progress2) [ $SAFEOP ] || opts+=(--delete-after) case "$1" in send) delay "Airport will run 'rsync -au' to $DIR to SEND" rsync ''${opts[@]} --mkpath $DIR/ $USER@$HOST:airport ;; recv) delay "Airport will run 'rsync -au' to $DIR to RECV" rsync ''${opts[@]} $USER@$HOST:airport/ $DIR ;; configure) delay "Will write configuration to $HOME/.airport-env" cat >$HOME/.airport-env <