#!/bin/sh
#BHEADER**********************************************************************
#
#  Copyright (c) 1995-2009, Lawrence Livermore National Security,
#  LLC. Produced at the Lawrence Livermore National Laboratory. Written
#  by the Parflow Team (see the CONTRIBUTORS file)
#  <parflow@lists.llnl.gov> CODE-OCEC-08-103. All rights reserved.
#
#  This file is part of Parflow. For details, see
#  http://www.llnl.gov/casc/parflow
#
#  Please read the COPYRIGHT file or Our Notice and the LICENSE file
#  for the GNU Lesser General Public License.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License (as published
#  by the Free Software Foundation) version 2.1 dated February 1999.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms
#  and conditions of the GNU General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA
#**********************************************************************EHEADER


#-----------------------------------------------------------------------------
#
# Program usage 
#
#-----------------------------------------------------------------------------
usage () {
   echo "usage: $0 run [-hvpg] pfidb_file num_procs"
   echo "    -h               help"
   echo "    -v               verbose"
   echo "    -p executable    executable to run"
   echo ""
   echo "Runs parflow on using mpi startup command (e.g. mpiexec, mpirun etc)."
}

#=============================================================================
#
# Verbose logging
#
#=============================================================================
pflog () {
   if [ $VERBOSE -eq 1 ]
   then
      echo "pfrun: $@"
   fi
}

if [ -f ~/.parflowrc ] 
then
   . ~/.parflowrc
fi

#=============================================================================
#
# Parse users arguments
#
#=============================================================================
VERBOSE=0
PROGRAM=""
DEBUG=""
while getopts vmhp: c
do
   case $c in
      h)
	 usage;
	 exit 1;;
      v)
	 VERBOSE=1
	 shift;
	 ;;
      p) 
	 PROGRAM=$2
	 shift 
	 shift;;
   esac
done

if [ ! \( "$#" -eq 2 \) ]; then
  usage
  exit 1
fi

PFSCRIPT=$1

if [ -z "$PROGRAM" ]
then
	#
	# The default is to run ParFlow
	#
	PROGRAM=parflow
fi

if [ -z ${PARFLOW_MEMORYCHECK_COMMAND} ]
then
   CMD="$PARFLOW_DIR/bin/$PROGRAM ${PFSCRIPT} 2>&1 > ${PFSCRIPT}.out.txt"
else
   CMD="${PARFLOW_MEMORYCHECK_COMMAND} ${PARFLOW_MEMORYCHECK_COMMAND_OPTIONS} $PARFLOW_DIR/bin/$PROGRAM ${PFSCRIPT} 2>&1 > ${PFSCRIPT}.out.txt"
fi

pflog "executing command : $CMD" 
eval $CMD
return_code=$?

exit ${return_code}






