########################################################################
#
#       RCS2PWS for PWS 3.2
#
#       script to copy the RCS file to PWS
#
#       Author: Angelika Hierath
#               April 1986
#       Modification History:
#               19880119 Larry Fenske   skip SRM, go over LAN
#
########################################################################
#
#       usage: rcs2pws -a | -u | -f <file name> <nocheck>
#
#       rcs2pws can be called with three different options. The -a option
#       copies all RCS files from $RCSSRC/*/RCS onto the SRM system. If
#       rcs2pws is invoked with the -u option only those files in those
#       directories are copied that are newer than $LASTXFR. In the last
#       mode the user can specify a file name which contain the names of
#       all the files which should be copied. Those files are not compared
#       against a given file, they are always copied. The files have to be
#       in one of the two forms:
#               <dir>/RCS/file_name,v
#               <dir>/file_name,v
#       <dir> has to be relative to RCSSRC.
#       The file names on SRM are derived from the original one (all modes).
#       All lower case letters in directory names are translated into upper
#       case letters. If there is a suffix .c, .p or .h at the end of the
#       file name it is stripped. A .TEXT is appended to all file names on
#       the SRM.
#


SRMVOL=CLAWS
SRMHOME=/NEWCODE
SRMDEV=/dev/srm

RCSSRC=/src/paws/src
NETNAME=/net/pawsturn
PWSSRC=$NETNAME/source
LOGDIR=/src/paws/log
LASTXFR=$LOGDIR/lastxfr
flagfile=$RCSSRC/turninprogress		#must be indentical to newci and
					#allnewrev

netunam $NETNAME paws:error104
if [ $? -eq 1 ]
then
	echo netunam failure
	exit 1
fi
umask 0

USAGE="usage: $0 -a | -u | -f <file name> [nocheck]"
if [ $# -lt 1 -o $# -gt 3 ]
then
	echo $USAGE
	exit 1
fi
case $1 in
	-*) ;;
	*) echo $USAGE
	   exit 1 ;;
esac

docheck=1
if [ $# -eq 2 ]
then
	if [ $2 = nocheck ]
	then
		docheck=0
	fi
elif [ $# -eq 3 ]
then
	if [ $3 = nocheck ]
	then
		docheck=0
	fi
fi
if [ docheck -eq 1 ]
then
	chkrcsclean
	if [ $? -eq 1 ]
	then
		echo chkrcsclean failure
		exit 1
	fi
fi

echo "turn in progress." > $flagfile

# directories containing the files to be copied
dirlist="assm comp1 hfs init init2 iolib libr mysys osfs progs rs232 specs hprogs pclib pclib/crt pclib/gen pclib/stdio pclib/sys pclib/include pclib/include/machine pclib/include/sys ccomp vme scsi fp40/paws_fp40"

# initialize variables
all=
update=
filecp=
error1=
error2=

# generate logfile name
date=`date`
month=`expr substr "$date" 5 3`
day=`expr substr "$date" 9 2 | tr ' ' 0`
LOGFILE=$LOGDIR/cptosrm.$month$day

# nuke any leftover LOGFILE SFB 013087
>$LOGFILE

echo "\n=============================================================="\
	>>$LOGFILE
# parse options
case $1 in
	-a)             all=y
			echo "*** Full $0 started on `date`"
			echo "*** Full $0 started on `date`" >> $LOGFILE;;
	-u)             if [ ! -f "$LASTXFR" ]
			then
				all=y
				echo "WARNING: file $LASTXFR not existent" >>$LOGFILE
				echo "WARNING: file $LASTXFR not existent"
				echo "*** Full $0 started on `date`" >> $LOGFILE
				echo "*** Full $0 started on `date`"
			else
				echo "*** Update $0 started on `date`" >> $LOGFILE
				echo "*** Update $0 started on `date`"
				update=y
			fi;;
	-f)             filecp=y
			if [ -z $2 ]
			then
				echo "$2 is empty"
				echo $USAGE
				exit 1
			fi
			if [ ! -f "$2" ]
			then
				echo "File $2 not existent"
				exit 1
			else
				echo "Full $0 started for files in $2 on `date`"
				echo "Full $0 started for files in $2 on `date`" >>$LOGFILE
				case $2 in
					/*)     filelist=$2;;
					*)      filelist=`pwd`/$2;;
				esac
			fi;;
	*)              echo $USAGE
			exit 1;;
esac

#       change to the directory containing the PWS source
cd $RCSSRC

if [ -z "$filecp" ]
# all or update
then
	for dir in $dirlist
	do
	if [ -d "$dir" ]
	then
		# Find all the related files in the directory $dir/RCS
		echo "$0 of $dir on `date`"
		echo "\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" >>$LOGFILE
		echo "$0 of $dir on `date`" >>$LOGFILE
		error1=
		if [ -n "$all" ]
		then
			find $dir/RCS -name '*,v' -print >$dir/filelist
			if [ $? != 0 ]
			then
				error1=y
			fi
		else
			find $dir/RCS -name '*,v' -newer $LASTXFR -print  \
			     >$dir/filelist
			if [ $? != 0 ]
			then
				error1=y
			fi
		fi

		# For all those filenames do
		for filename in `cat $dir/filelist`
		do
			# file locked ??
			LOGINFO=`rlog -L -R $filename`
			if [ $? != 0 ]
			then
				echo "ERROR: cannot rlog $filename"
				echo "ERROR: cannot rlog $filename" >>$LOGFILE
			elif [ -n "$LOGINFO" ]
			then
				echo "ERROR: file  $filename locked for editing"
				echo "ERROR: file  $filename locked for editing" >>$LOGFILE
				error1=y
			else

				PWSDIR=$PWSSRC/`echo $dir | tr [a-z] [A-Z]`
				case $filename in
					*.p,v) PWSBNAME=`basename $filename .p,v`;;
					*.c,v) PWSBNAME=`basename $filename .c,v`
						PWSNAME=$PWSDIR/${PWSBNAME}.c;;
					*.h,v) PWSBNAME=`basename $filename .h,v`
						PWSNAME=$PWSDIR/${PWSBNAME}.h;;
					*.s,v) PWSBNAME=`basename $filename .s,v`;;
					*,v)   PWSBNAME=`basename $filename ,v`;;
				esac

				case $filename in
					*.c,v | *.h,v)
						co -p $filename > $PWSNAME;;
#                                               co -p $filename | srmcp -b -t59723 -v$SRMVOL  - $SRMDEV:$PWSNAME;;
					*.p,v | *.s,v | *,v) PWSNAME=$PWSDIR/${PWSBNAME}.TEXT
						co -p $filename | expand > $PWSNAME;;
#                                               co -p $filename | expand | srmcp -p  -v$SRMVOL  - $SRMDEV:$PWSNAME;;
				esac

				if [ $? != 0 ]
				then
					echo "ERROR: cannot copy file $filename"
					echo "ERROR: cannot copy file $filename" >>$LOGFILE
					error1=y
				else
					echo "copied: $filename"
				fi
			fi
		done
		echo "++++++ FILELIST of $dir ++++++" >>$LOGFILE
		cat $dir/filelist >>$LOGFILE
		rm $dir/filelist
		if [ -z "$error1" ]
		then
			echo "copy semaphor for $dir"
		else
			error2=y
		fi
	fi
	done

	echo "*** $0 finished on `date`"
	echo "*** $0 finished on `date`" >> $LOGFILE
	echo "=============================================================="
	echo "=============================================================="\
	       >>$LOGFILE

	# if successful touch $LASTXFR
	touch $LASTXFR

	if [ -z "$error2" ]
	then
		echo "copy semaphor"
	else
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"\
		      >>$LOGFILE
		echo "ERROR during $0 check $LOGFILE"
		echo "ERROR during $0 check $LOGFILE" >>$LOGFILE
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"\
		      >>$LOGFILE
	fi


# copy all files in $2
else
	for path in `cat $filelist`
	do
		filename=`basename $path`
		dir=`dirname $path`
		case $dir in
			*/RCS)  dir=`dirname $dir` ;;
			*)      ;;
		esac
		filename=$dir/RCS/$filename

		# file locked ??
		LOGINFO=`rlog -L -R $filename`
		if [ $? != 0 ]
		then
			echo "ERROR: cannot rlog $filename"
			echo "ERROR: cannot rlog $filename" >>$LOGFILE
		elif [ -n "$LOGINFO" ]
		then
			echo "ERROR: file  $filename locked for editing"
			echo "ERROR: file  $filename locked for editing" >>$LOGFILE
			error1=y
		else
			PWSDIR=$PWSSRC/`echo $dir | tr [a-z] [A-Z]`
			case $filename in
				*.p,v) PWSBNAME=`basename $filename .p,v`;;
				*.c,v) PWSBNAME=`basename $filename .c,v`
					PWSNAME=$PWSDIR/${PWSBNAME}.c;;
				*.h,v) PWSBNAME=`basename $filename .h,v`
					PWSNAME=$PWSDIR/${PWSBNAME}.h;;
				*.s,v) PWSBNAME=`basename $filename .s,v`;;
				*,v)   PWSBNAME=`basename $filename ,v`;;
			esac

			case $filename in
				*.c,v | *.h,v)
					co -p $filename  > PWSNAME;;
#                                       co -p $filename  | srmcp -b -t59723 -v$SRMVOL  - $SRMDEV:$PWSNAME;;
				*.p,v | *.s,v | *,v) PWSNAME=$PWSDIR/${PWSBNAME}.TEXT
					co -p $filename | expand > $PWSNAME;;
#                                       co -p $filename | expand | srmcp -p  -v$SRMVOL  - $SRMDEV:$PWSNAME;;
			esac
			if [ $? != 0 ]
			then
				echo "ERROR: cannot copy file $filename"
				echo "ERROR: cannot copy file $filename" >>$LOGFILE
				error1=y
			else
				echo "copied: $filename"
			fi
		fi
	done
	echo "++++++ FILELIST from $filelist ++++++" >>$LOGFILE
	cat $filelist >>$LOGFILE
fi


# Do the rest of what /TOOLS/CPTOLOCAL did.
# All the files are already in .UX form.
echo "renaming some CCOMP files from *.TEXT to *"
echo "renaming some CCOMP files from *.TEXT to *" >>$LOGFILE

( cd $PWSSRC/CCOMP      # local 'cd'
for file in common mac2defs macdefs manifest mfile1 mfile2
	do
	if [ -f $file.TEXT ]
		then
		rm -f $file
		mv $file.TEXT $file
	fi
	echo "CCOMP/$file renamed"
	echo "CCOMP/$file renamed" >>$LOGFILE
done
)

# link the pws c-compiler include files to the /usr/include directory
# on the pws turn machine.
PWSINCLUDE=$NETNAME/usr/include
PWSSRCINCLUDE=$PWSSRC/PCLIB/INCLUDE

echo "linking the pws c-compiler include files to /usr/include."
echo "linking the pws c-compiler include files to /usr/include." >> $LOGFILE

#remove everything under /usr/include
cd $PWSINCLUDE
rm -f -r . >> $LOGFILE 2>&1
mkdir machine sys

cd $PWSSRCINCLUDE/MACHINE
ln * $PWSINCLUDE/machine >> $LOGFILE 2>&1

cd $PWSSRCINCLUDE/SYS
ln * $PWSINCLUDE/sys >> $LOGFILE 2>&1

cd $PWSSRCINCLUDE
ln * $PWSINCLUDE >> $LOGFILE 2>&1  # this will generate two errors.




echo "*** $0 finished on `date`"
echo "*** $0 finished on `date`" >> $LOGFILE
echo "=============================================================="
echo "=============================================================="\
      >>$LOGFILE
