#!/bin/sh
#
# gammu-smsd		Start/stop the Gammu SMS Daemon.
#
# chkconfig:	2345 89 11
#
# description:	Gammu SMS Daemon receives and sends SMS through \
#               a GSM modem
#

# Get service config
[ -f /etc/sysconfig/gammu-smsd ] && . /etc/sysconfig/gammu-smsd

CONFIG_SMSD_INSTANCES=$SMSD_INSTANCES

[ -n "$2" ] && SMSD_INSTANCES="$2"

# no configured instances. exit silently
if [ -z "$SMSD_INSTANCES" ]; then
   	case "$1" in
	start|stop|restart|reload|force-reload)
		exit 0
		;;
	esac
fi

# Source function library
. /etc/rc.d/init.d/functions


# check if SMSD instance $1 s up
instanceup() {
	local instance="$1"
	local pidfile=/var/run/gammu-smsd/$instance.pid
	local pid=$(cat $pidfile 2>/dev/null)
	kill -0 $pid 2>/dev/null
	return $?
}

# check if all of the configured SMSD instances are up
instancesup() {
	ret=0
	for instance in $CONFIG_SMSD_INSTANCES; do
		instanceup $instance && continue
		ret=1
	done
	return $ret
}

# check if any of the configured SMSD instances is up
anyinstancesup() {
	ret=1
	for instance in $CONFIG_SMSD_INSTANCES; do
		instanceup $instance && return 0
	done
	return $ret
}

start() {
	# Check if the service is already running?
	if ! instancesup; then
		msg_starting "Gammu SMSD"; started
		for instance in $SMSD_INSTANCES; do
			config="/etc/gammu-smsd/$instance.conf"
			if [ ! -f "$config" ]; then
				nls "Invalid Gammu SMSD instance \`%s': missing config: %s" "$instance" "$config"
				fail
				RET=1
			else	
				GAMMU_SMSD_OPTS=""
				show "Starting Gammu SMSD instance %s" "$instance"
				if instanceup "$instance" ; then
					started
					continue
				fi
				daemon --pidfile /var/run/gammu-smsd/$instance.pid \
					/usr/bin/gammu-smsd --config /etc/gammu-smsd/${instance}.conf \
						--pid /var/run/gammu-smsd/$instance.pid \
						--user gammu-smsd \
						--daemon \
						 ${GAMMU_SMSD_OPTS}

				RET=$?
			fi
			[ $RETVAL -eq 0 ] && RETVAL=$RET
		done
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/gammu-smsd
	else
		msg_already_running "Gammu SMSD"
	fi
}

stop() {
	if anyinstancesup; then
		# Stop daemons.
		msg_stopping "Gammu SMSD"; started
		for instance in $SMSD_INSTANCES; do
			pidfile=/var/run/gammu-smsd/$instance.pid
			[ -f "$pidfile" ] || continue
			pid=$(cat "$pidfile")
			show "Stopping Gammu SMSD instance %s" "$instance"; busy
			killproc --pidfile "$pidfile" || err=1
			rm -f "$pidfile" >/dev/null 2>&1
		done
		anyinstancesup || rm -f /var/lock/subsys/gammu-smsd >/dev/null 2>&1
	else
		msg_not_running "Gammu SMSD"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  reload|force-reload)
	if instancesup; then
		msg_reloading "Gammu SMSD"; started
		for instance in $SMSD_INSTANCES; do
			show "Reloading Gammu SMSD instance %s" "$instance"
			killproc --pidfile gammu-smsd/$instance.pid gammu-smsd -HUP
			[ $? -ne 0 -a $RETVAL -eq 0 ] && RETVAL=7
		done
	else
		msg_not_running "Gammu SMSD"
		exit 7
	fi
	;;
  restart)
	stop
	sleep 1
	start
	exit $?
	;;
  status)
	nls "Configured Gammu SMSD instances:"
   	echo " $SMSD_INSTANCES"
	nls "Currently running Gammu SMSD instances:"
	for pidfile in /var/run/gammu-smsd/*.pid; do
		[ -f "$pidfile" ] || continue
		instance=${pidfile#/var/run/gammu-smsd/}
		instance=${instance%.pid}
		instanceup $instance && echo -n " $instance($(cat $pidfile))"
	done
	echo ""
	instancesup
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
	;;
esac

exit $RETVAL
