#!/bin/sh
# $Id: dtsprep,v 1.5 2002/03/30 22:20:24 jhawk Exp jhawk $
#
# Tool to prepare a filesystem tree to be mkisofs'd and cdwritten
# from "dtsgrab" output.
#
AWK=gawk
case `uname` in
  Darwin) AWK=awk ;;
esac

srcdir=/var/tmp/dts
dstdir=/var/tmp/dtscd
dstht=/var/tmp/dtscd.txt

tmptoc=/tmp/dtstoc${$}
tmphead=/tmp/dtsheader$$
dtsblock=3675

# not portable, so use uuencoded mess.
# header="Trailers Reel 14=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00\
# =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00\
# =00=00english=00none=00=00=00=01=00=00=0E=00=15=04=05=00=00=06=00=00=08=06\
# w=00="
headerlen=92

echo Assuming values:
echo "  Source trailers (and .t files), DTS.EXE, etc.: ${srcdir}"
echo "  Write the output to                          : ${dstdir}"
echo "  Human-readable table-of-contents             : ${dstht}"
echo ""
echo Hit newline to continue...\\c
read pause

cd ${srcdir}
mkdir -p ${dstdir}/dts

(
  echo begin 600 ${tmphead}
  cat <<'EOF'
M5')A:6QE<G,@4F5E;"`Q-```````````````````````````````````````
M````````````````````96YG;&ES:`!N;VYE`````0``#@`5!`4```8```@&
"=P``
`
end
EOF
) | uudecode

(
  cat *.t | egrep -v '^sil2sec';
  cat sil2sec.t
) > ${tmptoc}.1

< ${tmptoc}.1 $AWK '
  BEGIN{ dtsblock='"${dtsblock}"'; dstdir="'"${dstdir}"'";
         tmphead="'"${tmphead}"'"; 
    printf "( dd if=%s\n", tmphead }
  { printf "  dd if=%s bs=%d count=%d\n", $1, dtsblock, ($4-$3) }
  END{ printf ") > %s/dts/r14t5.aud\n", dstdir}' > ${tmptoc}.2

echo Generating the track file.
sh -x ${tmptoc}.2

echo Generating the table of contents

< ${tmptoc}.1 $AWK '
  BEGIN{ dtsblock="'"${dtsblock}"'"; headerlen="'"${headerlen}"'";
    printf ";NAME           SERIAL  START   END     OFFSET\r\n";
    printf ";----           ------  -----   ---     ------\r\n";
    offset=headerlen
  }
  { printf "%s\t%s%s\t%s\t%s\t%d\r\n",
    $1, (length($1)<8)?"\t":"", $2, $3, $4, offset;
    offset=offset + ($4-$3)*dtsblock;
  }
  END{ printf "\032"; }' > ${dstdir}/dts/r14trlr.txt

echo Copying DTS.EXE

cp -f ${srcdir}/dts.exe ${dstdir}/dts.exe

echo Generating the human-readable table-of-contents ${dstht}
< ${tmptoc}.1 awk '{print $6}' > ${dstht}
cat ${dstht}
