#!/bin/ksh
#
# Use this script to propagated modifcations done to files in one configuration
# into the all other configurations in the same directory. Files will be copied
# only where they already exist.
#
# Syntax :
# ./propagate_master_config MyConfig
# Modifcations in MyConfig will be copied to the other configurations.

# Josefine Ghattas IPSL

if [ $# == 0 ] ; then
    echo "You need to specify the configuration from which the modifications will be propagted"
    echo "./propagate_master_config MyConfig"
    exit
fi

MasterConfig=$1
verbose=yes
BaseDir=`pwd`
ConfigFamily=$(basename ${BaseDir}) 
ConfigList=`ls |grep ${ConfigFamily} |grep -v out |grep -v $MasterConfig`

echo All modifications in configuration $MasterConfig will copied to the other configurations $ConfigList

cd $BaseDir/$MasterConfig
FileList=`ls -d GENERAL/DRIVER/* GENERAL/PARAM/* GENERAL/POST/* USER_SPEC/*/*/* USER_SPEC/*/*/*/*`

cpfile=$BaseDir/cpfile

echo "" > $cpfile
cd $BaseDir
for conf in $ConfigList ; do
    for file in $FileList ; do
	if [ -f ${conf}/${file} ] ; then 
	    cmp -s ${MasterConfig}/${file}  ${conf}/${file}
	    if [ $? -ne 0 ] ; then
	    # Files are differents
		echo "cp ${MasterConfig}/${file} ${conf}/${file}" >> $cpfile
		if [ $verbose == yes ] ; then
		    diff  --brief ${MasterConfig}/${file}  ${conf}/${file}
		    diff  ${MasterConfig}/${file}  ${conf}/${file}
		fi	    
	    fi
	fi
    done
echo "" >> $cpfile
done

# Test if there is nothing to copy
grep cp $cpfile
if [ $? -ne 0 ] ; then
    echo "Noting to be done. All files are the same."
    rm $cpfile
    exit
fi

# List files to copy and ask for permission to copy
echo ""
cat $cpfile

echo "Would you like to do the actions listed above, answer yes/no ?"
echo -n " Your answer : "
read answer

case ${answer} in
yes|y)
chmod +x $cpfile
$cpfile
echo "Copy has now been done."
;;
no|n)
echo "Nothing will be done"
;;
*)
echo "Bad answer. Nothing will be dnoe"
;;
esac
rm $cpfile

exit
