#! /bin/csh -f

set cmd=`basename $0`

set mandargs='(-i *[1-9][0-9]* +[1-9][0-9]* *)'
set optargs='(-S *[1-9][0-9]* *)*'

if (`echo $*|egrep '(^((('"$optargs"'*)('"$mandargs"')('"$optargs"'*))|([1-9][0-9]*))$)'|wc -l` < 1) then
  echo "usage: $cmd "'<cartridge-number>'
  echo "       $cmd "'[ -S <cartridge-set-number> ] \'
  echo "              "'-i <cartridge-number> <file-number>'
  exit 1
endif

set cartset=1

if ($#argv > 1) then
  set i=1
  while ($i <= $#argv)
    if ("_$argv[$i]" == "_-i") then
      @ i++
      set cartno=$argv[$i]
      @ i++
      set fileno=$argv[$i]
    endif
    if ("_$argv[$i]" == "_-S") then
      @ i++
      set cartset=$argv[$i]
    endif
    @ i++
  end
endif

if (! $?BACKUP_HOME) then
  set pnam=$0
  set pnam = $pnam:t
  if ($pnam == $0 || ./$pnam == $0) then
    foreach dir ($path)
      set p=$dir/$pnam
      if (-x $p) then
	break;
      endif
    end
    if ($p == ./$pnam) then
      set p=`pwd`
      setenv BACKUP_HOME  $p:h
    else
      set p=$p:h
      setenv BACKUP_HOME  $p:h
    endif
  else
    if (`echo $0|cut -c1` != "/") then
      set p=`pwd`/$0
    else
      set p=$0
    endif
    set p=$p:h
    setenv BACKUP_HOME $p:h
  endif
  set bindir="$p"
else
  set bindir=$BACKUP_HOME/bin
endif

set configfile=(`grep "$bindir/[^/]*server" /etc/inetd.conf|awk 'NR==1 {print $NF}'`)
if ($configfile == "") then
  echo Cannot find configuration file, assuming $BACKUP_HOME/lib/backup.conf
  set configfile=$BACKUP_HOME/lib/backup.conf
endif

# the solaris awk is doin' dawn f...... BS
foreach awk (nawk gawk awk)
  foreach dir ($path)
    if (-x $dir/$awk) then
      setenv AWK $dir/$awk
      break
    endif
  end
  if ($?AWK) break
end

if (! $?AWK) then
  echo 'No awk ? Is this really one of the wonderful UNIX-es ?'
  echo 'Sorry. I have to exit.'
  exit 1
endif

alias getparam $AWK' '"'"'/^[ 	]*'"'"'\!:1'"'"'/{split($0,a,"'"'"'\!:1'"'"'[ 	]*"); print a[2]}'"' $configfile"

set tapeposfile=`getparam '^[ 	]*[Tt]ape[-_ 	]*[Pp]osi?t?i?o?n?[-_ 	]*[Ff]ile:?'`
if ($tapeposfile == "") then
  echo Cannot find tape-positioning file.
  exit 1
endif

set lastcarts=(`getparam '^[ 	]*[Ll]ast[-_ 	]*[Cc]artr?i?d?g?e?s[-_ 	]*:?'`)
set num_cartsets=$#lastcarts
if ($num_cartsets < 1) then
  set num_cartsets=1
endif

if (-r $tapeposfile) then
  set nums=(`cat $tapeposfile`)
else
  if ($#argv == 1) then
    set cartno=$1
  endif
  echo 'Warning: Assuming initial startup. Setting read- and write-position'
  echo '         to cartridge '$cartno', file 1.'
  echo '         Storing into file '$tapeposfile.

  set nums=($cartno 1 $cartno 1)
  set fileno=1
endif

@ cartsets=$#nums / 2 - 1
while ($cartsets < $num_cartsets)
  set nums=($nums 0 0)
  @ cartsets=$#nums / 2 - 1
end

if ($#nums < 4) then
  echo Tape positioning file has invalid format.
  exit 1
endif

if ($#argv == 1) then
  set nums[1]="$1"
else
  @ idx1=2 * $cartset + 1
  @ idx2=$idx1 + 1

  if ($idx1 > $#nums) then
    echo Error: cartridge set number too high 'max: '$cartsets
    exit 1
  endif
  if ($idx1 < 1) then
    echo Error: Illegal cartridge number.
    exit 1
  endif

  set nums[$idx1]=$cartno
  set nums[$idx2]=$fileno
endif

echo $nums >! $tapeposfile
