# agent-svr
#  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
#
#  Contact:
#  DongHee Yang <donghee.yang@samsung.com>
#  Sungmin Kim <sm.art.kim@samsung.com>
#  Jiil Hyoun <jiil.hyoun@samsung.com>
#  Jonghwan Park <iwin100.park@samsung.com>
#  Kitae Kim <kt920.kim@samsung.com>
#
#  Licensed 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.
#
#  Contributors:
#  - S-Core Co., Ltd

#!/bin/bash

BASEDIR=$(cd "$(dirname "$0")"; pwd)
AGENTAPP=""

# check if agent is exists
if [ -e ${BASEDIR}/agent.js ]; then
    AGENTAPP=${BASEDIR}/agent.js
elif [ -e ${BASEDIR}/../plugins/dibs.server.agent/bin/agent.js ]; then
    AGENTAPP=${BASEDIR}/../plugins/dibs.server.agent/bin/agent.js
else
    echo "Can not find agent server module(agent.js)"
    exit 1
fi

# check if node is installed
if !(which node > /dev/null); then
    echo "Can not find node."
    exit 1
fi

# switch case for user input
# validate subcommand
SUBCOMMAND=""
case "$1" in
    start)
        SUBCOMMAND="start"
        ;;
    stop)
        SUBCOMMAND="stop"
        ;;
    *)
        echo "Usage: $0 {start|stop} -i <serverId> -p <port>"
        exit 1
esac

# validate options
ID=""
PORT=""
while test $# -gt 1
do
    case "$2" in
        -i)
            ID=${3}
            if [ "${ID}" = "" ] || [ "${ID:0:0}" = "-" ]; then
                echo "invalid -i value"
                echo "Usage: $0 {start|stop} -i <serverId> -p <port>"
                exit 1
            fi
            ;;
        -p)
            PORT=${3}
            if [ "${PORT}" = "" ] || [ "${PORT:0:0}" = "-" ]; then
                echo "invalid -p value"
                echo "Usage: $0 {start|stop} -i <serverId> -p <port>"
                exit 1
            fi
            ;;
    esac
    shift
done

# exec agent server
if [ "${ID}" = "" ]; then
    echo "Usage: $0 {start|stop} -i <serverId> -p <port>"
    exit 1
elif [ ${SUBCOMMAND} = "start" ] && [ "${PORT}" != "" ]; then
    exec node --stack-size=1024 ${AGENTAPP} start -i ${ID} -p ${PORT} > ${BASEDIR}/agent.log 2>&1 &
    echo "Start Agent Server( ${ID} )"
    echo "log file: ${BASEDIR}/agent.log"
    exit 0
elif [ ${SUBCOMMAND} = "start" ] && [ "${PORT}" = "" ]; then
    exec node --stack-size=1024 ${AGENTAPP} start -i ${ID} > ${BASEDIR}/agent.log 2>&1 &
    echo "Start Agent Server( ${ID} )"
    echo "log file: ${BASEDIR}/agent.log"
    exit 0
elif [ ${SUBCOMMAND} = "stop" ]; then
    echo "Stop Agent Server( ${ID} )"
    exec node ${AGENTAPP} stop -i ${ID}
    exit 0
else
    echo "Usage: $0 {start|stop} -i <serverId> -p <port>"
    exit 1
fi
