#!/bin/bash
#
#
# 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.
#
#
# traffic ops	Start up of the traffic ops application.
#
# chkconfig: 2345 55 25
# description: Traffic Ops is the administrative web UI for IPCDN.
#
# pidfile:
#

### BEGIN INIT INFO
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the Traffic Ops Application.
# Description: Traffic Ops is the administrative UI for the CDN.
#
### END INIT INFO

TO_DIR="/opt/traffic_ops/app"; export TO_DIR

# uncomment this for riak client debug logging to the defined info log location.
# logs to stdout if the info log location is not defined in the cdn.conf
#RIAK_GO_CLIENT_DEBUG="true"; export RIAK_GO_CLIENT_DEBUG

# source function library
. /etc/rc.d/init.d/functions

start ()
{
	stop
	echo -e "Starting Traffic Ops\n"
	ulimit -n 200000 || echo "Setting ulimit max files failed for traffic_ops_golang"
	cd $TO_DIR && $TO_DIR/bin/traffic_ops_golang -cfg $TO_DIR/conf/cdn.conf -dbcfg $TO_DIR/conf/production/database.conf -riakcfg $TO_DIR/conf/production/riak.conf -backendcfg $TO_DIR/conf/production/backends.conf &
}

stop ()
{
	killproc $TO_DIR/bin/traffic_ops_golang
}

restart ()
{
	stop
	start
}

status ()
{
	PID=`pidof -s /opt/traffic_ops/app/bin/traffic_ops_golang`
	if [ -z "$PID" ] ; then
		echo "Traffic Ops is offline" >&2
		return 1
	fi
	echo "Traffic Ops is running, pid = $PID"
	return 0
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	status)
		status
		;;
	*)
		echo "Usage: $0 {start | stop | restart | status}"
		;;
esac

exit 0
