summaryrefslogtreecommitdiff
path: root/bin/common/sync-install.sh
blob: e26c74d7b585f412c274d6e353327e8bd0ed3298 (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
#!/bin/sh

die ()
{
	echo "$@" >&2
}

read_char ()
{
	old_stty_cfg=$(stty -g)
	stty raw -echo 
	dd ibs=1 count=1 2> /dev/null
	stty $old_stty_cfg
}

confirm ()
{
	printf "$1 "
	read_char | grep "[yY]"
}

usage()
{
	>&2 printf 'Usage: %s <remote> <destination>\n' "${0##*/}"
}

[ $# -lt 2 ] && usage && exit 1
REMOTE="$1"
DEST="$2"
SCRIPT="${3:-sync.sh}"

if ! ssh $REMOTE test -w $DEST 2> /dev/null
then
	die "Not a valid remote or destination."
	exit 1
fi

die "─────────────────────────────────────────────────────────────"
cat <<EOF | tee "$SCRIPT" >&2
#!/bin/sh

THISDIR="\$(dirname "\$(readlink -f "\$0")")"
inotifywait -m -e create,modify,delete --format "%f" "\$THISDIR" | 
while read FILE
do
	rsync -aP "\$THISDIR/" "$REMOTE:$DEST"
	sleep 1m
done
EOF
die "─────────────────────────────────────────────────────────────"
die "located at $(readlink -f "$SCRIPT")"

if confirm "good?"
then
	chmod +x "$SCRIPT"
else
	rm -f "$SCRIPT"
fi