#! /bin/bash
verbose=0
#
#  Specify parameters:
#  srcdir -  The (cvs) directory containing the reference configurations
#  targdir - The directory containing the run-time configuration files.
#  archdir - The archive directory.
#  archsfx - Archive file suffix (todays date by default).
#
# cvsdir=${DMTSRC:-/export/home/dmt}/cvs_`uname -m`
cvsdir=${DMTSRC:-/export/home/dmt}/cvs
srcdir="$cvsdir/gds/Monitors"
targdir="$HOME/pars"
runid=`cat $targdir/RunID`
archdir="$targdir/archive/$runid"
archsfx=`date +%Y%m%d`
#
#  Loop over files in the target directory
for f in $targdir/* ; do

    #-----------------------------------  Look for a replacement file
    targname=`echo $f | sed -e "s,.*/,,g"`
    srcfile=`echo $srcdir/*/pars/$targname`
    if [ ! -f $srcfile ]; then
	srcfile=`echo $srcdir/*/$targname`
    fi
    if [ -d "$f" ] ; then 
	if [ $verbose -ne 0 ] ; then echo "$targfile is a directory" ; fi
    elif [ -h "$f" ] ; then 
	if [ $verbose -ne 0 ] ; then echo "$targfile is a link" ; fi
    elif [ ! -f "$srcfile" ] ; then
	echo "$targname is not in cvs"
    elif [ `diff -b $f $srcfile | wc -l` -eq 0 ] ; then
	if [ $verbose -ne 0 ] ; then echo "$targname checked" ; fi
    else
	echo "----------------   $targname differs"
	ls -o $f $srcfile
	if [ `stat -c "%Y" $srcfile` -gt `stat -c "%Y" $f` ] ; then
	    echo  -n "Replace $f with $srcfile? "; read yorn
	    if [ "$yorn" = "yes" ] ; then
		archfile="$archdir/${targname}.$archsfx"
		mv $f $archfile
		echo "$f archived to $archfile"
		cp -avu $srcfile $f
	    else
		echo "File $f not replaced!"
	    fi
	fi
    fi
done
