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