#!/bin/sh

JAVA=@JAVA@

working_dir=@CWD@

prefix=@prefix@
exec_prefix=@exec_prefix@

if [ $# -lt 1 ]
then
  echo "Usage: prunjava [-t] <procnum> <classname> <options>"
  exit 1
fi

if [ $1 = -t ] 
then 
  TESTMODE=1
  shift
else
  TESTMODE=0
fi

PNUMBER=$1
shift

CLASSNAME=$1
shift

if test -f $CLASSNAME.jig
then
  rm -f $CLASSNAME.jig
#  echo "There is a $CLASSNAME.jig in the current dir ..."
#  exit 1
fi

cat > $CLASSNAME.jig <<EOF
#!/bin/sh

working_dir=@CWD@

prefix=@prefix@
exec_prefix=@exec_prefix@

JAVA=@JAVA@

JAVA_COMPILER=sunwjit
#export JAVA_COMPILER

if test "$CLASSPATH" = ""
then
  if [ -f ${working_dir}/lib/libmpijava.so ]
  then
    CLASSPATH=.:${working_dir}/lib/classes
    export CLASSPATH
  else
    CLASSPATH=.:${exec_prefix}/lib
    export CLASSPATH
  fi
else
  if [ -f ${working_dir}/lib/libmpijava.so ]
  then
    CLASSPATH=.:${working_dir}/lib/classes:$CLASSPATH
    export CLASSPATH
  else
    CLASSPATH=.:${exec_prefix}/lib:$CLASSPATH
    export CLASSPATH
  fi
fi

if test "$LD_LIBRARY_PATH" = ""
then
  if [ -f ${working_dir}/lib/libmpijava.so ]
  then
    LD_LIBRARY_PATH=${working_dir}/lib
    export LD_LIBRARY_PATH
  else
    LD_LIBRARY_PATH=${exec_prefix}
    export LD_LIBRARY_PATH
  fi
else
  if [ -f ${working_dir}/lib/libmpijava.so ]
  then
    LD_LIBRARY_PATH=${working_dir}/lib:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH
  else
    LD_LIBRARY_PATH=${exec_prefix}:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH
  fi
fi


#echo \$0
cd \`dirname \$0\`

# exec $JAVA/bin/java @JAVATHREAD@ $CLASSNAME $CLASSNAME \$*
exec $JAVA/bin/java ${JAVAOPTION} $CLASSNAME $CLASSNAME \$*

EOF

chmod a+x $CLASSNAME.jig



if [ $TESTMODE = 1 ]
then 
  if [ -f ${working_dir}/lib/libmpijava.so ]
  then
    ${working_dir}/src/scripts/prun -t $PNUMBER $CLASSNAME.jig $*  
  else
    echo " ${working_dir}/lib/libmpijava.so not found"
  fi
else
  if [ -f ${working_dir}/lib/libmpijava.so ]
  then
    ${working_dir}/src/scripts/prun $PNUMBER $CLASSNAME.jig $*  
  else
    echo " ${working_dir}/lib/libmpijava.so not found"
  fi
fi

rm -f $CLASSNAME.jig

