#!/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.Jigniter
then
  echo "There is a $CLASSNAME.Jigniter in the current dir ..."
  exit 1
fi

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

working_dir=@CWD@

prefix=@prefix@
exec_prefix=@exec_prefix@

JAVA=@JAVA@

if test "$CLASSPATH" = ""
then
  if [ -f ${working_dir}/src/adJava/libspmdjava.so ]
  then
    CLASSPATH=.:${working_dir}/src/adJava/class
    export CLASSPATH
    LD_LIBRARY_PATH=${working_dir}/src/adJava
    export LD_LIBRARY_PATH
  else
    CLASSPATH=.:${exec_prefix}/lib
    export CLASSPATH
    LD_LIBRARY_PATH=${exec_prefix}
    export LD_LIBRARY_PATH
  fi
else
  if [ -f ${working_dir}/src/adJava/libspmdjava.so ]
  then
    CLASSPATH=.:${working_dir}/src/adJava/class:$CLASSPATH
    export CLASSPATH
    LD_LIBRARY_PATH=${working_dir}/src/adJava
    export LD_LIBRARY_PATH
  else
    CLASSPATH=.:${exec_prefix}/lib:$CLASSPATH
    export CLASSPATH
    LD_LIBRARY_PATH=${exec_prefix}
    export LD_LIBRARY_PATH
  fi
fi

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

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

java $CLASSNAME $CLASSNAME \$*

EOF

chmod a+x $CLASSNAME.Jigniter

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

rm -f $CLASSNAME.Jigniter
