########################################################################
#
#	UNIX2PWS for PWS 3.2
#	
#	script to copy the RCS file to PWS
#
#	Author: Angelika Hierath
#		April 1986
#
########################################################################
#
#	usage: unix2pws -a | -u | -f <file name>
#
#	unix2pws can be called with three different options. The -a option
#	copies all RCS files from $UXSRC/*/RCS onto the SRM system. If 
#	unix2pws 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
#		<dir>/file_name
#	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

UXSRC=/src/paws/src
PWSSRC=/NEWCODE
LOGDIR=/src/paws/log
LASTXFR=$LOGDIR/lastxfr

USAGE="usage: unix2pws -a | -u | -f <file name>"
if [ $# -lt 1 -o $# -gt 2 ] 
then
	echo $USAGE
	exit 1
fi

# 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

echo "\n=============================================================="\
	>>$LOGFILE
# parse options
case $1 in
	-a)		all=y
			echo "*** Full unix2pws started on `date`"
			echo "*** Full unix2pws 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 unix2pws started on `date`" >> $LOGFILE
				echo "*** Full unix2pws started on `date`"
			else
				echo "*** Update unix2pws started on `date`" >> $LOGFILE
				echo "*** Update unix2pws started on `date`"
				update=y
			fi;;
	-f)		filecp=y	
			if [ -z $2 ]
			then
				echo $USAGE
				exit 1
			fi
			if [ ! -f "$2" ]
			then
				echo "File $2 not existent"
				exit 1
			else
				echo "Full unix2pws started for files in $2 on `date`"
				echo "Full unix2pws 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 $UXSRC

if [ -z "$filecp" ]
# all or update
then
	for dir in `/bin/ls`
	do
	if [ -d "$dir" ]
	then
		# Find all the related files in the directory $dir/RCS
		echo "unix2pws of $dir on `date`"
		echo "\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" >>$LOGFILE
		echo "unix2pws 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
	
				case $filename in
					*.p,v) PWSBNAME=`basename $filename .p,v`;;
					*.c,v) PWSBNAME=`basename $filename .c,v`;;
					*.h,v) PWSBNAME=`basename $filename .h,v`;;
					*.s,v) PWSBNAME=`basename $filename .s,v`;;
					*,v)   PWSBNAME=`basename $filename ,v`;;
				esac
				PWSDIR=$PWSSRC/`echo $dir | tr [a-z] [A-Z]`
				PWSNAME=$PWSDIR/${PWSBNAME}.TEXT
				
				# srm copy the file
				co -p $filename | expand | srmcp -p  -v$SRMVOL \
							    - $SRMDEV:$PWSNAME
				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 "*** unix2pws finished on `date`"
	echo "*** unix2pws finished on `date`" >> $LOGFILE
	echo "=============================================================="
	echo "=============================================================="\
	       >>$LOGFILE
	
	# if successful touch $LASTXFR
	touch $LASTXFR
	
	if [ -z "$error2" ]
	then
		echo "copy semaphor"
	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`
				dir=`basename $dir`;;
			*)	dir=`basename $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
			case $filename in
				*.p,v) PWSBNAME=`basename $filename .p,v`;;
				*.c,v) PWSBNAME=`basename $filename .c,v`;;
				*.h,v) PWSBNAME=`basename $filename .h,v`;;
				*.s,v) PWSBNAME=`basename $filename .s,v`;;
				*,v)   PWSBNAME=`basename $filename ,v`;;
			esac
			PWSDIR=$PWSSRC/`echo $dir | tr [a-z] [A-Z]`
			PWSNAME=$PWSDIR/${PWSBNAME}.TEXT
			
			# srm copy the file
			co -p $filename | expand | srmcp -p  -v$SRMVOL \
						    - $SRMDEV:$PWSNAME
			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
	echo "*** unix2pws finished on `date`"
	echo "*** unix2pws finished on `date`" >> $LOGFILE
	echo "=============================================================="
	echo "=============================================================="\
	      >>$LOGFILE
fi
