#!/sbin/openrc-run

depend() { 
	need net 
	after mysql postgresql sshd
}

checkconfig() {
	if [ ! -f /etc/kannel/kannel.conf ] ; then
		eerror "/etc/kannel/kannel.conf file doesn't exists!"
		return 1
	fi
	#set the location of logs
	if ! cd /var/log/kannel ; then
		eerror "/var/log/kannel directory doesn't exists!"
		return 1
	fi
}

start() {
	checkconfig || return 1

	einfo "Starting Kannel"
	ebegin " - Bearer Box"
	start-stop-daemon --start --quiet --chuid kannel --exec /usr/sbin/bearerbox \
		--background -- $BEARERBOX_OPTS /etc/kannel/kannel.conf
	eend $? || return 1

	if [ "$START_SMSBOX" = "yes" ] ; then
		ebegin " - SMS Box" 
		start-stop-daemon --start --quiet --chuid kannel --exec /usr/sbin/smsbox \
			--background -- $SMSBOX_OPTS /etc/kannel/kannel.conf
		eend $?
	fi

	if [ "$START_WAPBOX" = "yes" ] ; then
		ebegin " - WAP Box" 
		start-stop-daemon --start --quiet --chuid kannel --exec /usr/sbin/wapbox \
			--background -- $WAPBOX_OPTS /etc/kannel/kannel.conf
		eend $?
	fi

	return 0
}

stop() {
	if [ "$START_WAPBOX" = "yes" ] ; then
		einfo "Stopping Kannel" 
		ebegin " - WAP Box"
		start-stop-daemon --stop --quiet --exec /usr/sbin/wapbox
		eend $?
	fi

	if [ "$START_SMSBOX" = "yes" ] ; then
		ebegin " - SMS Box"
		start-stop-daemon --stop --quiet --exec /usr/sbin/smsbox
		eend $?
	fi

	ebegin " - Bearer Box" 
	start-stop-daemon --stop --quiet --exec /usr/sbin/bearerbox
	eend $?
	
	#stopping any other processes owned by kannel user
	start-stop-daemon --stop --quiet --user kannel
	
	return 0
}
