working version
[distro-setup] / myunison
1 #!/bin/bash -l
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6 gen_args=()
7 do_snapshot=true
8 batch=false
9 while [[ $1 ]]; do
10 case $1 in
11 -ob) gen_args+=(--ours -b); batch=true; shift ;;
12 -b) gen_args+=(-b); batch=true; shift ;;
13 -n) do_snapshot=false; shift ;;
14 -h|--help)
15 echo "$0: help is head of $BASH_SOURCE:"
16 head -n 30 "$BASH_SOURCE"
17 exit 0
18 ;;
19 --) shift; break ;;
20 -*) gen_args+=($1) ; shift ;;
21 *) host=$1; shift ;;
22 esac
23 done
24 if [[ ! $host ]]; then
25 host=$1
26 fi
27
28 if [[ $host == demohost ]]; then
29 do_snapshot=false
30 fi
31
32 # arch and debian don't play nice with unison.
33 # "connection lost" on unison-gtk, with bignum errs in term,
34 # and simply freezing on cli, all means compiled under
35 # different ocaml versions.
36 # I had the great realization that unison is a self contained
37 # binary with no special runtime dependencies.
38 # However, I got a gtk error running the ui version.
39 # todo: I should test pushing my unison version instead
40 # of pulling it.
41 echo "$0: running: gen ${gen_args[@]}"
42 /p/c/subdir_files/.unison/gen ${gen_args[@]}
43 if [[ ! $host ]]; then
44 echo "$0: error: expected argument for hostname"
45 exit 1
46 fi
47
48
49 # todo: this doesn't deal with if the host is running arch too
50
51 # debian uses this path, and has gtk version linked as /usr/bin/unison,
52 # but I'd rather not push the gtk version
53 push_path=/usr/bin/unison-latest-stable
54 if [[ ! -e $push_path ]]; then
55 push_path=/usr/bin/unison
56 fi
57 # note, i'd prefer to use /usr/local/bin, but it's not in arch's default
58 # path for root.
59 scp /usr/bin/unison-latest-stable root@$host:/usr/bin/unison
60
61 if $do_snapshot; then
62 unison-snapshot
63 if ! ssh root@$host "! type unison-snapshot || unison-snapshot"; then
64 if [[ ! ${gen_args[@]} ]]; then
65 # gen_args are all about syncing with new systems where
66 # we could expect the remote snapshot to not be available yet.
67 echo "$0: error: failed remote unison-snapshot"
68 exit 1
69 fi
70 fi
71 fi
72
73 if $batch; then
74 # todo, figure out what the cli version is called in non-debian distros
75 f=unison-latest-stable
76 else
77 f=unison-gtk
78 type -t $f &>/dev/null || f=unison-gtk2 # for arch
79 fi
80
81 if s $f "$host"; then
82 if [[ $host == frodo ]]; then
83 # only send data to the machine i do full backups to
84 p=/q/p/c/machine_specific/$HOSTNAME/.unison/
85 s rsync -rlptgohi $p root@$host:$p
86 fi
87 p=/q/p/c/machine_specific/$host/.unison/
88 s rsync -rlptgohvi root@$host:$p $p
89 fi