#! /bin/bash -e prog=${0##*/} dev=${DVDRIP_DEVICE-/dev/dvd} tmp=${DVDRIP_TMPDIR-${HOME?}/tmp/dvdrip} archive=${DVDRIP_ARCHIVE-jem.distorted.org.uk:/mnt/dvd/archive} : ${DVD_SECTOR_COPY=dvd-sector-copy} : ${DVDRIP_UPLOAD=dvdrip-upload} backup=nil eject=nil force=nil verbose=nil bogus=nil; unset dir sub n label usage () { cat <&2; exit 2 ;; esac case $verbose in t) set -x ;; esac case $archive in *:*) archhost=${archive%%:*} archpath=${archive#*:} ;; *) unset archhost; archpath=$archive ;; esac notify () { colour=$1 message=$2 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" } fail () { notify 1 "!!! $*"; exit 2; } warn () { notify 5 "??? $*"; } info () { notify 6 "--- $*"; } run_setrc () { notify 2 "+++ $*"; set +e; nice "$@"; rc=$?; set -e } run () { run_setrc "$@"; case $rc in 0) ;; *) fail "$1: exit $rc" ;; esac; } archdo () { op=$1; shift case ${archhost+t} in t) qq= for a in "$@"; do qq="${qq:+$qq }'${a//\'/"'\\''"}'" #" # emacs is confused done "$op" ssh "$archhost" "$qq" ;; *) "$op" "$@" ;; esac } archrun () { archdo run "$@"; } case ${dir+t},${n+t} in t,t | ,) n=$(printf "%02d" "$n") ;; *) echo >&2 "$prog: must specify both directory and disc number, or neither" exit 2 ;; esac hack_label () { tr "[:lower:]" "[:upper:]" | tr -Cs "[:alnum:]_\n" "[-*]" | sed 's/^-//; s/-$//' } case $backup in t) case ${label+t},${dir+t} in t,*) ;; ,) label=$(printf "%s" "$title" | hack_label) ;; ,t) label=$(printf "%s_%s" "$dir" "$n" | hack_label) ;; esac len=$(printf "%s" "$label" | wc -c) if [ $len -gt 32 ]; then echo >&2 "$prog: label too long"; exit 2; fi ;; nil) case ${label+t} in t) echo >&2 "$prog: label only meaningful to \`dvdbackup'"; exit 2 ;; esac ;; esac case ${dir+t} in t) tag="${dir}_${n}_${title}" out="$dir/$n. $title" ;; *) tag=$title out=$title ;; esac tag=${tag//\//_} archdo run_setrc test -f "$archpath${sub+/$sub}/$out.iso" case $rc,$force in 0,nil) fail "output file already exists" ;; 0,t) warn "output file already exists; will overwrite" ;; esac accumulate_badblocks () { if [ -f "$tmp/$tag/badblocks.new" ]; then if [ ! -f "$tmp/$tag/badblocks" ]; then { echo "## bad-blocks region map"; echo; } >"$tmp/$tag/badblocks" fi sed -n "/^[^#]/p" "$tmp/$tag/badblocks.new" >>"$tmp/$tag/badblocks" rm "$tmp/$tag/badblocks.new" fi } mkdir -p "$tmp/$tag" case $backup in t) if [ ! -d "$tmp/$tag/rip" ]; then rm -rf "$tmp/$tag/rip.new" run dvdbackup -Mp -i"$dev" -o"$tmp/$tag" -n"rip.new" run mv "$tmp/$tag/rip.new" "$tmp/$tag/rip" fi if [ ! -f "$tmp/$tag/iso" ]; then run genisoimage -quiet -dvd-video -udf -V "$label" \ -o "$tmp/$tag/iso.new" "$tmp/$tag/rip" run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso" fi ;; nil) if [ ! -f "$tmp/$tag/iso" ]; then accumulate_badblocks run_setrc "$DVD_SECTOR_COPY" -c -b"$tmp/$tag/badblocks.new" \ "$dev" "$tmp/$tag/iso.new" accumulate_badblocks case $rc in 0) run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso" ;; 1) run mv "$tmp/$tag/iso.new" "$tmp/$tag/iso" fail "bad sectors found: check \`$tmp/$tag/iso', run again if ok" ;; *) fail "$DVD_SECTOR_COPY: exit $rc" ;; esac fi ;; esac printf "%s\n" "${sub+$sub/}$out.iso" >"$tmp/$tag/dest.new" mv "$tmp/$tag/dest.new" "$tmp/$tag/dest" run "$DVDRIP_UPLOAD" case $eject in t) run eject "$dev" ;; esac printf "\a"