#! /bin/sh


# -------------------------------------------------------------------
# set default values for DUPHOST and NSCM if they are not yet defined
# -------------------------------------------------------------------

if test -z "$DUPHOST"
then
    DUPHOST=-
fi

if test -z "$NSCM"
then
    NSCM=-
fi


# ----------------------
# parse the command line
# ----------------------

set -- ` getopt dn: $*`
if [ $? != 0 ] 
then
    exit 2
fi
while [ $1 != -- ]
do
    case $1 in
    -d)
        DUPHOST=TRUE
        shift;
        ;;
    -n)
        NSCM=$2
        shift;
        shift;
        ;;
     esac
done
shift
export NSCM
export DUPHOST


# ------------------------------------------
# make sure ADFHOME and ADFBIN have been set
# ------------------------------------------

if test -z "$ADFHOME"
then
    echo You need to set ADFHOME in your environment
    exit 1
fi

if test -z "$ADFBIN"
then
    echo You need to set ADFBIN in your environment
    exit 1
fi


# -----------------------------
# pick up the command arguments
# -----------------------------


# --------------------------------
# first argument: program to start
# --------------------------------

if test -z "$1"
then
    echo "You need to specify at least the program to run"
    echo 
    echo "Usage: start [-n nproc] [-d] prog [script]"
    echo "       nproc : number of processes to use \(for a parallel run\)"
    echo "               default: $NSCM if defined, otherwise"
    echo "               number of CPU's available according to the nodeinfo file"
    echo "       -d    : allow more than one process per CPU \(duphost\)"
    echo "               default: do not allow more than one process per CPU"
    echo "       prog  : name of executable to start"
    echo "       script: name of script to start child processes"
    echo 
    echo "Options specified by the start command line override the options specified "
    echo "using the PARALLEL input key."
    echo "If prog or script are not executables, \$ADFBIN/prog and/or \$ADFBIN/script"
    echo "are used if they exist and are executable."
    echo
    echo "Examples: start adf <in >out"
    echo "          start -n 8 adf <in >out"
    echo "          start band keepouput <in"
    exit 1
fi

# ------------------------
# get first argument: prog
# ------------------------

if test -x "$1"
then
    PROG=$1
    export PROG
else
    if test -x "$ADFBIN/$1"
    then
        PROG=$ADFBIN/$1
        export PROG
    else
        echo "Could not find executable $1 or $ADFBIN/$1 (PROG)"
        exit 1
    fi
fi


# ---------------------------------------------
# seconde argument: SPAWNRC execuatble (script)
# ---------------------------------------------

if test -z "$2"
then
    SPAWNRC=
    export SPAWNRC
else
    if test -x "$2"
    then
        SPAWNRC=$2
        export SPAWNRC
    else
        if test -x "$ADFBIN/$2"
        then
            SPAWNRC=$ADFBIN/$2
            export SPAWNRC
        else
            echo "Could not find executable $2 or $ADFBIN/$2 (SPAWNRC)"
            exit 1
        fi
    fi
fi


# --------------------------------------
# define variables for parallel PVM runs
# --------------------------------------

TEMPDIR=/tmp
export TEMPDIR

MHOST=`hostname`
export MHOST

WDIR=`pwd`
export WDIR

pvmgroup=SCM_$$
export pvmgroup

PVM_DATA_MAX=6112
export PVM_DATA_MAX

PVM_SM_POOL=512
export PVM_SM_POOL

PVM_EXPORT=PATH:TEMPDIR:PROG:SPAWNRC:PVM_ROOT:PVM_ARCH:MHOST:WDIR:pvmgroup
export PVM_EXPORT


# -----------------------------------------------------------------------------------
# dump current environment to the environment file to make it accessable from fortran
# -----------------------------------------------------------------------------------

printenv | cut -c 1-80 > environment


# ---------------------------------------------
# copy the nodeinfo file to the local directory
# ---------------------------------------------
 
if test ! -f nodeinfo
then
    if test -f $ADFBIN/nodeinfo
    then
        cp -f $ADFBIN/nodeinfo nodeinfo
    fi
fi


# -----------------
# start the program
# -----------------

# SCM : $PROG
# Cerius2 : $MSTART $PROG

if test -x $MSTART
then
   $MSTART $PROG
else
   $PROG
fi


# --------------------------------------------------
# move all *_0 files to * (thus TAPE21_0 --> TAPE21)
# --------------------------------------------------

for f in *_0
do
    g=`basename $f _0`
    if test -f $g 
    then
        echo "WARNING: $f not moved because $g already exists"
    else
        mv $f `basename $f _0`
    fi
done
