#!/bin/sh
# $Id: dtsgrab,v 1.13 2003/04/15 23:34:37 jhawk Exp $
#
# Tool to grab dts trailers off of a cdrom.
#
AWK=gawk
DEFAULTVISUAL=vi
# Ugh, dd incompatibilities. Under SunOS, iseek is supported. If skip
# is used with a blocksize of 1, multiple single byte reads are done and
# it takes *forever*.
# Under NetBSD, iseek is not supported, but skip works. It seems to be
# efficient and behaves like iseek does.
case `uname` in
  SunOS) DDSEEK=iseek ECHO=echo ECHOZ="\\c"
	srcdir=/cdrom/cdrom0 ;;
  NetBSD) DDSEEK=skip ECHO="echo -n" ECHOZ=""
	srcdir=/var/tmp/j;;
  IRIX) DDSEEK=iseek ECHO="echo -n" ECHOZ=""
	srcdir=/CDROM;;
  Linux) DDSEEK=skip ECHO="echo -n" ECHOZ=""
	srcdir=/mnt/cdrom;;
  Darwin) DDSEEK=skip AWK=awk ECHO="echo -n" ECHOZ=""
	  DEFAULTVISUAL=/Applications/TextEdit.app/Contents/MacOS/TextEdit
	srcdir=/Volumes/cd9660 ;;
  *) DDSEEK=skip ECHO=echo ECHOZ="" srcdir=/var/tmp/dstsrc ;;
esac

dstdir=/var/tmp/dts
# dstdir=/afs/sipb/user/jhawk/dtswork${1}

tmptoc=/tmp/dtstoc${$}
dtsblock=3675		# 3675 bytes / sequence

echo Assuming values:
echo   cdrom mounted on: ${srcdir}
echo   dump trailers to: ${dstdir}
echo Temporary files are: ${tmptoc}.1 ${tmptoc}.2
$ECHO Hit newline to continue...$ECHOZ
read pause

mkdir -p ${dstdir}
cd ${dstdir}
echo Copying the table of contents
cat < ${srcdir}/dts/r14trlr.txt > ${tmptoc}.1

# Here's a list of trailers to exclude from the edit list. We do it
# this way to make it easy to patch, etc.
regexp=`cat << EOF |
;NAME
;----
thx
simpsons
tex2
cinemtrl
cineimax
timesliv
jbllogo
pepsiacd
pepsihot
pepafi.0
pepaf.0r
startrlb
sil2sec
wilrog99
EOF
${AWK} '{printf "%s^%s", t?"|":"", $1; t=1}'`
# echo regexp: $regexp

# remove ^Ms (end of line) and ^Zs (end of file)
< ${tmptoc}.1 tr -d \\015\\032 > ${tmptoc}.2
# remove annoying crud we care not about.
< ${tmptoc}.2 egrep -v "${regexp}" | sort > ${tmptoc}.3

# We also want to mark any trailers, by serial number, that we
# have already gotten in this batch. This makes avoiding duplicates much
# less confusing. We tag them with "DUP" in the
# description field, in case we want to override.
{
  files=`echo *.t`
  case $files in
    '*.t')
     ;;
     *)
      cat *.t | ${AWK} '{printf "%s$2==%s", t?"||":"", $2; t=1}'
      echo '{print $0, "DUP"; next}'
     ;;
  esac
  echo "1"
} > ${tmptoc}.awk
    
< ${tmptoc}.3 ${AWK} -f ${tmptoc}.awk > ${tmptoc}.4

echo Please edit the following list of trailers to include
echo the ones you wish to grab. Please do not change the offsets
echo or serial numbers unless you know what you are doing.
echo ""
echo "Feel free to add a more descriptive trailer name after the 5th field"
echo "to assist in decyphering the produced disc."
echo ""
$ECHO Hit newline to continue...$ECHOZ
read pause

# For whatever reason, if you hit ^C in vi, the shell gets SIGCHLD and SIGINT.
# Trap them temporarily.
trap "" 2 18
${VISUAL:-$DEFAULTVISUAL} ${tmptoc}.4
trap 2 18

for i in startrlb sil2sec; do
  if [ ! -f $i ] ; then
    grep "^${i}" ${tmptoc}.2 >> ${tmptoc}.4
    echo 'Adding in "'$i'" trailer.'
  fi
done

echo "Executing the copy..."
< ${tmptoc}.4 ${AWK} '
  BEGIN{dtsblock="'"${dtsblock}"'"; srcdir="'"${srcdir}"'" }
  {printf "echo %s\t\t%s\t%s\t%s\t%s\t%s > %s\n" \
  "(dd of=/dev/null '"${DDSEEK}"'=%d bs=1 count=1; " \
  " dd of=%s bs=%d count=%d ) < %s/dts/r14t5.aud \n", \
  $1, $2, $3, $4, 0, $6,   $1 ".t",
  $5-1,
  $1, dtsblock, ($4-$3), srcdir;
  }' > ${tmptoc}.5
sh -x ${tmptoc}.5
echo "Copy done."

if [ ! -f dts.exe ] ; then
  echo "All discs should have DTS.exe, copying that too."
  cp ${srcdir}/dts.exe dts.exe
fi
