#!/bin/csh -f
# 
# write data directories to tape.  will append if tape not empty. eg:
# write_nights 19991120 19991121
#
# 25 Nov 1999
#

if ($#argv < 1) then
  echo "Usage: write_nights dir1 dir2 ..."
  echo "Eg, write_nights 19991120 19991121"
  exit
endif

alias do 'echo `date` \!* ; \!* '

foreach datadir ($*)
  if (! -e $datadir) then
     echo "Sorry, didn't find: $datadir"
     echo "Not writing to tape"
     exit
  endif
end

setenv TAPE /dev/rmt/0cn                # no-auto-rewind device

mt rewind >& /dev/null
mt status >& /dev/null

if ($status) then
  echo "Tape drive not ready it seems"
  exit
endif

mt fsr >& /dev/null                     # fsr fails if tape is empty

if (! $status) then                     # if not empty count no. of tar files
  mt eom >& /dev/null
  set nfiles = `mt status | perl -n -e '/file no=(\s+\d)/ ; print $1'`
  echo "Found $nfiles file(s), will append new file(s)"
else
  mt rewind
  echo "Writing new tape"
endif

foreach datadir ($*)
  do tar c $datadir
end

mt rewind

exit
