dvdrip: Fail if we can't read the `.IFO' hash for the disc id.
[dvdrip] / dvdrip
CommitLineData
7fbe0fb9
MW
1#! /bin/bash -e
2
3prog=${0##*/}
4dev=${DVDRIP_DEVICE-/dev/dvd}
5tmp=${DVDRIP_TMPDIR-${HOME?}/tmp/dvdrip}
6archive=${DVDRIP_ARCHIVE-jem.distorted.org.uk:/mnt/dvd/archive}
f49b5c86 7: ${DVD_SECTOR_COPY=dvd-sector-copy}
32c8d775 8: ${DVD_ID=dvd-id}
f49b5c86 9: ${DVDRIP_UPLOAD=dvdrip-upload}
dc53ebfa
MW
10backup=nil ding=nil eject=nil force=nil retry=nil verbose=nil bogus=nil
11unset params
7fbe0fb9
MW
12usage () {
13 cat <<EOF
dc53ebfa 14usage: $prog [-defrv] [-D DEV] [-a ARCH] [-t TMP] TITLE
7fbe0fb9
MW
15EOF
16}
dc53ebfa 17while getopts "hB:D:a:defrt:v" opt; do
7fbe0fb9
MW
18 case $opt in
19 h) usage; exit 0 ;;
dc53ebfa 20 B) params=${params+$params,}$OPTARG ;;
7fbe0fb9
MW
21 D) dev=$OPTARG ;;
22 a) archive=$OPTARG ;;
dc53ebfa 23 d) ding=t ;;
7fbe0fb9
MW
24 e) eject=t ;;
25 f) force=t ;;
192df729 26 r) retry=t ;;
7fbe0fb9
MW
27 t) tmp=$OPTARG ;;
28 v) verbose=t ;;
29 *) bogus=t ;;
30 esac
31done
32shift $(( $OPTIND - 1 ))
822ac8a8 33case $# in 1) title=$1 ;; *) bogus=t ;; esac
7fbe0fb9
MW
34case $bogus in t) usage >&2; exit 2 ;; esac
35case $verbose in t) set -x ;; esac
36case $archive in
37 *:*) archhost=${archive%%:*} archpath=${archive#*:} ;;
38 *) unset archhost; archpath=$archive ;;
39esac
40
41notify () {
42 colour=$1 message=$2
43 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)"
44}
45fail () { notify 1 "!!! $*"; exit 2; }
46warn () { notify 5 "??? $*"; }
47info () { notify 6 "--- $*"; }
48run_setrc () {
49 notify 2 "+++ $*";
50 set +e; nice "$@"; rc=$?; set -e
51}
52run () { run_setrc "$@"; case $rc in 0) ;; *) fail "$1: exit $rc" ;; esac; }
53
54archdo () {
55 op=$1; shift
56 case ${archhost+t} in
57 t)
58 qq=
59 for a in "$@"; do
60 qq="${qq:+$qq }'${a//\'/"'\\''"}'" #" # emacs is confused
61 done
62 "$op" ssh "$archhost" "$qq"
63 ;;
64 *)
65 "$op" "$@"
66 ;;
67 esac
68}
69archrun () { archdo run "$@"; }
70
59f1250f 71tag=${title//\//_}
7fbe0fb9 72
59f1250f 73archdo run_setrc test -f "$archpath/$title.iso"
7fbe0fb9
MW
74case $rc,$force in
75 0,nil) fail "output file already exists" ;;
76 0,t) warn "output file already exists; will overwrite" ;;
77esac
78
dc53ebfa
MW
79mkdir -p "$tmp/$tag"
80
e14e3621 81discid=$("$DVD_ID" -I "$dev")
dc53ebfa
MW
82if [ -f "$tmp/$tag/discid" ]; then
83 read oldid <"$tmp/$tag/discid"
84 case $force,$oldid in
85 t,"$discid" | nil,"$discid")
86 ;;
87 nil,*)
88 fail "discid mismatch: expected \`$oldid' but drive has \`$discid'"
89 ;;
90 t,*)
91 warn "discid mismatch: expected \`$oldid' but drive has \`$discid'; continuing anway"
92 ;;
93 esac
94fi
95info "copying \`$discid'"
96echo "$discid" >"$tmp/$tag/discid.new"
97mv "$tmp/$tag/discid.new" "$tmp/$tag/discid"
98
1ef7b954
MW
99echo "$dev" >"$tmp/$tag/device.new"
100mv "$tmp/$tag/device.new" "$tmp/$tag/device"
101
b1a4618a
MW
102accumulate_badblocks () {
103 if [ -f "$tmp/$tag/badblocks.new" ]; then
104 if [ ! -f "$tmp/$tag/badblocks" ]; then
105 { echo "## bad-blocks region map"; echo; } >"$tmp/$tag/badblocks"
106 fi
107 sed -n "/^[^#]/p" "$tmp/$tag/badblocks.new" >>"$tmp/$tag/badblocks"
108 rm "$tmp/$tag/badblocks.new"
109 fi
110}
111
192df729 112set --
861c3824
MW
113any=nil
114for i in "$tmp/$tag/dest.new" "$tmp/$tag/dest" "$tmp/$tag/dest.seen"; do
115 if [ -f "$tmp/$tag/dest.new" ]; then any=t; fi
116done
117case $any in
59f1250f 118 nil) printf "%s\n" "$title.iso" >"$tmp/$tag/dest.new" ;;
861c3824 119esac
dc53ebfa
MW
120case $eject in
121 t) touch "$tmp/$tag/eject" ;;
122 nil) rm -f "$tmp/$tag/eject" ;;
123esac
124case $ding in
125 t) touch "$tmp/$tag/ding" ;;
126 nil) rm -f "$tmp/$tag/ding" ;;
127esac
59f1250f
MW
128
129accumulate_badblocks
130case $retry in
7fbe0fb9 131 t)
59f1250f
MW
132 if [ -f "$tmp/$tag/badblocks.retry" ]; then
133 :
134 elif [ -f "$tmp/$tag/badblocks" ]; then
135 run mv "$tmp/$tag/badblocks" "$tmp/$tag/badblocks.retry"
136 else
137 fail "no blocks to retry"
7fbe0fb9 138 fi
59f1250f
MW
139 set -- "$@" -R"$tmp/$tag/badblocks.retry"
140 if [ -f "$tmp/$tag/iso" ]; then
141 mv "$tmp/$tag/iso" "$tmp/$tag/iso.new"
7fbe0fb9
MW
142 fi
143 ;;
144esac
59f1250f 145if [ ! -f "$tmp/$tag/iso" ]; then
dc53ebfa
MW
146 run "$DVD_SECTOR_COPY" -cs ${params+"-B$params"} \
147 -b"$tmp/$tag/badblocks.new" "$@" "$dev" "$tmp/$tag/iso.new"
59f1250f
MW
148 run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
149 accumulate_badblocks
1ef7b954 150 rm -f "$tmp/$tag/device"
59f1250f
MW
151 case $retry in t) rm -f "$tmp/$tag/badblocks.retry" ;; esac
152 if [ -f "$tmp/$tag/badblocks" ]; then
153 fail "bad sectors found: check \`$tmp/$tag/iso', run again if ok"
154 fi
155fi
156
1ef7b954 157rm -f "$tmp/$tag/device"
192df729 158run mv "$tmp/$tag/dest.new" "$tmp/$tag/dest"
dc53ebfa
MW
159if [ -f "$tmp/$tag/eject" ]; then eject=t; else eject=nil; fi
160if [ -f "$tmp/$tag/ding" ]; then ding=t; else ding=nil; fi
7fbe0fb9
MW
161run "$DVDRIP_UPLOAD"
162case $eject in t) run eject "$dev" ;; esac
dc53ebfa
MW
163case $ding in
164 t)
165 if [ -t 1 ]; then exec 3>&1
166 elif [ -t 2 ]; then exec 3>&2
167 else exec 3>/dev/tty
168 fi
169 printf "\a" >&3
170 exec 3>&-
171 ;;
172esac