#!/bin/csh
################### Start of $RCSfile: autocptapes.in,v $ ##################
#
# $Source: /home/alb/afbackup/afbackup-3.3.9beta4/RCS/autocptapes.in,v $
# $Id: autocptapes.in,v 1.3 2004/07/08 20:34:42 alb Exp alb $
# $Date: 2004/07/08 20:34:42 $
# $Author: alb $
#
#
####### description ################################################
#
#
#
####################################################################

### User settable section ###

#
# The backup server where to copy to, and the port number
#
#set TARGETSERVER=localhost
#set TARGETPORT=2989
#set TARGETKEYFILE=""

#
# The offset to get the target cartridge numbers from the source numbers
#
#set CARTNUMOFFSET=0

### End of user settable section ###

onintr cleanup

if (`echo "$*"|egrep '^ *-[khpo] *[^ ]+( +-[khpo] *[^ ]+)* *$'|wc -l` < 1) then
  goto usage
endif

set args=`getopt h:k:o:p: "$*"`
@ n=$#args / 2
@ r=$#args - ( $n * 2 )
if ($r != 1) goto usage

@ n=$#args - 1

set i=1
while ($i <= $n)
  set opt="$argv[$i]"
  @ i++
  set arg="$argv[$i]"
  @ i++

  switch (_"$opt")
    case _-k:
	set TARGETKEYFILE="$arg"
	breaksw;
    case _-h:
	set TARGETSERVER="$arg"
	breaksw;
    case _-p:
	set TARGETPORT="$arg"
	breaksw;
    case _-o:
	set CARTNUMOFFSET="$arg"
	breaksw;
    default:
	goto usage
  endsw
end

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

  set oldpath=$p

 restart_resolv:
  set newpath="$oldpath"_
  while ("$newpath" != "$oldpath")
    set newpath="$oldpath"

    set oldpath=`echo "$oldpath"|sed 's#/\./#/#g;s#/\.$##g;s#//*#/#g;s#^\./##g;s#^/\.\./#/#g'`

    if ("$oldpath" != '/') then
      set oldpath=`echo "$oldpath"|sed 's#/*$##g'`
    endif
  end

  if (`echo "$oldpath"|cut -c1` == "/") then
    set oldhead="/"`echo "$oldpath"|cut -c2-|sed 's#/.*$##g'`
    set oldtail=`echo "$oldpath"|cut -c2-|sed 's#^[^/]*/##'`
  else
    set oldhead=`echo "$oldpath"|sed 's#/.*$##g'`
    set oldtail=`echo "$oldpath"|sed 's#^[^/]*/##'`
  endif
  if ("$oldpath" == "$oldhead") set oldtail=""

  while (1)
    if ("$oldhead" == "") set oldhead="."
    if (! -f "$oldhead" && ! -d "$oldhead") then
      set newpath=""
      goto getout_1
    endif

    set linkchar=`ls -ld "$oldhead"|cut -c1`
    if ($linkchar == 'l') then
      set points_to=`ls -ld "$oldhead"|sed 's#^.*->[ 	]*##g'`

      if (`echo "$points_to"|cut -c1` == '/') then
        set newpath="$points_to"/"$oldtail"
      else
        set newpath=`dirname "$oldhead"`/"$points_to"/"$oldtail"
      endif

      set oldpath="$newpath"
      goto restart_resolv
    endif

    if ("$oldtail" == "") then
      set oldpath="$oldhead"
      break
    endif

    set oldhead="$oldhead"/`echo "$oldtail"|sed 's#/.*$##g'`
    if (`echo "$oldtail"|grep /|wc -l` < 1) then
      set oldtail=''
    else
      set oldtail=`echo "$oldtail"|sed 's#^[^/]*/##g'`
    endif
  end

  set newpath="$oldpath"_
  while ("$newpath" != "$oldpath")
    set newpath="$oldpath"

    set oldpath=`echo $oldpath|sed 's#[^/][^/]*/\.\./##g;s#[^/][^/]*/\.\.$##g;s#/\./#/#g;s#/\.$##g;s#//*#/#g;s#^\./##g;s#^/\.\./#/#g'`

    if ("$oldpath" != '/') then
      set oldpath=`echo "$oldpath"|sed 's#/*$##g'`
    endif
  end

 getout_1:

  set p="$newpath"
  set p=$p:h

  setenv BACKUP_HOME $p:h
endif


set path=($BACKUP_HOME/bin $path)
rehash

unsetenv AWK

# 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 a lovely UNIX ?'
  echo 'Sorry. I have to exit.'
  exit 1
endif

set found=0
set configfiles=($BACKUP_HOME/lib/backup.conf /etc/buclient.conf /etc/afbuclient.conf /etc/afclient.conf /etc/afbackup/client.conf "/etc/afbackup/client.conf")

foreach configfile ($configfiles)
  if (-r $configfile) then
    set found=1
    break
  endif
end
if (! $found) then
  echo "No configuration file found. Exiting."
  exit 2
endif

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

set SOURCEKEYFILE=`getparam '([Ee]n)?[Cc]rypti?o?n?[ 	_-]*[Kk]ey[ 	_-]*[Ff]ile:?[ 	]*'`

set SOURCEKEYOPT=""
if ($SOURCEKEYFILE != "") then
  set SOURCEKEYOPT="-k $SOURCEKEYFILE"
endif

set EXITST=0

set TMPFILE=/tmp/autocptape.$$

/bin/rm -f $TMPFILE

onintr cleanup

cat > $TMPFILE

set SOURCESERVER=`grep -i 'Backup Server:' $TMPFILE|awk '{print $NF}'`
set SOURCEPORT=`grep -i 'Backup Port:' $TMPFILE|awk '{print $NF}'`
set CARTSUSED=`grep -i 'Used Cartridges:' $TMPFILE|awk '{print $NF}'`
set FIRSTFILEUSED=`grep -i 'First Tapefile accessed:' $TMPFILE|awk '{print $NF}'`
set FIRSTCARTUSED=`grep -i 'First Cartridge used:' $TMPFILE|awk '{print $NF}'`
set EXITSTATUS=`grep -i 'Exit status:' $TMPFILE|tail -1|awk '{print $NF}'`
if ($EXITSTATUS != 0) then
  set EXITST=$EXITSTATUS
  goto cleanup
endif

/bin/rm -f $TMPFILE

if (! $?TARGETSERVER) then
  set TARGETSERVER="$SOURCESERVER"
endif
if (! $?TARGETPORT) then
  set TARGETPORT=$SOURCEPORT
endif
set TARGETKEYOPT=""
if (! $?TARGETKEYFILE) then
  set TARGETKEYFILE="$SOURCEKEYFILE"
endif
if (! $?CARTNUMOFFSET) then
  set CARTNUMOFFSET=0
endif

if ($TARGETKEYFILE != "") then
  set TARGETKEYOPT="-k $TARGETKEYFILE"
endif

foreach cartrange (`echo $CARTSUSED|tr "," " "`)
  set ACTCART=`echo $cartrange|cut -d- -f1`
  if (`echo $cartrange|grep '[-]'|wc -l` < 1) then
    set LASTCART=$ACTCART
  else
    set LASTCART=`echo $cartrange|cut -d- -f2`
  endif

  while (1)
    @ TARGETCART=$ACTCART + ( $CARTNUMOFFSET )

    set TAPEFILEOPT=""
    if ($ACTCART == $FIRSTCARTUSED) then
      set TAPEFILEOPT="-F $FIRSTFILEUSED"
    endif

    eval $BACKUP_HOME/bin/copy_tape -C $ACTCART $TAPEFILEOPT -h $SOURCESERVER -P $SOURCEPORT \
	$SOURCEKEYOPT -D -h $TARGETSERVER -P $TARGETPORT $TARGETKEYOPT -C $TARGETCART
    if ($status != 0) then
      set EXITST=$status
      goto cleanup
    endif

    if ($ACTCART == $LASTCART) then
      break
    endif

    @ ACTCART++
  end
end


cleanup:
/bin/rm -f $TMPFILE

exit $EXITST


usage:
echo 'Usage: '`basename $0`' [ -k <targetkeyfile> ]' \
		'[ -h <targetserver> ] [ -p <targetport> ]' \
		'[ -o <cartridgeoffset> ]'
exit 2
