#!/bin/sh assert_command_exists() { command -v "$1" > /dev/null && return echo "Error: command not found: '$1'" [ -n "$2" ] && echo "$2" exit 1 } assert_command_exists rsync usage_exit() { echo "Usage: $(basename "$0") push/pull/push-watch/pull-watch [rsync args...]" exit 2 } [ -z "$1" ] && usage_exit # Parameter expansion is undifined with $* and $@ rsync_args=$([ -z "$2" ] || echo "$*") rsync_cmd="rsync -avh --progress --compress ${rsync_args#* }" remote_user=charles remote_host=cacharle.xyz remote="$remote_user@$remote_host" # '/' suffix is significant for rsync to know if it needs to copy in the directory or the directory itself sync_dir=${CLOUT_SYNC_DIR:-'clout-sync/'} sync_path="${CLOUT_SYNC_PATH:-"${XDG_DATA_HOME:-"$HOME/.sync"}/$sync_dir"}" create_sync_dirs() { # --mkpath only available in latest version of rsync (not in debian repositories) [ ! -d "$sync_path" ] && mkdir -p "$sync_path" # shellcheck disable=SC2029 ssh "$remote" "[ ! -d \"\$HOME/$sync_dir\" ] && mkdir -pv \"\$HOME/$sync_dir\"" } case "$1" in push) create_sync_dirs $rsync_cmd "$sync_path" "$remote:$sync_dir" ;; pull) create_sync_dirs $rsync_cmd "$remote:$sync_dir" "$sync_path" ;; push-watch) assert_command_exists inotifywait "Install inotify-tools package on ArchLinux" while true do echo 'Waiting for syncable content' inotifywait -q -r -e modify -e create "$sync_path" clout push done ;; dump-config) echo "rsync command: $rsync_cmd" echo "remote: $remote" echo "sync path: $sync_path" ;; *) usage_exit ;; esac