#!/bin/ksh

# Startup script
# Peter Guntert, 13-01-1999

# ------ Display only name of executable? ------

mode=""
if [ $# -ge 1 ]; then
  if [ $1 = "-x" ]; then
    mode=x; shift 1
  fi
fi

# ------ Use MPI? ------

mpi=""
if [ $# -ge 1 ]; then
  if [ $1 = "-mpi" ]; then
    mpi=y; shift 1
  fi
fi

# ------ Explicit system type ------

sys=""
if [ $# -ge 2 ]; then
  if [ $1 = "-t" ]; then
    sys=$2; shift 2
  fi
fi

# ------ Treat command line arguments ------

if [ "$mpi" ]; then
  mpiopt="$*"
else
  mpiopt=
  ARGS="$*"
  export ARGS
fi

# ------ Set COFIMALIB ------ 

f=`csh -f -c "which $0"`
odir=`pwd`
while [ -L $f ]; do
  l=`ls -l $f | awk '{ i = NF; print $i }'`
  cd `dirname $f`; cd `dirname $l`; l=`pwd`/`basename $l`
  f=$l
done
cd `dirname $f`
COFIMALIB=`pwd | sed 's,/src/cofima$,,'`
cd $odir
export COFIMALIB

# ------ Start the program ------

exe=$COFIMALIB/src/cofima/ancomaexe
if [ "$sys" = "" ]; then
  sys=`$COFIMALIB/etc/identify`
  files="$exe.$sys* $exe.gnu $exe.*"
else
  files=$exe.$sys
fi
for exefile in $files; do
  if [ -x $exefile ]; then
    if [ $mode ]; then
      echo $exefile; exit 0
    else
      if [ "`echo $exefile | grep -c 'ancomaexe.*-mpi'`" -gt 0 ]; then
        ln -fs $exefile .
        exec ./`basename $exefile` $mpiopt
      else
        exec $exefile
      fi
    fi
  fi
done
echo "No executable found in \"$COFIMALIB/src/cofima\"."
exit 1
