27e96e4c383b41a2e84ff3a22aa9831453315c66
[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 here=$(realpath "$0"); here=${here%/*}
8 : ${DVD_SECTOR_COPY=$here/dvd-sector-copy.$(uname -m)}
9 : ${DVDRIP_UPLOAD=$here/dvdrip-upload}
10 backup=nil eject=nil force=nil verbose=nil bogus=nil; unset dir sub n label
11 usage () {
12 cat <<EOF
13 usage: $prog [-befv] [-D DEV] [-a ARCH] [-d DIR]
14 [-l LABEL] [-n N] [-s SUB] [-t TMP] TITLE
15 EOF
16 }
17 while 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
33 done
34 shift $(( $OPTIND - 1 ))
35 case $# in
36 1) title=$1 ;;
37 *) bogus=t ;;
38 esac
39 case $bogus in t) usage >&2; exit 2 ;; esac
40 case $verbose in t) set -x ;; esac
41 case $archive in
42 *:*) archhost=${archive%%:*} archpath=${archive#*:} ;;
43 *) unset archhost; archpath=$archive ;;
44 esac
45
46 notify () {
47 colour=$1 message=$2
48 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)"
49 }
50 fail () { notify 1 "!!! $*"; exit 2; }
51 warn () { notify 5 "??? $*"; }
52 info () { notify 6 "--- $*"; }
53 run_setrc () {
54 notify 2 "+++ $*";
55 set +e; nice "$@"; rc=$?; set -e
56 }
57 run () { run_setrc "$@"; case $rc in 0) ;; *) fail "$1: exit $rc" ;; esac; }
58
59 archdo () {
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 }
74 archrun () { archdo run "$@"; }
75
76 case ${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 ;;
84 esac
85
86 hack_label () {
87 tr "[:lower:]" "[:upper:]" |
88 tr -Cs "[:alnum:]_\n" "[-*]" |
89 sed 's/^-//; s/-$//'
90 }
91
92 case $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 ;;
107 esac
108
109 case ${dir+t} in
110 t) tag="${dir}_${n}_${title}" out="$dir/$n. $title" ;;
111 *) tag=$title out=$title ;;
112 esac
113
114 archdo run_setrc test -f "$archpath${sub+/$sub}/$out.iso"
115 case $rc,$force in
116 0,nil) fail "output file already exists" ;;
117 0,t) warn "output file already exists; will overwrite" ;;
118 esac
119
120 mkdir -p "$tmp/$tag"
121 case $backup in
122 t)
123 if [ ! -d "$tmp/$tag/rip" ]; then
124 rm -rf "$tmp/$tag/rip.new"
125 run dvdbackup -Mp -i"$dev" -o"$tmp/$tag" -n"rip.new"
126 run mv "$tmp/$tag/rip.new" "$tmp/$tag/rip"
127 fi
128 if [ ! -f "$tmp/$tag/iso" ]; then
129 run genisoimage -quiet -dvd-video -udf -V "$label" \
130 -o "$tmp/$tag/iso.new" "$tmp/$tag/rip"
131 run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
132 fi
133 ;;
134 nil)
135 if [ ! -f "$tmp/$tag/iso" ]; then
136 run_setrc "$DVD_SECTOR_COPY" -D"$dev" -c -b"$tmp/$tag/badblocks" -o"$tmp/$tag/iso.new"
137 case $rc in
138 0)
139 run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
140 ;;
141 1)
142 run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso"
143 fail "bad sectors found: check \`$tmp/$tag/iso', run again if ok"
144 ;;
145 *)
146 fail "$DVD_SECTOR_COPY: exit $rc"
147 ;;
148 esac
149 fi
150 ;;
151 esac
152 printf "%s\n" "${sub+$sub/}$out.iso" >"$tmp/$tag/dest.new"
153 mv "$tmp/$tag/dest.new" "$tmp/$tag/dest"
154 run "$DVDRIP_UPLOAD"
155 case $eject in t) run eject "$dev" ;; esac