#!/bin/sh

########################################################## 
#               Gridworks Control System                 #
##########################################################

fail() {
    cat <<EOF
ERROR: $1
Usage: $0 [options] <action>
  -h for more details
EOF
    exit 1
}

usage() {
    cat <<EOF
Usage: $0 [options] <action>
where [options] include:

  -h print this message and exit
  
  -p <port> the port that gridworks will listen to
     default: 3333

  -i <interface> the host interface gridworks should bind to
     default: 127.0.0.1

  -w <path> path to the webapp
     default: src/main/webapp
     
  -d enable JVM debugging (on port 8000)
       
  -x enable JMX monitoring (for jconsole)
  
and <action> is one of

   build ..... Build Gridworks      
   run ....... Run Gridworks
                
EOF
    exit 0
}

add_option() {
  OPTS="$OPTS $1"
}

OPTS="$GRIDWORKS_OPTS"

# ----- actions -------------------------------------------------

build() {
    if [ ! -d $GRIDWORKS_BUILD_DIR ] ; then 
        mkdir $GRIDWORKS_BUILD_DIR || exit 1
    fi
    
    ANT=`which ant`
    if [ "$ANT" = "" ] ; then
        ANT_TAR=`ls thirdparty | grep apache-ant`
        ANT_DIR="$GRIDWORKS_BUILD_DIR/ant"
        ANT="$ANT_DIR/bin/ant"
        if [ ! -d $ANT_DIR ] ; then
            tar xzf $ANT_TAR -C $BUILD_DIR || exit 1
        fi
    fi
    
    $ANT -f build.xml compile || exit 1
}

run() {
    if [ ! -d $GRIDWORKS_BUILD_DIR/classes ] ; then
        build
        echo ""
    fi
    
    CLASSPATH="$GRIDWORKS_BUILD_DIR/classes:$GRIDWORKS_LIB_DIR/*"

    RUN_CMD="$JAVA -cp $CLASSPATH $OPTS com.metaweb.gridworks.Gridworks"
  
    echo "Starting Gridworks at 'http://${GRIDWORKS_HOST}:${GRIDWORKS_PORT}/'"
    echo ""
    #echo "$RUN_CMD"
    #echo ""

    exec $RUN_CMD
}
    
# ----- We called without arguments print the usage -------------

[ $# -gt 0 ] || usage

# ----- Normalize the current directory -------------------------

cd `dirname $0`

# ----- Make sure there is a java environment installed -------------------------
    
if [ ! -z "$JAVA_HOME" ] ; then
    JAVA="$JAVA_HOME/bin/java"
else 
    JAVA=`which java`
    if [ -z "$JAVA" ] ; then
        system=`uname`
        
        if [ "$system" = 'Darwin' ] ; then
            JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
        else
            echo "The 'java' command should be in your path or the 'JAVA_HOME' environment variable should be set"
            exit 1
        fi
    fi
fi

JAVA_VERSION=`java -version 2>&1 | grep version | cut -d ' ' -f 3 | egrep ^\"1.6`
if [ -z "$JAVA_VERSION" ] ; then
    echo "Gridworks requires java version 6 or later."
    exit 1
fi

# ----- Parse the command line args ------------------------------------------

while [ $# -ne 0 ] ; do
  case "$1" in
    -p) shift; GRIDWORKS_PORT="$1"; shift; continue;;
    -i) shift; GRIDWORKS_HOST="$1"; shift; continue;;
    -w) shift; GRIDWORKS_WEBAPP="$1"; shift; continue;;
    -d) shift; add_option '-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n'; continue;;
    -x) shift; add_option '-Dcom.sun.management.jmxremote'; continue;;
    -*) fail "Invalid option: $1";;
    *) break;;
  esac
done

ACTION=$1; shift

# ----- Verify and Set Required Environment Variables -------------------------

if [ "$JAVA_OPTIONS" == "" ] ; then
  JAVA_OPTIONS="-Xms256M -Xmx1024M"
fi
add_option "$JAVA_OPTIONS"

if [ "$GRIDWORKS_PORT" == "" ] ; then
    GRIDWORKS_PORT="3333"
fi
add_option "-Dgridworks.port=$GRIDWORKS_PORT"

if [ "$GRIDWORKS_HOST" == "" ] ; then
    GRIDWORKS_HOST="127.0.0.1"
fi
add_option "-Dgridworks.host=$GRIDWORKS_HOST"

if [ "$GRIDWORKS_WEBAPP" == "" ] ; then
    GRIDWORKS_WEBAPP="src/main/webapp"
fi
add_option "-Dgridworks.webapp=$GRIDWORKS_WEBAPP"

if [ "$GRIDWORKS_BUILD_DIR" == "" ] ; then
    GRIDWORKS_BUILD_DIR="build"
fi

if [ "$GRIDWORKS_LIB_DIR" == "" ] ; then
    GRIDWORKS_LIB_DIR="lib"
fi


# ----- Respond to the action given --------------------------------------------

case "$ACTION" in
  build)
    build;;

  run) 
    run;;  
    
  *)
    usage; ;;
esac