#!/bin/bash

BASE_DIR="$(cd "$(dirname "$0")"; pwd)";

CNT=$#
if [ $CNT = 1 ] ; then
    sed -i -e "s|MANAGER_URL=.*|MANAGER_URL=$1|g" $BASE_DIR/env.sh
    rm -f $BASE_DIR/env.sh-e
fi

. $BASE_DIR/env.sh

PID=`ps -ef | grep java | grep wasup.websocket.endpoint | grep "$AGENT_PROCESS_NAME" | awk '{print $2}'`

if [ e$PID != "e" ] ; then
    echo "$AGENT_PROCESS_NAME is already RUNNING..."
    exit;
fi

if [ $JAVA_HOME ] ; then
    EXE_JAVA=$JAVA_HOME/bin/java
else
    EXE_JAVA=java
fi

echo $JAVA_OPTS

#SUDO=`sudo -n echo 1`
#if [ "$SUDO" = "1" ] ; then
#    EXE_JAVA="sudo $EXE_JAVA"
#fi

# Change directory to create host.id file in BASE_DIR
cd $BASE_DIR

nohup $EXE_JAVA $JAVA_OPTS -jar $BASE_DIR/../lib/wasup-agent.jar > /dev/null 2>&1 &

sleep 1
tail -100 $LOG_FILE
