#!/bin/ksh
# Check out (if necessary) the files listed in the file $2 from
# directory $1 and copy them to the SRM in directory $3
#
# $1  path name to RCS working directory
# $2  name of file the fileset is in (under /src/paws/bin/filesets)
# $3  SRM directory
#
# If the name of the script ($0) begins with the letter "l", the
# files will be checked out "locked".
#
# If "locked" is requested, root will be disallowed.

if [ $# -ne 3 ]
then
  echo usage: $0 RCSpath fileset SRMpath
  exit 1
fi

SRMdir=$3

case `basename $0` in
  l*) options="-l -p";;  # lock version used
  *)  options="-p";;
esac

uid=`id | cut -c5`

if [ $uid = 0 -a "$options" = "-l -p" ]
then
  echo Do not run $0 as root!!
  exit 1
fi

function copy {
  /usr/bin/srmcp -p $1 /dev/srm:$SRMdir/$1.TEXT 2>/dev/null
  if [ $? ]
  then
    echo deleting from SRM: $SRMdir/$1.TEXT
    /usr/bin/srmrm /dev/srm:$SRMdir/$1.TEXT
    /usr/bin/srmcp -p $1 /dev/srm:$SRMdir/$1.TEXT
  fi
  }

cd $1
cat /src/paws/bin/filesets/$2 | while read name
  do
  echo $name "\c"
  if [ -r $name ]
  then		# file is already there and is readable
    echo readable
    copy $name
  else		# file is not there or is not readable
    if [ -f $name ]
    then 	# file is there but not readable
      echo WARNING: $name may not be latest version.
      echo checking out to /tmp
      co $options RCS/$name,v | expand > /tmp/$name
      copy /tmp/$name
      rm /tmp/$name
    else 	# file is not yet checked out
      echo checking out
      co $options $name | expand > $name
      copy $name
      rm $name
    fi
  fi
done
