#!/bin/sh

JAVA=@JAVA@

working_dir=@CWD@

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
fi

PNUMBER=$1
shift

CLASSNAME=$1
shift

cat > JavaStarter <<EOF
#!/bin/sh

working_dir=@CWD@

exec_prefix=@exec_prefix@

JAVA=@JAVA@

if [ -f ${working_dir}/src/adJava/adlib/libadlibjava.so ]
then
  CLASSPATH=.:$JAVA/class:$JAVA/lib/classes.zip:${working_dir}/src/adJava
  export CLASSPATH
  LD_LIBRARY_PATH=${working_dir}/src/adJava/adlib
  export LD_LIBRARY_PATH
else
  CLASSPATH=.:$JAVA/class:$JAVA/lib/classes.zip:${exec_prefix}/lib
  export CLASSPATH
  LD_LIBRARY_PATH=${exec_prefix}
  export LD_LIBRARY_PATH
fi

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

#java $CLASSNAME \$0 \$* # why stater ?

java $CLASSNAME $CLASSNAME \$*

EOF

chmod a+x JavaStarter

if [ $TESTMODE = 1 ]
then 
  if [ -f ${working_dir}/src/adJava/adlib/libadlibjava.so ]
  then
    ${working_dir}/src/script/prun -t $PNUMBER JavaStarter $*  
  else
    ${exec_prefix}/bin/prun -t $PNUMBER JavaStarter $* 
  fi
else
  if [ -f ${working_dir}/src/adJava/adlib/libadlibjava.so ]
  then
    ${working_dir}/src/script/prun $PNUMBER JavaStarter $*  
  else
    ${exec_prefix}/bin/prun $PNUMBER JavaStarter $* 
  fi
fi

rm -f JavaStarter
