#! /usr/bin/env bash

###################################################################################
#                                                                                 #
#                   Copyright 2010-2011 Ning, Inc.                                #
#                                                                                 #
#      Ning licenses this file to you under the Apache License, version 2.0       #
#      (the "License"); you may not use this file except in compliance with the   #
#      License.  You may obtain a copy of the License at:                         #
#                                                                                 #
#          http://www.apache.org/licenses/LICENSE-2.0                             #
#                                                                                 #
#      Unless required by applicable law or agreed to in writing, software        #
#      distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  #
#      WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the  #
#      License for the specific language governing permissions and limitations    #
#      under the License.                                                         #
#                                                                                 #
###################################################################################


HERE=`cd \`dirname $0\`; pwd`
TOP=$HERE/..
SERVER=$TOP/server

PROPERTIES="$SERVER/src/main/resources/killbill-server.properties"

DEBUG_OPTS_ECLIPSE=" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=12345 "
DEBUG_OPTS_ECLIPSE_WAIT=" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=12345 "

# Default JVM settings if unset
MAVEN_OPTS=${MAVEN_OPTS-"-Xms512m -Xmx1024m -XX:MaxPermSize=512m -XX:MaxDirectMemorySize=512m -XX:+UseConcMarkSweepGC"}

LOG="$SERVER/src/main/resources/logback.xml"

# From Argument Options
PORT=8080
START=
DEBUG=
WAIT_DEBUGGER=


function usage() {
    echo -n "./start-server "
    echo -n " -s (start server)"
    echo -n " -d (debugger turned on)"
    echo -n " -w (along with -d, wait for debugger before starting)"
    echo -n " -p <port_number> default 8080"
    echo -n "-h this message"
    exit 1
}

function build_properties() {
    local opts=
    local prop=
    for prop in `cat  $PROPERTIES | grep =`; do
        local k=`echo $prop | awk '  BEGIN {FS="="} { print $1 }'`
        local v=`echo $prop | awk 'BEGIN {FS="="} { print $2 }'`
        opts="$opts -D$k=$v"
    done
    echo $opts
}

function start() {
    local opts=`build_properties`
    local start_cmd="mvn $opts -Dlogback.configurationFile=$LOG -Dning.jmx.http.port=$PORT -Dxn.host.external.port=$PORT -DjettyPort=$PORT -Dxn.server.port=$PORT jetty:run"

    local debug_opts_eclipse=
    if [ ! -z $DEBUG ]; then
        if  [ ! -z $WAIT_DEBUGGER ]; then
            debug_opts_eclipse=$DEBUG_OPTS_ECLIPSE_WAIT
        else
            debug_opts_eclipse=$DEBUG_OPTS_ECLIPSE
        fi
    fi
    export MAVEN_OPTS="$MAVEN_OPTS -Duser.timezone=UTC $debug_opts_eclipse"

    echo "Starting IRS MAVEN_OPTS = $MAVEN_OPTS"
    echo "$start_cmd"
    cd $SERVER
    $start_cmd
}


while getopts ":pswdh" options; do
  case $options in
	s ) START=1;;
	d ) DEBUG=1;;
	w ) WAIT_DEBUGGER=1;;
	p ) PORT=$OPTARG;;
    h ) usage;;
    * ) usage;;
  esac
done

if [ ! -z $START ]; then
    start
else
    usage
fi
