#! /bin/bash
#
### BEGIN INIT INFO
# Provides:             vgemu-world
# Required-Start:       $network $local_fs $remote_fs dbus
# Required-Stop:	$network $local_fs $remote_fs dbus
# Should-Start:		$mysql
# Should-Stop:		$mysql
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    VGOEmulator World Server
### END INIT INFO

DAEMONDIR=/vgworld
NAME=vgemu-world
DAEMON=$DAEMONDIR/$NAME
DESC="VGOEmulator World Server"
PIDDIR=/var/run/$NAME
PIDFILE=$PIDDIR/$NAME.pid
# LOGSDIR=/var/log/$NAME
# LOGSFILE=$LOGSDIR/worldserver.log
USER=root
GROUP=root
VGINITVERSION=2
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

red='\e[1;31m'
NC='\e[0m'

. /lib/init/vars.sh
. /lib/lsb/init-functions

###########################################################
# Check if all needed directories exist otherwise create it !
###########################################################
if [ ! -d "$PIDDIR" ]; then
	echo "Create the PID directory"
	mkdir -p $PIDDIR
fi

#if [ ! -d "$LOGSDIR" ]; then
#	echo "Create the LOGS directory"
#        mkdir -p $LOGSDIR
#fi
###########################################################
# Check if required files are in the binary directory
###########################################################
myvgoemutest='0'

if [ ! -e "$DAEMON" ]; then
        echo -e "${red}Error, The world server daemon is not in the directory or you need to configure the DAEMONDIR var in this init script, exit${NC}"
	myvgoemutest='1'
fi

if [ ! -e "$DAEMONDIR/vgemu-structs.xml" ]; then
        echo -e "${red}Error, The file vgemu-structs.xml is not in the daemon directory, exit${NC}"
	myvgoemutest='1'
fi

if [ ! -e "$DAEMONDIR/vgemu-world.xml" ]; then
        echo -e "${red}Error, The file vgemu-world.xml is not in the daemon directory, exit${NC}"
	myvgoemutest='1'
fi

if [ ! -e "$DAEMONDIR/generic.lua" ]; then
        echo -e "${red}Error, The file generic.lua is not in the daemon directory, exit${NC}"
	myvgoemutest='1'
fi

if [ "$myvgoemutest" == '1' ]; then
	exit 0
fi

if [ ! -x "$DAEMON" ]; then
        chmod ug+x $DAEMON
fi
###########################################################

case "$1" in
	start)
		log_daemon_msg "Starting $DESC" "$NAME"
		start-stop-daemon --start --make-pidfile --quiet --oknodo \
			--pidfile "$PIDFILE" \
			--chuid "$USER":"$GROUP" \
			--chdir "$DAEMONDIR" \
			--name "$NAME" \
			--exec "$DAEMON" \
			--background
		case "$?" in
			0|1)
				log_end_msg 0 ;;
			2)
				log_end_msg 1 ;;
		esac
		;;
	stop)
		log_daemon_msg "Stopping $DESC" "$NAME"
		start-stop-daemon --stop --quiet --oknodo \
			--retry=TERM/30/KILL/5 \
			--pidfile "$PIDFILE" \
			--chuid "$USER":"$GROUP" \
			--user "$USER" \
			--name "$NAME" \
			--exec "$DAEMON"
		case "$?" in
			0|1)
				rm -f $PIDFILE
				log_end_msg 0
				;;
			2)
				log_end_msg 1 ;;
		esac
		;;
	status)
		if start-stop-daemon --test --stop --quiet \
			--pidfile "$PIDFILE" \
			--chuid "$USER":"$GROUP" \
			--user "$USER" \
			--name "$NAME" \
			--exec "$DAEMON"
		then
			echo "$DESC is running."
			exit 0
		else
			echo "$DESC is not running"
			exit 3
		fi
		;;
	restart)
		log_daemon_msg "Restarting $DESC" "$NAME"
                start-stop-daemon --stop --quiet --oknodo \
                        --retry=TERM/30/KILL/5 \
                        --pidfile "$PIDFILE" \
			--chuid "$USER":"$GROUP" \
                        --user "$USER" \
                        --name "$NAME" \
                        --exec "$DAEMON"
		case "$?" in
			0|1)
				rm -f $PIDFILE
				start-stop-daemon --start --make-pidfile --quiet --oknodo \
					--pidfile "$PIDFILE" \
					--chuid "$USER":"$GROUP" \
					--chdir "$DAEMONDIR" \
					--name "$NAME" \
					--exec "$DAEMON" \
					--background
				case "$?" in
					0) log_end_msg 0 ;;
					*) log_end_msg 1 ;;
				esac
				;;
				
			*)
				log_end_msg 0 ;;
		esac
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart}" >&2
		;;

esac
exit 0
