dvdrip-upload: Replace the signalling protocol with, err, signals.
[dvdrip] / dvdrip-upload
CommitLineData
7fbe0fb9
MW
1#! /bin/bash -e
2
3prog=${0##*/}
4tmp=${DVDRIP_TMPDIR-${HOME?}/tmp/dvdrip}
5archive=${DVDRIP_ARCHIVE-jem.distorted.org.uk:/mnt/dvd/archive}
6kill=nil listen=nil verbose=nil bogus=nil opts=; unset dir sub n label
7usage () {
8 cat <<EOF
9usage: $prog [-klv]
10EOF
11}
4d42a31f 12while getopts "ha:klv" opt; do
7fbe0fb9
MW
13 case $opt in
14 h) usage; exit 0 ;;
4d42a31f 15 a) archive=$OPTARG ;;
7fbe0fb9
MW
16 k) kill=t ;;
17 l) listen=t; opts=${opts}l ;;
18 v) verbose=t; opts=${opts}v ;;
19 *) bogus=t ;;
20 esac
21done
22shift $(( $OPTIND - 1 ))
23case $# in
24 0) ;;
25 *) bogus=t ;;
26esac
27case $bogus in t) usage >&2; exit 2 ;; esac
28case $verbose in t) set -x ;; esac
29
7fbe0fb9
MW
30notify () {
31 colour=$1 message=$2
32 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)"
33}
34fail () { notify 1 "!!! $*"; exit 2; }
35warn () { notify 5 "??? $*"; }
36info () { notify 6 "--- $*"; }
37run_setrc () {
38 notify 2 "+++ $*";
39 set +e; nice "$@"; rc=$?; set -e
40}
41run () { run_setrc "$@"; case $rc in 0) ;; *) fail "$1: exit $rc" ;; esac; }
42
43case $archive in
44 *:*) archhost=${archive%%:*} archpath=${archive#*:} ;;
45 *) unset archhost; archpath=$archive ;;
46esac
47
48archdo () {
49 op=$1; shift
50 case ${archhost+t} in
51 t)
52 qq=
53 for a in "$@"; do
54 qq="${qq:+$qq }'${a//\'/"'\\''"}'" #" # emacs is confused
55 done
56 "$op" ssh "$archhost" "$qq"
57 ;;
58 *)
59 "$op" "$@"
60 ;;
61 esac
62}
63archrun () { archdo run "$@"; }
64
65check () {
66 while :; do
67 any=nil
68 for i in "$tmp"/*; do
69 if [ -f "$i/dest" ]; then
70 read dest <"$i/dest"; any=t
71 mv "$i/dest" "$i/dest.seen"
72 (info "copy $i/iso -> $dest"
73 case $dest in
74 */*) dir=${dest%/*} ;;
75 *) dir= ;;
76 esac
77 archrun mkdir -p "$archpath${dir:+/$dir}"
3eab0d69 78 run rsync -svPSW "$i/iso" "$archive/$dest"
7fbe0fb9
MW
79 run rm -rf "$i") || :
80 fi
81 done
82 case $any in nil) break ;; esac
83 done
84}
85
d1d615a9
MW
86try_kick_daemon () {
87 sig=$1
88
89 if [ -f "$tmp/upload.pid" ]; then
90 daemon=$(cat "$tmp/upload.pid")
91 case $daemon in
92 "" | *[!0-9]*) ;;
93 *) if kill -$sig $daemon >/dev/null 2>&1; then return 0; fi ;;
94 esac
95 fi
96 return 1
97}
98
99try_kill () {
100 victim=$1
101
102 case $victim in
103 nil) ;;
104 *) kill $victim >/dev/null 2>&1 || : ;;
105 esac
106}
107
108case $kill,$listen in
109 t,t)
110 fail "inconsistent options \`-k' and \`-l'"
7fbe0fb9 111 ;;
d1d615a9
MW
112 t,nil)
113 if ! try_kick_daemon TERM; then fail "failed to kill listener"; fi
114 ;;
115 nil,t)
116 if try_kick_daemon 0; then fail "daemon already running (pid $daemon)"; fi
117 trap 'try_kill $sleepy; rm -f "$tmp/upload.pid"' EXIT
118 trap 'info "quitting on user request"; exit 0' INT TERM
119 trap 'try_kill $sleepy' HUP
120 echo $$ >"$tmp/upload.pid.new"
121 mv "$tmp/upload.pid.new" "$tmp/upload.pid"
122 while :; do
123 sleep 3600& sleepy=$!
124 check
125 wait $sleepy || :; sleepy=nil
126 done
127 ;;
128 nil,nil)
129 if ! try_kick_daemon HUP; then check; fi
7fbe0fb9
MW
130 ;;
131esac