#!/bin/bash

#==============================================================================
# $Id: check_map.sh 41514 2012-11-02 21:26:54Z mlevy@ucar.edu $
# $URL: https://svn-ccsm-models.cgd.ucar.edu/tools/mapping/trunk_tags/mapping_121113b/check_maps/check_map.sh $
#
# Use an updated ESMF tool to check the quality of a mapping file
#
#==============================================================================
date
SDIR=`dirname $0`

#==============================================================================
# Usage subroutine
#==============================================================================

usage() {
	echo 'USAGE: ./check_maps.sh [OPTION]... FILELIST'
	echo 'Runs a modified version of the ESMF RegridWeightGenCheck program'\
	     'over files listed in FILELIST'
	echo ''
	echo '  --recompile, -rc  Force recompile (necessary to change verbose)'
	echo '  --verbose, -v     Compile with verbose output (use with -rc)'
	echo '  --clean, -c       Remove log / aux files generated by this script'
	echo '  --help, -h        Output this usage information'
	echo ''
	echo 'Notes:'
	echo '  1) For use on bluefire only!'
	echo '  2) Need to set ESMFMKFILE=/contrib/esmf-5.2.0r-64-O/lib/esmf.mk'\
	          'or compilation will fail'
	echo '  3) If -rc option is not enabled, -v flag is ignored and verbose /'\
	          'concise will depend on previous compilation'
}

#==============================================================================
# Main Program
#==============================================================================

# Process input arguments
verbose="FALSE"
compile="FALSE"
FileList=""
while [ $# -gt 0 ]; do
	case $1 in
		-rc|--recompile)
			compile="TRUE"
		;;
		-v|--verbose)
			verbose="TRUE"
		;;
		-h|--help)
			usage
			exit 0
		;;
		-c|--clean)
			rm -f *.Log hostfile
			echo 'Removed all .Log files and hostfile'
			exit 0
		;;
		* )
			if [ -e $1 ]; then
				FileList="$FileList $1"
			else
				echo "File not found: $1"
			fi
		;;
	esac
	shift
done

if [ -z "$FileList" ]; then
	echo "No files given!"
	usage
	exit 1
fi

hostname=`hostname`
# Environment to run interactively (bluefire / geyser only)
case $hostname in
  ## bluefire
  be*)
    if [ -z "$ESMFBIN_PATH" ]; then
      ESMFBIN_PATH=contrib/esmf-5.2.0r-64-O/bin
    fi

    if [ ! -z "$MP_EUIDEVICE" ]; then
    	# Disable MP_EUIDEVICE to avoid warning message
    	MP_EUIDEVICE_tmp=$MP_EUIDEVICE
    	unset MP_EUIDEVICE
    fi

    if [ ! -z "$MP_INSTANCES" ]; then
    	# Disable MP_INSTANCES to avoid warning message
    	MP_INSTANCES_tmp=$MP_INSTANCES
    	unset MP_INSTANCES
    fi
  ;;
  ## geyser (yellowstone in interactive mode)
  ge*)
    if [ -z "$ESMFBIN_PATH" ]; then
      ESMFBIN_PATH=contrib/esmf-5.2.0r-64-O/bin
    fi
  ;;
esac

if [ -z "$MPIEXEC" ]; then
	MPIEXEC="mpirun.lsf"
fi

export MP_PROCS=1
export MP_EUILIB=ip

echo $hostname > hostfile
declare -i p=2
until ((p>$MP_PROCS)); do
	echo $hostname >> hostfile
	p=p+1
done
export MP_HOSTFILE=hostfile

if [ $compile == "TRUE" ]; then
	echo "Building $EXE"
	CURR_DIR=$PWD
	cd $SDIR/src
	gmake VERBOSE=$verbose
	cd $CURR_DIR
fi

EXE=$SDIR/ESMF_RegridWeightGenCheck
if [ ! -e $EXE ]; then
	echo "WARNING: $EXE not found. To check quality of maps, build this tool manually or use the -rc flag"
	exit 1
fi

declare -i n=0

for MAP in $FileList
do
	if [ -e $MAP ]; then
		n=n+1
		echo "${n}: ${MAP}"
		$EXE $MAP
		echo "-----"
	else
		echo "File not found: $MAP"
	fi
done

if [ ! -z "$MP_EUIDEVICE_tmp" ]; then
	# Re-enable MP_EUIDEVICE (if previously defined)
	export MP_EUIDEVICE=$MP_EUIDEVICE_tmp
fi

if [ ! -z "$MP_INSTANCES_tmp" ]; then
	# Re-enable MP_EUIDEVICE (if previously defined)
	export MP_INSTANCES=$MP_INSTANCES_tmp
fi
