#!/bin/csh -f
# script to burn a cdrom

if (`whoami` != root) then
    echo must be root
    exit
endif

set v = `head -1 VERSION`
echo -n Create OCAAS version $v CDROM'? [y] (y/n) '
set ans = "$<"
if ("$ans" == "n") exit

echo -n Quick erase'? [n] (y/n) '
set ans = "$<"
if ("$ans" == "y") set quickerase

echo -n Make a new image'? [n] (y/n) '
set ans = "$<"
if ("$ans" == "y") set makenew
if ($?makenew) then
    set cdroot = /usr/local/cdimage/root
    echo -n 'image path ['$cdroot'] '
    set ans = "$<"
    if ($ans != "") set cdroot = $ans
    echo building based on $cdroot

    # sanity build checks
    echo making sanity checks
    foreach saned (telescope gsc home)
	set d = $cdroot/$saned
	if (! -e $d) then
	    echo missing $d
	    exit 1
	endif
    end

    # save stamps
    echo saving stamps
    cp install README COPYRIGHT $cdroot
    cp VERSION $cdroot
    cp VERSION $cdroot/telescope

    # save manual
    echo saving manual
    cp ~ecdowney/OCAAS/manual/ocaas.pdf $cdroot
    if ($status) exit 1

    # save source
    echo saving source
    set new = `ls -t ~ecdowney/telbu* | head -1`
    csi 1 "Zoom to OCaaS" < $new > $cdroot/.setup

    # clean up
    echo cleaning out logs and fifos
    touch $cdroot/telescope/comm/x.x
    rm $cdroot/telescope/comm/*
    touch $cdroot/telescope/archive/logs/x.x
    rm $cdroot/telescope/archive/logs/*
endif

# insert disk
if ($?quickerase) then
    echo -n insert CD-RW ..
else
    echo -n insert CD-R ..
endif
$<

# defines
set mkisofs = "/usr/lib/xcdroast-0.96e/bin/mymkisofs-1.12b4"
set cdrecord = "/usr/lib/xcdroast-0.96e/bin/cdrecord-1.6.1"
set mycmp = "/usr/lib/xcdroast-0.96e/bin/mycmp"
set raw = /usr/local/cdimage/image1.raw
set appid = "Observatory Contol and Astronomical Analysis System"
set preper = "Elwood C. Downey"
set pub = "Clear Sky Institute, Inc."

# :-)
sync

# go
set tmp = /tmp/burn.$$
echo logging to $tmp

if ($?makenew) then
    echo "=================================================" >> $tmp

    echo removing but preserving usno link
    set usnoln = $cdroot/telescope/archive/catalogs/usno
    set saveusno = `ls -l $usnoln | awk '{print $NF;}'`
    rm $usnoln

    echo temporarily adding links to .xe and sites files
    ln -s /mnt/db/tycho/tycho.xe $cdroot/telescope/archive/catalogs/tycho.xe
    ln -s /mnt/db/PPM/ppm.xe $cdroot/telescope/archive/catalogs/ppm.xe
    ln -s $cdroot/telescope/xephem/auxil/xephem.sit $cdroot/telescope/xephem/auxil/xephem_sites

    echo making iso9660 master
    date >> $tmp
    rm -f $raw
    echo $mkisofs -o $raw -f -a -l -r -L -N -V "OCAAS $v" -P "$pub" -p "$preper" -A "$appid"  $cdroot >>& $tmp
    $mkisofs -o $raw -f -a -l -r -L -N -V "OCAAS $v" -P "$pub" -p "$preper" -A "$appid"  $cdroot >>& $tmp
    set trouble = $status

    echo restoring usno link
    ln -s $saveusno $usnoln

    echo removing links to .xe and sites files
    rm $cdroot/telescope/archive/catalogs/tycho.xe
    rm $cdroot/telescope/archive/catalogs/ppm.xe
    rm $cdroot/telescope/xephem/auxil/xephem_sites

    if ($trouble) exit 1

endif

if ($?quickerase) then
    echo "=================================================" >> $tmp
    echo quick erase
    date >> $tmp
    echo $cdrecord -v speed=2 dev=0,01,00 -blank=fast >>& $tmp
    $cdrecord -v speed=2 dev=0,01,00 -blank=fast >>& $tmp
    if ($status) exit 1
endif

echo "=================================================" >> $tmp
echo writing disk
date >> $tmp
echo $cdrecord -v speed=2 dev=0,01,00 -data $raw >>& $tmp
$cdrecord -v speed=2 dev=0,01,00 -data $raw >>& $tmp
if ($status) exit 1
grep 'min fill' $tmp

echo "=================================================" >> $tmp
echo verifying
date >> $tmp
set raw2048 = `ls -l $raw | awk '{print $5/2048;}'`
echo $mycmp /dev/sr0 $raw $raw2048 >>& $tmp
$mycmp /dev/sr0 $raw $raw2048 >>& $tmp
if ($status) then
    echo ""
    echo Compare failed ^G
    echo ""
    exit 1
endif


echo done 
date >> $tmp

# For RCS Only -- Do Not Edit
# @(#) $RCSfile: burn,v $ $Date: 2003/04/15 20:47:56 $ $Revision: 1.1.1.1 $ $Name:  $
