#!/bin/bash
########################################################################
# MuGLIn - MuGLIn GNU/Linux Installation		                       #
#                                                                      #
# Copyright (C) 2010 Jakob Gurnhofer <jakob.gurnhofer@gmail.com>       #
# Copyricht (C) 2010 Srdjan Markovic <smark2ki@htl.moedling.at>        #
#                                                                      #
# This file is part of MuGLIn source code.                             #
#                                                                      #
# MuGLIn is free software: you can redistribute it and/or modify       #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# MuGLIn is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with MuGLIn. If not, see <http://www.gnu.org/licenses/>.       #
########################################################################

if [ -e /etc/muglin/services.conf ]; then
	. /etc/muglin/services.conf
fi
. /usr/local/lib/muglin/muglin.d/libmuglin

if [ "$SERVICES" = "" ];then
	echo "Missing SERVICES= in /etc/muglin/services.conf"
	exit -2
fi
if [ ! -d "$SERVICE_DIR" ]; then
	echo "\"$SERVICE_DIR\" is not a directory or \"SERVICE_DIR\" is missing in /etc/muglin/services.conf"
	exit -2
fi


# i've implemented it this way because i couldn't get case to handle $SERVICES,
# if you have a better idea, please do it, this looks awful :(
while [ "$1" != "" ]; do
	if [ "$1" = "-start" ]; then
			setcmd start
			continue
	elif [ "$1" = "-h" ]; then
		echo "Manages services needed by MuGLIn
Those are: \"$(echo $SERVICES | sed 's/|/" "/g')\" \"all\".
\"all\" invokes all of those.
Available options:
-start		Starts services
-stop		Stops services
-status		Show status of services
-restart	Restarts services
"
		exit -1
	elif [ "$1" = "-stop" ]; then
			setcmd stop
			continue
	elif [ "$1" = "-status" ]; then
			setcmd status
			continue
	elif [ "$1" = "-restart" ]; then
			setcmd restart
			continue
	elif [ "$1" = "-php" ]; then
			PHP=1
			continue
	elif [ "$1" = "all" ]; then
			SERVICE=all
			continue
	elif [ "`echo $SERVICES | grep $1`" != "" ]; then
			SERVICE=$1
			continue
	else	
			echo "Unknown service or option $1. Aborting"
			exit -1;
	fi
	shift
done

if [ "$SERVICE" = "" -o "$CMD" = "" ]; then
	echo "You must provide <Service> and <CMD>"
	exit -1
fi

execact(){
if [ -x $SERVICE_DIR/$SERVICE ]; then
	case $PHP in
		1)
			$SERVICE_DIR/$SERVICE $CMD > /dev/null 2>&1
			ret=$?
			if [ "$CMD" = "status" ]; then
				echo -n $ret
			fi
		;;
		*)
			$SERVICE_DIR/$SERVICE $CMD
		;;
	esac	
fi
}

if [ "$SERVICE" = "all" ]; then
	for SRV in `echo $SERVICES | sed 's/|/ /g'`; do
		SERVICE=$SRV execact
	done
else
	execact
fi
