#=========================================================================
#
# pws2rcs
# modified by Dave P. Willis 10/88
# original is in olspws2rcs.
#
# Goal:
#
#       The goal of this program is to pass updated or new files from
#       the Pascal Turn machine to the RCS system on the RCS machine.
#
# Givens:
#
#       There exists two seperate machines:
#               1) The RCS machine (currently hpfclw)
#               2) The PWS machine (currently pawsturn)
#
#       Both machines are running UNIX.  The PWS machine is both
#       a UNIX machine and a PWS machine that uses a common HFS disk.
#
#       The naming conventions on the RCS machine and the PWS machine
#       are different.  The name differences determine the file type on
#       the PWS machine.  The differences have been determined historically
#       and I claim no deep understanding as to they whys.
#
# Operation.
#       There are two levels of
#       comparison, first are there files with same names on both machines,
#       and second, are those files identical in content?  When comparing
#       if files are on both machines, the file names are sorted into
#       three categories:
#
#               1) On RCS but not on PWS
#               2) On PWS but not on RCS
#               3) On both.
#
#       for case 1):
#               Log this anomoly.
#
#       for case 2):
#               Using newrcs, add this new PWS file to the RCS system on
#               the RCS machine.
#
#       for case 3):
#               Compare the contents of the files from both systems.
#               if they are identical, do nothing.  Otherwise, check in,
#               using ci, the PWS file into the RCS system on the
#               RCS machine.
#
#       All unix output is logged into the log directory of the RCS machine.
#       the logname is cmplog.<month><day>
#
# DEW - 11/07/88
#   updated to update the build tools on the SRM under RCS control.
#=========================================================================

#
# if the user is running under root, kick them off.
#
if id | grep -l root
then
	echo DO NOT RUN ME AS ROOT!!
	exit 1
fi

PATH=:/bin:/usr/bin:/src/paws/bin:/usr/local/bin
TMPDIR=/tmp/$$
RCSDIR=/src/paws/src
NETNAME=/net/pawsturn
PWSORG=$NETNAME/source
PWSSHADOW=$NETNAME/tmpsrc
REMOVES=/src/paws/bin/nocopies
TOOLSDIR=/src/paws/SRMTOOLS

#=========================================================================
# make sure that the rcs directory structure is clean and that there
# are no locked files.
#=========================================================================
docheck=1
if [ $# -ge 1 ]
then
	if [ $1 = nocheck ]
	then
		docheck=0
	fi
fi
if [ docheck -eq 1 ]
then
	chkrcsclean
	if [ $? -eq 1 ]
	then
		echo chkrcsclean failure
		exit 1
	fi
fi

#=========================================================================
# set up a remote access to the PWS machine filesystem.
#=========================================================================
netunam $NETNAME paws:error104

#=========================================================================
# determine the logfile name
#=========================================================================
date=`date`
month=`expr substr "$date" 5 3`
day=`expr substr "$date" 9 2 | tr ' ' 0`
LOGFILE=$RCSDIR/../log/cmplog.$month$day


#=========================================================================
# create a shadow subtree on the PWS machine that will contain
# files linked to the original tree, but will have names more
# (but not completely) compatible with the RCS machine.
#
# file name modification conventions for this go around:
#       x.TEXT -> x.TEXT
#       x.c, x.h -> x.c, x.h
#       CCOMP Specials -> Special.TEXT
#
# some files are purposely NOT linked accross.
#=========================================================================
echo +++++ linking files in $PWSORG to files in $PWSSHADOW +++++ >>$LOGFILE
echo linking files in $PWSORG to files in $PWSSHADOW
rm -rf $PWSSHADOW
mkdir $PWSSHADOW
cd $PWSSHADOW

mkdir OSFS INIT PROGS HPROGS IOLIB HFS INIT2 ASSM COMP1 LIBR MYSYS SPECS RS232 PCLIB PCLIB/CRT PCLIB/GEN PCLIB/STDIO PCLIB/SYS PCLIB/INCLUDE PCLIB/INCLUDE/SYS PCLIB/INCLUDE/MACHINE CCOMP VME SCSI FP40/PAWS_FP40

#
# link x.TEXt -> x.TEXT
#
for d in OSFS INIT PROGS HPROGS IOLIB HFS INIT2 ASSM COMP1 LIBR MYSYS SPECS RS232 PCLIB PCLIB/CRT PCLIB/SYS VME SCSI FP40/PAWS_FP40
  do
  ln $PWSORG/$d/*.TEXT $PWSSHADOW/$d 2> /dev/null
  done

#
# link x.h, x.c -> x.h, x.c
#
for d in HPROGS PCLIB/GEN PCLIB/STDIO PCLIB/INCLUDE PCLIB/INCLUDE/SYS PCLIB/INCLUDE/MACHINE
  do
  ln $PWSORG/$d/*.c $PWSORG/$d/*.h $PWSSHADOW/$d
  done

#
# handle CCOMP specially
#
ln $PWSORG/CCOMP/* $PWSSHADOW/CCOMP
rm $PWSSHADOW/CCOMP/*.CODE $PWSSHADOW/CCOMP/*.s
for f in common manifest macdefs mac2defs mfile1 mfile2
  do
  mv $PWSSHADOW/CCOMP/$f $PWSSHADOW/CCOMP/$f.TEXT
  done

#
# Remove files from the shadow directory that shouldn't be there.
#
for i in `cat $REMOVES`; do rm $PWSSHADOW/$i; done

echo +++++ done linking files +++++ >>$LOGFILE
echo done linking files



#=========================================================================
# Compute the first level of comparison.
# Determine which files are on both system, and on one system but
# not the other.
#=========================================================================
echo +++++Results of PWS/RCS comparison $date+++++ >>$LOGFILE
echo Compare PWS sources with RCS sources $date

# run out of a junk directory
mkdir $TMPDIR

# look at each rcs directory
cd $RCSDIR
find . -type d -print | cut -c3- | grep -v RCS > $TMPDIR/rcssubdirs
cd $TMPDIR

for subdir in `cat $TMPDIR/rcssubdirs`
do

subdiruc=`echo $subdir | tr '[a-z]' '[A-Z]'`
echo subdirectory $subdir
echo "\nsubdirectory $subdir" >>$LOGFILE

# make the following lists of files:
#       RCSf -- files on RCS machine
#       RCSp -- files on RCS, pws-style names
#       PWSf -- files on PWS
#       hnt (here not there) -- files on RCS, not on PWS
#       tnh (there not here) -- files on PWS, not on RCS
#       both -- files in both places
#
# suffix changes to produce PWS names:
#       .p,.s -> .TEXT
#       .c,.h remain
#       no suffix -> .TEXT

ls -1 $RCSDIR/$subdir/RCS | grep '.*,v$' >RCSf
sed '1,${
s/,v$//
/\.[ps]$/s/\.[ps]$//
/\.[ch]$/!s/$/.TEXT/
}' < RCSf >RCSp

ls $PWSSHADOW/$subdiruc | egrep '.TEXT$|.c$|.h$' | sort >PWSf

comm -23 RCSp PWSf >hnt
comm -13 RCSp PWSf >tnh
comm -12 RCSp PWSf >both



#=========================================================================
# Second level comparison or handling.
#  for files on rcs but not on PWS, note in log
#  for files on pws but not on rcs, add them to the RCS system.
#  for files on both, compare.  If differences, update RCS system.
#=========================================================================

thisdir=`pwd`

# files here but not there -- just note them in log
if [ -s hnt ]; then
{ echo '\nFile(s) on RCS machine but not on PWS machine'
  cat hnt
  echo
}>>$LOGFILE
fi

# files there but not here -- we check them in
if [ -s tnh ]; then
echo checking in new files...
for newfile in `cat tnh`; do
    rcsname=`basename $newfile .TEXT`
    cp $PWSSHADOW/$subdiruc/$newfile $rcsname
    cd $RCSDIR/$subdir
    newrcs $thisdir/$rcsname
    cd $thisdir
done
{ echo '\nFile(s) on PWS machine but not on RCS machine, newly checked into rcs'
  cat tnh
  echo
}>>$LOGFILE
fi

# files in both places -- we do a comparison
echo comparing common files...
paste RCSf RCSp | fgrep -f both |\
while read rcsfile pwsfile; do
    co -q $RCSDIR/$subdir/RCS/$rcsfile
    namercs=`basename $rcsfile ,v`
    # be sure no name conflicts with pws name
    mv $namercs 5$namercs
    cp $PWSSHADOW/$subdiruc/$pwsfile $pwsfile
    expand $pwsfile > /tmp/exp.$$
    mv /tmp/exp.$$ $pwsfile
    normalize $pwsfile
    if cmp -s 5$namercs $pwsfile; then
	: files are the same
    else
	# files are different
	{ echo "\n$subdir/$pwsfile is different; diffs follow
	  (new file already put back into RCS)"
	  diff 5$namercs $pwsfile
	}>>$LOGFILE 2>&1
	cd $RCSDIR/$subdir
	co -q -l $namercs
	mv -f $thisdir/$pwsfile $namercs
	ci $namercs <<@

pws2rcs automatic delta on $date
@
	cd $thisdir
    fi
    rm -f 5$namercs $pwsfile
done
done

#=========================================================================
# update the build tools on the SRM to UNIX under RCS control
#=========================================================================
cd $TOOLSDIR
toolsupdate 2>&1 | tee >> $LOGFILE


#=========================================================================
# clean up
#=========================================================================
rm -f -r $TMPDIR
cd /

echo "removing $PWSSHADOW" >>$LOGFILE
echo "removing $PWSSHADOW"
rm -rf $PWSSHADOW

echo "\ndone" >>$LOGFILE
echo "done `date`"
