dvd-sector-copy.c: Show overall progress if we're simply continuing.
[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}
7here=$(realpath "$0"); here=${here%/*}
8: ${DVD_SECTOR_COPY=$here/dvd-sector-copy.$(uname -m)}
9: ${DVDRIP_UPLOAD=$here/dvdrip-upload}
10backup=nil eject=nil force=nil verbose=nil bogus=nil; unset dir sub n label
11usage () {
12 cat <<EOF
13usage: $prog [-befv] [-D DEV] [-a ARCH] [-d DIR]
14 [-l LABEL] [-n N] [-s SUB] [-t TMP] TITLE
15EOF
16}
17while getopts "hD:a:bd:efl:n:s:t:v" opt; do
18 case $opt in
19 h) usage; exit 0 ;;
20 D) dev=$OPTARG ;;
21 a) archive=$OPTARG ;;
22 b) backup=t ;;
23 d) dir=$OPTARG ;;
24 e) eject=t ;;
25 f) force=t ;;
26 l) label=$OPTARG ;;
27 n) n=$OPTARG ;;
28 s) sub=$OPTARG ;;
29 t) tmp=$OPTARG ;;
30 v) verbose=t ;;
31 *) bogus=t ;;
32 esac
33done
34shift $(( $OPTIND - 1 ))
35case $# in
36 1) title=$1 ;;
37 *) bogus=t ;;
38esac
39case $bogus in t) usage >&2; exit 2 ;; esac
40case $verbose in t) set -x ;; esac
41case $archive in
42 *:*) archhost=${archive%%:*} archpath=${archive#*:} ;;
43 *) unset archhost; archpath=$archive ;;
44esac
45
46notify () {
47 colour=$1 message=$2
48 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)"
49}
50fail () { notify 1 "!!! $*"; exit 2; }
51warn () { notify 5 "??? $*"; }
52info () { notify 6 "--- $*"; }
53run_setrc () {
54 notify 2 "+++ $*";
55 set +e; nice "$@"; rc=$?; set -e
56}
57run () { run_setrc "$@"; case $rc in 0) ;; *) fail "$1: exit $rc" ;; esac; }
58
59archdo () {
60 op=$1; shift
61 case ${archhost+t} in
62 t)
63 qq=
64 for a in "$@"; do
65 qq="${qq:+$qq }'${a//\'/"'\\''"}'" #" # emacs is confused
66 done
67 "$op" ssh "$archhost" "$qq"
68 ;;
69 *)
70 "$op" "$@"
71 ;;
72 esac
73}
74archrun () { archdo run "$@"; }
75
76case ${dir+t},${n+t} in
77 t,t | ,)
78 n=$(printf "%02d" "$n")
79 ;;
80 *)
81 echo >&2 "$prog: must specify both directory and disc number, or neither"
82 exit 2
83 ;;
84esac
85
86hack_label () {
87 tr "[:lower:]" "[:upper:]" |
88 tr -Cs "[:alnum:]_\n" "[-*]" |
89 sed 's/^-//; s/-$//'
90}
91
92case $backup in
93 t)
94 case ${label+t},${dir+t} in
95 t,*) ;;
96 ,) label=$(printf "%s" "$title" | hack_label) ;;
97 ,t) label=$(printf "%s_%s" "$dir" "$n" | hack_label) ;;
98 esac
99 len=$(printf "%s" "$label" | wc -c)
100 if [ $len -gt 32 ]; then echo >&2 "$prog: label too long"; exit 2; fi
101 ;;
102 nil)
103 case ${label+t} in
104 t) echo >&2 "$prog: label only meaningful to \`dvdbackup'"; exit 2 ;;
105 esac
106 ;;
107esac
108
109case ${dir+t} in
110 t) tag="${dir}_${n}_${title}" out="$dir/$n. $title" ;;
111 *) tag=$title out=$title ;;
112esac
287d8dd8 113tag=${tag//\//_}
7fbe0fb9
MW
114
115archdo run_setrc test -f "$archpath${sub+/$sub}/$out.iso"
116case $rc,$force in
117 0,nil) fail "output file already exists" ;;
118 0,t) warn "output file already exists; will overwrite" ;;
119esac
120
121mkdir -p "$tmp/$tag"
122case $backup in
123 t)
124 if [ ! -d "$tmp/$tag/rip" ]; then
125 rm -rf "$tmp/$tag/rip.new"
126 run dvdbackup -Mp -i"$dev" -o"$tmp/$tag" -n"rip.new"
127 run mv "$tmp/$tag/rip.new" "$tmp/$tag/rip"
128 fi
129 if [ ! -f "$tmp/$tag/iso" ]; then
130 run genisoimage -quiet -dvd-video -udf -V "$label" \
131 -o "$tmp/$tag/iso.new" "$tmp/$tag/rip"
132 run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
133 fi
134 ;;
135 nil)
136 if [ ! -f "$tmp/$tag/iso" ]; then
137 run_setrc "$DVD_SECTOR_COPY" -D"$dev" -c -b"$tmp/$tag/badblocks" -o"$tmp/$tag/iso.new"
138 case $rc in
139 0)
140 run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
141 ;;
142 1)
143 run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
144 fail "bad sectors found: check \`$tmp/$tag/iso', run again if ok"
145 ;;
146 *)
147 fail "$DVD_SECTOR_COPY: exit $rc"
148 ;;
149 esac
150 fi
151 ;;
152esac
153printf "%s\n" "${sub+$sub/}$out.iso" >"$tmp/$tag/dest.new"
154mv "$tmp/$tag/dest.new" "$tmp/$tag/dest"
155run "$DVDRIP_UPLOAD"
156case $eject in t) run eject "$dev" ;; esac