#!/bin/ksh
PATH=/bin:/usr/bin:/etc:/usr/ucb:$HOME/bin:/usr/bin/X11:.:/usr/local/bin:/usr/abin
export PATH

#    edastapeinv.scr
#    EDAS Archive inventory program for data on cartridge or tape

   # blocksize
#     BLOCK=30475

#    RAMS ARC block size
     if [ $# -le 0 ];then 
      echo " Enter archive type (cb or ne)"
      read atyp 
     else
      atyp=$1
     fi
     if [ $atyp = cb ];then
      BLOCK=19820
     else 
      BLOCK=58220
     fi

   read dest?" (1) 4mm or (2) 8mm tape, or (3) cartridge archive? "
   
   # set device name and rewind
     HOST=`hostname`
     if [ $dest -eq 1 ]; then
        media=4mm_tape
        if [ $HOST = 'gus' ];then
          devi=rmt0
        else
          devi=rmt1
        fi
        tctl -f /dev/$devi rewind
     elif [ $dest -eq 2 ]; then
        media=8mm_tape
        devi=rmt0
        tctl -f /dev/$devi rewind
     elif [ $dest -eq 3 ]; then
        media=Cartridge
        devi=nst0
        st -f /dev/st0 rewind
     else
        echo "Termination: Must chose 1-3"; exit
     fi
     
     rm -f filedump datafile
     echo "$media inventory ......................................."
     echo "$media Archive Dump"            >filedump
     echo "Data Blocking Factor: $BLOCK"  >>filedump
     echo "                             " >>filedump

# ---- tape inventory ---------------------------------------
     if [ $dest -le 2 ]; then
        let file=0
        let prnt=0
while dd if=/dev/$devi.1 of=datafile ibs=$BLOCK count=1;do
    let file=$file+1
    let prnt=$prnt+1
 #  strng[$prnt]="F:`printf "%04d" $file` `head -c 8 datafile`" 
    strng[$prnt]="F:`printf "%06d" $file` `head -c 14 datafile`" 
    rm datafile
    if [ $prnt -ge 2 ];then
       let prnt=0
       echo "${strng[1]} ${strng[2]}" >>filedump
       strng[1]=" "; strng[2]=" "
    fi
done
  # dump for case when number of files not a multiple of 4
    if [ $prnt -ne 2 ];then
       echo "${strng[1]}" >>filedump
    fi

    echo; echo "rewinding..."
    tctl -f /dev/$devi rewind
    tctl -f /dev/$devi offline
    echo "Tape is unloaded"
    rm -f   datafile
    echo; echo "filedump:";  cat filedump
    qprt filedump 

# ---- cartridge inventory ----------------------------------
     else 
          let file=0
while dd if=/dev/nst0 of=datafile ibs=$BLOCK count=1; do
    let file=$file+1
    strng="F:`printf "%06d" $file` `head -c 14 datafile`" 
    rm datafile
    echo "${strng}" >>filedump
    st -f /dev/nst0 fsf 1
done
    echo; echo "rewinding..."
    st -f /dev/st0 rewind
    st -f /dev/st0 unload
    echo "Cartridge is unloaded"
    cdelete datafile
    echo; echo "filedump:"; cat filedump
    qprt filedump 
    fi
# -----------------------------------------------------------
    exit
