#!/bin/bash
### BEGIN INIT INFO
# Provides: scp
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: scp
### END INIT INFO

##################################################################################
# #
# Usage: {start|stop|status|console} #
# - start: start the Server #
# - stop: stop the Server #
# - status: display the status of the Server (down or up) #
# - console: display the server console #
# To exit the console without stopping the server, press CTRL + A then D. #
# #
##################################################################################

SCREEN_NAME="scp"
USER="scp"
DIR_ROOT="/home/scp/scp_server1/"
SCP_RUNCMD="mono MultiAdmin.exe server1"
SCREEN_IGNORE_TTY="Y" # Y = Ignore the tty, you can access the Server screen as root (i not recommend it) root should be disabled :) | N = disabled

# No edits necessary beyond this line
PATH=/bin:/usr/bin:/sbin:/usr/sbin
if [ ! -x `which screen` ]; then echo "ERROR: You need screen for this script (try apt-get install screen)"; exit 1; fi
if [ ! -x `which tar` ]; then echo "WARNING: You need tar for the Backup Function (try apt-get install tar)";  fi

function start {
	if [ ! -d $DIR_ROOT ]; then echo "ERROR: $DIR_ROOT is not a directory"; exit 1; fi
	if status; then echo "$SCREEN_NAME is already running"; exit 1; fi

	# Start Server
	if [ `whoami` = root ]
	then
		su - $USER -c "cd $DIR_ROOT ; screen -AmdS $SCREEN_NAME $SCP_RUNCMD"
	else
		cd $DIR_ROOT
		screen -AmdS $SCREEN_NAME $SCP_RUNCMD
	fi
}

function stop {
	if ! status; then echo "$SCREEN_NAME could not be found. Probably not running."; exit 1; fi

	if [ `whoami` = root ]
	then
		su - $USER -c "screen -S $SCREEN_NAME -X stuff '\003'"
	else
		screen -S $SCREEN_NAME -X stuff '\003'
	fi
}

function status {
	if [ `whoami` = root ]
	then
		su - $USER -c "screen -ls" | grep [.]$SCREEN_NAME[[:space:]] > /dev/null
	else
		screen -ls | grep [.]$SCREEN_NAME[[:space:]] > /dev/null
	fi
}

function console {
	if ! status; then echo "$SCREEN_NAME could not be found. Probably not running."; exit 1; fi

	if [ `whoami` = root ]
	then
		
		if [ $SCREEN_IGNORE_TTY == 'Y' ]
		then 
		su - $USER -c "script -q -c 'screen -x $SCREEN_NAME' /dev/null"
		else 
		su - $USER -c "screen -x $SCREEN_NAME"
		fi
	else
		screen -x $SCREEN_NAME
	fi
}

function usage {
	echo "Usage: $0 {start|stop|status|console}"
	echo "On console, press CTRL+A then D to stop the screen without stopping the server."
}

case "$1" in

start)
    echo "Using following data:"
	echo "USER: $USER"
	echo "DIR ROOT: $DIR_ROOT"
	echo "BOT RUN CMD: $SCP_RUNCMD"
	echo ""
	sleep 2
	echo "Starting $SCREEN_NAME..."
	start
	sleep 2
	echo "$SCREEN_NAME started successfully"
;;

stop)
	echo "Stopping $SCREEN_NAME..."
	stop
	sleep 2
	echo "$SCREEN_NAME stopped successfully"
;;

status)
	if status
	then echo "$SCREEN_NAME is UP"
	else echo "$SCREEN_NAME is DOWN"
	fi
;;

console)
	echo "Open console on $SCREEN_NAME..."
	console
;;

*)
	usage
	exit 1
;;

esac

exit 0

