#!/bin/ksh
#
# Use this script to test if files are the same in all configurations in this directory. All files in the GENERAL, POST and EXPERIMENTS 
# directories with the same name and path should be the identic before commit to svn. The difference between configurations are the 
# scripts for compiling and which files are available. 
#
# Syntax
# To test all files in all configurations :
# ./diff_configuration.ksh
# To test the files file1 and file2 in all configurations :
# ./diff_configuration.ksh file1 file2
# 
# Josefine Ghattas IPSL

BaseDir=`pwd`
ConfigFamily=v6
ConfigList=`ls |grep ${ConfigFamily} |grep -v out`
ConfigList=$ConfigList" IPSLCM6" 
verbose=no
echo All configurations to tests : $ConfigList

if [ $# == 0 ] ; then
    TestAll="yes"
    echo "Test all files."
else
    TestAll="no"
    FileList0=$*
    echo "Test only following files : $FileList0"
fi


for conf in $ConfigList ; do
    # Construct list of files to test
    if [ $TestAll = "yes" ] ; then
	FileList=`ls -d ${conf}/GENERAL/DRIVER/* ${conf}/GENERAL/PARAM/* ${conf}/GENERAL/POST/* ${conf}/EXPERIMENTS/*/*/* ${conf}/EXPERIMENTS/*/*/*/*`
    else
	FileList=""
	for file in $FileList0 ; do
	    FileList=$( echo $FileList `ls ${conf}/GENERAL/*/$file`)
	done
    fi

    for conf2 in $ConfigList ; do
	if [ $conf2 != $conf ] ; then
	    for file in $FileList ; do
	        # Remove conf from path
		file=$( echo $file | awk -F"${conf}/" '{print $2}' )
		
	        # Test if the file exists also in second directory, if yes test difference
		if [ -f ${conf2}/${file} ] ; then 
                    # Test if files are different
		    cmp -s ${conf}/${file}  ${conf2}/${file}
		    if [ $? -ne 0 ] ; then
			diff --brief ${conf}/${file}  ${conf2}/${file}
			if [ $verbose == yes ] ; then
			    diff  ${conf}/${file}  ${conf2}/${file}
			    echo
			fi
		    fi
		fi
	    done
	fi
    done
    
done
