#!/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 [ -f ${working_dir}/lib/libmpijava.so ]
then
  if test "$CLASSPATH" = ""
  then
      CLASSPATH=.:${working_dir}/lib/classes
  else
      CLASSPATH=.:${working_dir}/lib/classes:$CLASSPATH
  fi
  if test "$LD_LIBRARY_PATH" = ""
  then
      LD_LIBRARY_PATH=${working_dir}/lib
  else
      LD_LIBRARY_PATH=${working_dir}/lib:$LD_LIBRARY_PATH
  fi
else
  if test "$CLASSPATH" = ""
  then
      CLASSPATH=.:${exec_prefix}/lib
  else
      CLASSPATH=.:${exec_prefix}/lib:$CLASSPATH
  fi
  if test "$LD_LIBRARY_PATH" = ""
  then
      LD_LIBRARY_PATH=${exec_prefix}
  else
      LD_LIBRARY_PATH=${exec_prefix}:$LD_LIBRARY_PATH
  fi
fi
export CLASSPATH
export LD_LIBRARY_PATH

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

exec $JAVA/bin/java @JAVATHREAD@ $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

