#!/bin/sh
#
# stage tape out
#       
#       @(#)DPMstageout	1.8  10/08/91  CERN CN-SW/DC Les Robertson
#
#  Copyright (C) 1990,1991 by CERN/CN/SW/DC
#  All rights reserved
#
#
#
# calling seqence:
#
# stageout 
#   {tpwrite parameters}
#         -v VSN [-V VID] [-g {CART|TAPE|SMCF}] [-l {sl|nl|al|blp}]
#         [-d {6250|1600}] [-q sequence] [-f fileid] [-F record-format]
#         [-L record-length] [-b block-size] [-N block-count] [-G]
#         [-t retention-period] [-S server]
#   {sfget parameters}
#         [-p pool] [-u user] [-s size] [-r] filename
#   {assign parameter}
#         [-U fun]
#
#    {other parameters}
#         [ -x ]  debug mode
#         [ -K ]  for Cray compatibility      (ignored)
#         [ -S sbin ] for Cray compatibility  (ignored)
#
####
#
# Modifications: 
#  26aug91: Clean up; add -G, -t, -S options
# 09sep91: Default user taken from environment variable STAGE_USER
#
######################################################################################
#
# Apollo systems: if necessary set the version to System V and re-issue the command
if [ "x$SYSTYPE" != "x" ]
  then
    if [ "x$SYSTYPE" != "xsys5.3" ]
      then
        ver sys5.3
        $0 $*
        exit $?
    fi
fi
#
# set defaults, initialise variables
#
CMD="stageout"
LIB="/usr/local/bin"
#LIB="/user/les/shift/test"
if [ ! -f "$LIB/sfget" -o ! -f "$LIB/sfrm" -o ! -f "$LIB/tpread" -o ! -f "$LIB/assign" ]
  then
    echo $CMD: 'missing SHIFT system function(s)' 1>&2
    exit 5
fi
VSNVID=FALSE
FUN=FALSE
FILENAME=""
tpparm=""
sfgetparm="-k"
sfrmparm=""
sfgetsize=""
assignparm=""
DEBUG=FALSE
if [ "x$STAGE_USER" != "x" ]
  then
    USER="-u $STAGE_USER"
  else
    USER=""
fi
#
#######################################################################################
#
#                  functions
#
#######################################################################################
#
usage()
{
  echo "usage: $CMD [-v VSN] [-V VID] [-g {CART|TAPE|SMCF}] [-l {sl|nl|al|blp}]" 1>&2
  echo "       [-d {6250|1600}] [-q sequence] [-f fileid] [-F record-format]" 1>&2
  echo "       [-L record-length] [-b block-size] [-N block-count] [-G]" 1>&2
  echo "       [-t retention-period] [-S server]" 1>&2
  echo "       [-p pool] [-u user] [-s size] [-r]" 1>&2
  echo "       [-U fun]" 1>&2
  echo "       filename" 1>&2
  exit 5
}
#####################################################################################
#
#
#
# parse parameters
#
set -- `getopt xrGKv:V:g:l:d:q:f:F:L:b:N:t:S:p:u:s:U: $*`
#
while [ $# -gt 0 ]
do
  case $1 in
  -x) set -x ;;
  -G) tpparm="$tpparm $1" ;;
  -r) sfgetparm="$sfgetparm $1" ;;
  -K) ;;
  --) ;;
  -?)
#     remaining options require a value
      if [ $# -lt 2 ]
        then
          usage
      fi
      case $1 in 
#     look for a tpread/tpwrite parameters
      -v)  VSNVID=TRUE
           tpparm="$tpparm $1 $2"
           shift
           ;;
      -V)  VSNVID=TRUE
	   tpparm="$tpparm $1 $2"
           shift
           ;;
      -g)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -l)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -d)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -q)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -f)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -F)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -L)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -b)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -N)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -t)  tpparm="$tpparm $1 $2"
           shift
           ;;
      -S)  tpparm="$tpparm $1 $2"
           shift
           ;;
#     and now sfget parameters
      -p)  sfgetparm="$sfgetparm $1 $2"
           sfrmparm="$sfrmparm $1 $2"
           shift
           ;;
      -u)  USER="$1 $2"
           shift
           ;;
      -s)  sfgetsize="$sfgetsize $1 $2"
           shift
           ;;
#     and an assign parameter
      -U)  FUN=TRUE
           assignparm="$2"
           shift
           ;;
      *)   echo $CMD: invalid option $1 1>&2
           usage
           ;;
      esac
      ;;
# must be the filename
  *)
       if [ "$FILENAME" != "" ]
         then
           usage
       fi
       FILENAME=$1
       ;;
  esac
  shift
done
#
# parameters parsed - check that a vsn and filename were supplied
if [ -z "$FILENAME" -o "$VSNVID" = "FALSE" ]
  then
    echo $CMD: "{vsn or vid} and filename required" 1>&2
    usage
fi
#
# add specified or defaulted user
sfgetparm="$sfgetparm $USER"
sfrmparm="$sfrmparm $USER"
#
# Obtain a fully qualified pathname
#
NFSPN=`sfget $sfgetparm $sfgetsize $FILENAME`
SFGSTAT=$?
if [ $SFGSTAT -lt 0 -o $SFGSTAT -gt 1 ]
  then
    echo $CMD: error detected by "sfget" 1>&2
    exit $SFGSTAT
fi
#
#
if [ $SFGSTAT -eq 1 ]
  then
#   the file should have existed
    echo $CMD: file $FILENAME does not exist in specified pool 1>&2
    exit 5
  else
#   initiate the copy to tape
    tpwrite $tpparm $NFSPN
    TPSTAT=$?
    if [ $TPSTAT -ne 0 ]
      then
        echo "$CMD: tape copy failed ($TPSTAT)"
        exit $TPSTAT
    fi
fi
#
# 
echo $NFSPN
exit 0
