#!/bin/sh
#
# Conv-host for Blue Gene:
#  Translates +pN-style conv-host options into 
# mpirun -npN options.
#  Copies executable to $BL_HOME directory before running.
#
# You MUST have a copy of MMCS running in another terminal window.
#

if [ -z "$BL_HOME" ]
then
	echo "You must set the BL_HOME environment variable"
	exit 1
fi

firstArg=""
args=""
pes=1

while [ $# -gt 0 ]
do
	case $1 in
	+p)
		pes=$2
		shift
		;;
	+p*)
		pes=`echo $1 | awk '{print substr($1,3)}'`
		;;
	*) 
		[ -z "$firstArg" ] && firstArg="$1"
		args="$args $1"
		;;
	esac
	shift
done

if [ $firstArg -nt $BL_HOME/$firstArg ]
then
  echo "Copying $firstArg to $BL_HOME..."
  cp $firstArg $BL_HOME 
  [ $? -ne 0 ] && exit 1
fi

echo "Running on $pes processors: $args"
exec /BlueLight/bin/CIORUN -p $pes $args
