chiark / gitweb /
dvd-cache-keys.c: Move the existence check into `kick_vob'.
[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 accumulate_badblocks () {
99   if [ -f "$tmp/$tag/badblocks.new" ]; then
100     if [ ! -f "$tmp/$tag/badblocks" ]; then
101       { echo "## bad-blocks region map"; echo; } >"$tmp/$tag/badblocks"
102     fi
103     sed -n "/^[^#]/p" "$tmp/$tag/badblocks.new" >>"$tmp/$tag/badblocks"
104     rm "$tmp/$tag/badblocks.new"
105   fi
106 }
107
108 set --
109 any=nil
110 for i in "$tmp/$tag/dest.new" "$tmp/$tag/dest" "$tmp/$tag/dest.seen"; do
111   if [ -f "$tmp/$tag/dest.new" ]; then any=t; fi
112 done
113 case $any in
114   nil) printf "%s\n" "$title.iso" >"$tmp/$tag/dest.new" ;;
115 esac
116 case $eject in
117   t) touch "$tmp/$tag/eject" ;;
118   nil) rm -f "$tmp/$tag/eject" ;;
119 esac
120 case $ding in
121   t) touch "$tmp/$tag/ding" ;;
122   nil) rm -f "$tmp/$tag/ding" ;;
123 esac
124
125 accumulate_badblocks
126 case $retry in
127   t)
128     if [ -f "$tmp/$tag/badblocks.retry" ]; then
129       :
130     elif [ -f "$tmp/$tag/badblocks" ]; then
131       run mv "$tmp/$tag/badblocks" "$tmp/$tag/badblocks.retry"
132     else
133       fail "no blocks to retry"
134     fi
135     set -- "$@" -R"$tmp/$tag/badblocks.retry"
136     if [ -f "$tmp/$tag/iso" ]; then
137       mv "$tmp/$tag/iso" "$tmp/$tag/iso.new"
138     fi
139     ;;
140 esac
141 if [ ! -f "$tmp/$tag/iso" ]; then
142   run "$DVD_SECTOR_COPY" -cs ${params+"-B$params"} \
143       -b"$tmp/$tag/badblocks.new" "$@" "$dev" "$tmp/$tag/iso.new"
144   run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
145   accumulate_badblocks
146   case $retry in t) rm -f "$tmp/$tag/badblocks.retry" ;; esac
147   if [ -f "$tmp/$tag/badblocks" ]; then
148     fail "bad sectors found: check \`$tmp/$tag/iso', run again if ok"
149   fi
150 fi
151
152 run mv "$tmp/$tag/dest.new" "$tmp/$tag/dest"
153 if [ -f "$tmp/$tag/eject" ]; then eject=t; else eject=nil; fi
154 if [ -f "$tmp/$tag/ding" ]; then ding=t; else ding=nil; fi
155 run "$DVDRIP_UPLOAD"
156 case $eject in t) run eject "$dev" ;; esac
157 case $ding in
158   t)
159     if [ -t 1 ]; then exec 3>&1
160     elif [ -t 2 ]; then exec 3>&2
161     else exec 3>/dev/tty
162     fi
163     printf "\a" >&3
164     exec 3>&-
165     ;;
166 esac