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

# STO: Modified from original "burn" script
# created by Elwood.  Made more generic
# 4/24/01

set CDHOME = (/home/cdbuilder)
# cdroot is set as a link to the real cdroot by makecd
set CDROOTDIR = ($CDHOME/cdroot)
set MANUALFILE= ($CDHOME/manual/ocaas.pdf)
set TAGFILES= ($CDHOME/tagfiles)

# resolve the link and turn into real path
cd $CDROOTDIR
set CDROOTDIR = `pwd`
cd $CDHOME

# scsi id of cd burner
# resolve the link and turn into real path
cd $CDROOTDIR
set CDROOTDIR = `pwd`
cd $CDHOME

# bus, target, lun
set CDSCSI = 0,0,0

# defines
set mkisofs = "mkisofs" # "/usr/lib/xcdroast-0.96e/bin/mymkisofs-1.12b4"
set cdrecord = "cdrecord" # "/usr/lib/xcdroast-0.96e/bin/cdrecord-1.6.1"
set mycmp = "cmp" # "/usr/lib/xcdroast-0.96e/bin/mycmp"
set v = `head -1 $CDROOTDIR/VERSION`
set raw = $CDROOTDIR/../Talon$v.iso
set appid = "'Talon Observatory Contol System'"
set preper = "'Torus'"
set pub = "'Torus Technologies, Inc.'"


if (`whoami` != root) then
    echo must be root -- enter root password
    su -c ./burncd
    exit
endif

cd $TAGFILES

echo -n Create Talon version $v CDROM'? [y] (y/n) '
set ans = "$<"
if ("$ans" == "n") exit

echo -n "SCSI LUN addr? [$CDSCSI]"
set ans = "$<"
if("$ans" != "") then
    set CDSCSI = $ans
endif

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 = $CDROOTDIR
    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

    # 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
clear
if ($?quickerase) then
    echo -n insert CD-RW ..
else
    echo -n insert CD-R ..
endif
$<

# :-)
sync

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

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

#Removed stuff seemingly unique to Elwood's setup (??)
#
#    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
# STO.. changed to remove 'follow sym links' and replaced ambiguous -a with allow-multidot and allow-lowercase
#    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
# 4/9/2002 --- umm... take out the -allow-multidot, -allow-lowercase stuff    
    echo $mkisofs -o $raw -l -r -L -N -V "Talon $v" -P "$pub" -p "$preper" -A "$appid"  $cdroot
    echo $mkisofs -o $raw -l -r -L -N -V "Talon $v" -P "$pub" -p "$preper" -A "$appid"  $cdroot >>& $tmp
    $mkisofs -o $raw -l -r -L -N -V "Talon $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=$CDSCSI -blank=fast >>& $tmp
    $cdrecord -v speed=2 dev=$CDSCSI -blank=fast >>& $tmp
    if ($status) exit 1
endif

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

echo "=================================================" >> $tmp
# Removed verify... cmp utility not available...
#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 CVS Only -- Do Not Edit
# $Id: burncd,v 1.1.1.1 2003/04/15 20:47:55 lostairman Exp $
