#!/bin/sh
# Copyright (C) 1994-2018 Altair Engineering, Inc.
# For more information, contact Altair at www.altair.com.
#
# This file is part of the PBS Professional ("PBS Pro") software.
#
# Open Source License Information:
#
# PBS Pro is free software. You can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# PBS Pro 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 GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Commercial License Information:
#
# For a copy of the commercial license terms and conditions,
# go to: (http://www.pbspro.com/UserArea/agreement.html)
# or contact the Altair Legal Department.
#
# Altair’s dual-license business model allows companies, individuals, and
# organizations to create proprietary derivative works of PBS Pro and
# distribute them - whether embedded or bundled with other software -
# under a commercial license agreement.
#
# Use of Altair’s trademarks, including but not limited to "PBS™",
# "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
# trademark licensing policies.
#

if [ $# -eq 1 ] && [ $1 = "--version" ]; then
   echo pbs_version = @PBS_VERSION@
   exit 0
fi

mpirun="mpirun"
name=`basename $0`

if [ "${PBS_NODEFILE:-XX}" = "XX" ]; then
	if [ "$name" != "mpirun" ]; then
		echo "$name: Warning, not running under PBS"
	fi
	echo $mpirun $*
	$mpirun $*
	exit $?
fi

start=1
options=""
where=""
prog_args=""
np_spec=0
c_spec=0
while [ $# -gt 0 ]; do

  if [ $start -eq 1 ] ; then 
        if [ "XX$1" = "XX-np" ]; then
		np_spec=1
		options="$options $1"	
		shift
		options="$options $1"
        elif [ "XX$1" = "XX-c" ]; then
		c_spec=1
		options="$options $1"	
		shift
		options="$options $1"	
        elif [ "XX$1" = "XX-s" ]     ||
             [ "XX$1" = "XX-x" ]     ||
             [ "XX$1" = "XX-wd" ]    ||
             [ "XX$1" = "XX-p" ] ; then
		options="$options $1"	
		shift
		options="$options $1"	
        elif [ "XX$1" = "XX-ssi" ] ; then
		options="$options $1"	
		shift
		options="$options $1"	
		shift
		if [ `echo $1 | wc -w` -gt 1 ] ; then
			options="$options \"$1\""
		else
			options="$options $1"
		fi
	elif [ `expr match "$1" "-\+"` -ne 0 ] ; then
		options="$options $1"
	elif [ `expr match $1 "n\+"` -ne 0 ] 	||
             [ `expr match $1 "c\+"` -ne 0 ]   	||
             [ $1 = "h" ]   			||
             [ $1 = "o" ]   			||
             [ $1 = "N" ]   			||
             [ $1 = "C" ] ; then
		where="$where $1"
	else
		prog_args="$prog_args $1"
                start=0
        fi
  else
	prog_args="$prog_args $1"
  fi
  shift

done

# Under LAM >= 7, need to put a -s (new session)
# option to pbs_attach  so as to not redundantly
# attach the pids of lamds that may have been
# started by tm-enabled lamboot. 

if [ `(lamboot -V | egrep -c "LAM 7") 2>/dev/null` -ne 0 ] ; then
	s_opt="-s"
else
	s_opt=""
fi

# no <where> parameter
if [ "${where:-XX}" = "XX" ]; then
	if [ $c_spec -eq 0 ] && [ $np_spec -eq 0 ] ; then
		eval $mpirun $options $prog_args
	else
		eval $mpirun $options C pbs_attach -P -j ${PBS_JOBID} $s_opt $prog_args
	fi
else
	eval $mpirun $options $where pbs_attach -P -j ${PBS_JOBID} $s_opt $prog_args
fi
