#!/bin/sh
#
#       HP High Performance Systems Division
#       (c)Copyright 2001 Hewlett-Packard Co.
#
#       $Revision: 3597 $
#
#       Function:       - MPI standardized job launching script
#
#       "@(#)HP MPI 02.02.05.00 (10/03/2006) Linux x86-64"
#

#
#       usage
#
#       Function:       - print usage information and exit
#
usage ()

{
        echo
        echo "Synopsis:"
        printf "\t%s\t%s\n\t\t%s\n\t\t%s\n" "$prog" \
                "[-n <maxprocs>] [-soft <ranges>] [-host <host>]" \
                "[-arch <arch>] [-wdir <dir>] [-path <dirs>]" \
                "[-file <file>] <command args>"
        printf "\t%s\t%s\n" "$prog" "<above> : <above> : ... : <above>"
        printf "\t%s\t%s\n" "$prog" "[-configfile <file>]"
        printf "\t%s\t%s\n" "$prog" "[-help]"
        echo
        echo "Description:  Lauch MPI application."
        echo
        echo "Options:  [-help]                Print this help menu."
        echo
        echo "          [-n <maxprocs>]        create <maxprocs> processes"
        echo "          [-soft <range list>]   ignored in HPMPI"
        echo "          [-host <host>]         create proceses on host <host>"
        echo "          [-arch <arch>]         ignored in HPMPI"
        echo "          [-wdir <dir>]          working dir for specified ranks"
        echo "          [-path <dirs>]         PATH env var for specified ranks"
        echo "          [-file <file>]         ignored in HPMPI"
        echo
        echo "          [-configfile <file>]   file of lines in above format"
}

#
#       deletefile
#
#       Function:      - delete temporory appfile and exit
#
deletefile()
{
        /bin/rm -rf $appfile
        exit $1
}

#
#       processline
#
#       Function:      - process one application "line"
#
processline()
{
        optionlist=
#
# Loop parsing command line arguments.
#
        while [ $# -ne 0 ] ; do
                case "$1" in
# help menu
                -help)  usage ; deletefile 0 ;;
# number of processes
                -n)     shift ; optionlist="$optionlist -np $1" ; shift ;;
# soft number of processes functionality (ignored)
                -soft)  shift ; shift ;;
# hostname
                -host)  shift ; optionlist="$optionlist -h $1" ; shift ;;
# architecture (ignored)
                -arch)  shift ; shift ;;
# working directory
                -wdir)  shift
                        optionlist="$optionlist -e MPI_WORKDIR=$1"
                        shift ;;
# path variable
                -path)  shift ; optionlist="$optionlist -sp $1" ; shift ;;
# additional options file (ignored)
                -file)  shift ; shift ;;
# bad argument
                -*)     echo "Error: Unknown option $1"
                        deletefile 1 ;;
# command arguments
                *)      optionlist="$optionlist $*"
                        break ;;
                esac
        done
        echo "$optionlist" >> $appfile
}

#
# main code
#
{
        PATH=/bin:/usr/bin:/usr/ucb
        prog=mpiexec

        tmpdir=${MPI_TMPDIR:-/tmp}
        MPI_ROOT=${MPI_ROOT:-$(cd $(dirname $0)/.. ; pwd)}
        appfile=$tmpdir/mpiexec.$$
        /bin/rm -rf $appfile
        touch $appfile

        trap "deletefile 1" 2
        trap "deletefile 1" 3
        trap "deletefile 1" 15

#
# Look for special -configfile usage
#
        if [ $# -eq 2 -a "$1" = "-configfile" ]; then
                ( read line
                while [ $? -eq 0 ]; do
                        if [ ! -z "${line##\#*}" ] ; then
                                processline $line
                        fi
                        read line
                done
                exit 0
                ) < $2
                if [ $? -ne 0 ]; then
                        exit 1
                fi
        else
                while [ $# -gt 0 ]; do
                        line=
                        while [ $# -gt 0 -a "$1" != ":" ]; do
                                line="$line $1"
                                shift
                        done
                        if [ $# -gt 0 ]; then
                                shift          # shift past ':'
                        fi
                        processline $line
                done
        fi

        rval=1
        if [ -s $appfile ] ; then
                $MPI_ROOT/bin/mpirun -f $appfile
                rval=$?
        else
                echo "Could not construct an appfile ($appfile)"
                echo "from the provided arguments."
        fi
        deletefile $?
        exit $rval
}
