#!/bin/sh
DIRNAME=`dirname $0`
POJO_HOME=`cd $DIRNAME/..; pwd`

if [ $# -lt 1 ]; then
   echo Utility which executes the JVM with properties needed by POJO Cache for load-time instrumentation. 
   echo 
   echo Usage:
   echo "pojo-run [-classpath <classpath>] <main-class> [arguments...] "
   echo "   classpath:        Classpath of your sourcefiles and all required libraries"
   echo "   main-class:       Normal Java main class"
   echo 
   echo Example:
   echo "   pojo-run -classpath myclasses org.foo.Main"
   exit 1
fi

cygwin=false;
case "`uname`" in
    CYGWIN*)
        cygwin=true
        ;;
esac

AOP_FILE="pojocache-aop.xml"
AOP_PATH="$POJO_HOME/$AOP_FILE"

if [ ! -f "$AOP_PATH" ]; then
   AOP_PATH="$POJO_HOME/etc/META-INF/$AOP_FILE"

   if [ ! -f "$AOP_PATH" ]; then
      echo "Could not locate pojocache-aop.xml in $POJO_HOME or $POJO_HOME/etc/META-INF"
      exit 1
   fi
fi

POJO_CLASSPATH=.:$POJO_HOME/jbosscache-pojo.jar

for i in $POJO_HOME/lib/*.jar
do
    POJO_CLASSPATH="$POJO_CLASSPATH:${i}"
done

while [ $# -ge 1 ]; do
   case $1 in
       "-classpath") POJO_CLASSPATH="$POJO_CLASSPATH:$2"; shift;;
       *) args="$args \"$1\""; echo $1;;
   esac
   shift
done


#Check for cygwin and convert path if necessary
if $cygwin; then
   POJO_CLASSPATH=`cygpath --path --windows $POJO_CLASSPATH`
fi

# Setup the JVM
if [ "x$JAVA" = "x" ]; then
    if [ "x$JAVA_HOME" != "x" ]; then
	JAVA="$JAVA_HOME/bin/java"
    else
	JAVA="java"
    fi
fi

#JPDA options. Uncomment and modify as appropriate to enable remote debugging .
#JAVA_OPTS="-classic -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n $JAVA_OPTS"

# Execute the JVM
eval "$JAVA" $JAVA_OPTS \
   -javaagent:"$POJO_HOME/lib/jboss-aop.jar" \
   -Djboss.aop.path="$AOP_PATH" \
   -classpath "$POJO_CLASSPATH" \
   "$args"
