#!/bin/bash

# check if everything is correct
function check() {
	if [ -x "$RCDIR/$SERVICE" ]; then
		case $PARAM in
			start|START)     PARAM="start" && rcrun;;
			stop|STOP)       PARAM="stop" && rcrun;;
			restart|RESTART) PARAM="restart" && rcrun;;
			reload|RELOAD)   PARAM="reload" && rcrun;;
			status|STATUS)   PARAM="status" && rcrun;;
			*) echo "ERROR: Parameter $PARAM not supported!"
		esac
	else
		echo "ERROR $RCDIR/$SERVICE NOT found - abort" >&2
	fi	
}

# starts/stops/restarts... the service
function rcrun() {
	$RCDIR/$SERVICE $PARAM
	retc=$?
	if [ $retc -ne 0 ] && [ $retc -ne 3 ]; then
		( echo "ERROR: $RCDIR/$SERVICE $PARAM failed"
		  echo "Run: /usr/sbin/kolabsrv rc all stop"
		  echo "to stop all services"
		) >&2
		exit 1
	fi
}	

# map distribution specific rc script names
function getServiceName() {
	sname=$1
	# Perhaps better use:
	# lsb_release -i | cut -f2
	if [ -e /etc/SuSE-release ]; then
		case $sname in
			openldap) SERVICE="ldap" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="cyrus" ;;
			amavisd) SERVICE="amavis" ;;
			clamav) SERVICE="clamd" ;;
			*) SERVICE=$sname
		esac
	elif [ -e /etc/debian_version ]; then
		case $sname in
			openldap) SERVICE="ldap" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="kolab-cyrus" ;;
			amavisd) SERVICE="amavis" ;;
			clamav) SERVICE="clamav-daemon" ;;
			spamd) SERVICE="spamassassin" ;;
			freshclam) SERVICE="clamav-freshclam" ;;
			*) SERVICE=$sname
		esac
	elif [ -e /etc/mandriva-release ]; then
		case $sname in
			apache2) SERVICE="httpd" ;;
			openldap) SERVICE="ldap" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="cyrus-imapd" ;;
			amavisd) SERVICE="amavisd" ;;
			clamav) SERVICE="clamd" ;;
			spamd) SERVICE="spamd" ;;
			freshclam) SERVICE="freshclam" ;;
			*) SERVICE=$sname
		esac
	fi
}	

# variables
RC=$1
SERVICE=$2
PARAM=$3

# Use the service name as defined in the openpkg environment
# Kolab installations on native distributions, should adap service names
# with the function getServiceName
SERVICES="ldap saslauthd spamd amavisd apache2 freshclam imapd postfix"
SERVICES="$SERVICES clamav kolabd"

case $RC in 
rc)
	# okay
	;;
*)
	SERVICES=$(echo $SERVICES | tr -s " " | sed 's, ,|,'g)

	( echo "ERROR: $RC not supported"
	  echo "Use: /usr/sbin/kolabsrv rc [all|$SERVICES] [stop|start|restart|status]"
	) >&2

	exit 1
	;;
esac

if [[ "$SERVICE" != "all" ]]; then

	SERVICE_FOUND=no

	for S in $SERVICES; do
		if [[ $S == "$SERVICE" ]]; then
			SERVICE_FOUND=yes
			SERVICES=$SERVICE
			break
		fi
	done

	if [[ $SERVICE_FOUND == "no" ]]; then
		( echo "ERROR: unknown service: $SERVICE"
		  echo "Use: /usr/sbin/kolabsrv rc [all|$SERVICES] [stop|start|restart|status]"
		) >&2

		exit 1
	fi
else
	if [[ "$PARAM" == "stop" ]]; then
		SERVICES=$(tr " " "\n" <<< "$SERVICES" | tac)
	fi
fi

[[ -f /etc/kolab/profile.sh ]] && . /etc/kolab/profile.sh

RCDIR=/etc/rc.d/init.d

for SERVICE in $SERVICES; do
	# Only start the virus scanning dependent services in case
	# virus scannning has been enabled on the kolab web admin interface.
	# An service is always stopped independent of the virus service	
	# scanning setting.
	if [[ "$PARAM" == "start" ]] || [[ "$PARAM" == "restart" ]]; then
		case $SERVICE in
		amavisd|clamav|freshclam|spamd)
			if [[ "$ENABLE_VIRUS_SCAN" == "no" ]]; then
				RUN_SERVICE=no
			else
				RUN_SERVICE=yes
			fi
			;;
		*)
			RUN_SERVICE=yes
			;;
		esac
	else
		RUN_SERVICE=yes
	fi

	[[ $RUN_SERVICE == "yes" ]] && {
		getServiceName $SERVICE
		check $SERVICE
	}
done

