aboutsummaryrefslogtreecommitdiff
path: root/local/bin/clout
blob: 51fa3b716430fce62492cd7c9a4df92d159cecfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/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