# Shell functions for the scripts in /etc/init.d

# If $DAEMON is defined before including this file, some additional preparations is performed

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/mod/sbin:/mod/bin:/mod/usr/sbin:/mod/usr/bin
export LD_LIBRARY_PATH=/mod/lib:/mod/usr/lib


# modlib_addgroup:
#   check for group name, create group if not found.
#   No check is done for a change in the optional arguments!
#	$1: group name
#	$2-n: optional arguments for addgroup
modlib_addgroup()
{
	local group="$1"
	shift
	echo -n "Looking for group '$group' ... "
	if grep -q "^$group:" /etc/group; then
		echo "found"
	elif addgroup "$@" $group; then
		echo -n "created - now saving to data buffer ... "
		modusers save && echo "done" || echo "failed"
	else
		echo "not created - error occurred"
	fi
}

# modlib_adduser:
#   check for user name, create user if not found.
#   No check is done for a change in the optional arguments!
#	$1: user name
#	$2-n: optional arguments for adduser
modlib_adduser()
{
	local user="$1"
	shift
	echo -n "Looking for user '$user' ... "
	if grep -q "^$user:" /etc/passwd; then
		echo "found"
		return 0
	fi
	local msg=$(adduser "$@" $user 2>&1)
	if [ "$?" = 0 ]; then
		echo -n "created - now saving to data buffer ... "
		modusers save && echo "done" || echo "failed"
	else
		echo "not created - error occurred: $msg"
		exit 1
	fi
}

# modlib_check_running
#   check whether daemon is running. return status
#	$1: optional pid-file. Default /var/run/$DAEMON.pid
modlib_check_running()
{
	local fn="$1"
	[ -z "$fn" ] && fn=/var/run/$DAEMON.pid
	[ ! -s "$fn" ] && return 1
	kill -0 $(cat "$fn") 2> /dev/null
	local status="$?"
	[ "$status" != "0" ] && rm -f "$fn"
	return "$status"
}

# modlib_stop
#   stop daemon
#	$1: optional pid-file. Default /var/run/$DAEMON.pid
modlib_stop()
{
	local fn="$1"
	[ -z "$fn" ] && fn=/var/run/$DAEMON.pid

	echo -n "Stopping $DAEMON..."
	if ! modlib_check_running "$fn"; then
		echo 'not running.'
		return 0
	fi
	kill $(cat "$fn") 2> /dev/null
	exitval=$?
	rm -f "$fn"
	if [ "$exitval" -eq 0 ]; then
		echo 'done.'
		return 0
	else
		echo 'failed.'
		return 1
	fi
}

# modlib_reload
#   reload daemon
#	$1: optional pid-file. Default /var/run/$DAEMON.pid
modlib_reload()
{
	local fn="$1"
	[ -z "$fn" ] && fn=/var/run/$DAEMON.pid

	echo -n "Reloading $DAEMON..."
	if ! modlib_check_running "$fn"; then
		echo 'not running.'
		return 0
	fi
	kill -HUP $(cat "$fn") 2> /dev/null
	exitval=$?
	if [ "$exitval" -eq 0 ]; then
		echo 'done.'
		return 0
	else
		rm -f "$fn"
		echo 'failed.'
		return 1
	fi
}

# modlib_restart
#   restart daemon
#   function "start" must be defined in the calling script
#	$1: optional pid-file. Default /var/run/$DAEMON.pid
modlib_restart()
{
	modlib_stop "$1"
	sleep 1
	start
}

# modlib_status
#   check whether daemon is running
#	$1: optional pid-file. Default /var/run/$DAEMON.pid
modlib_status()
{
	if modlib_check_running "$1"; then
		echo 'running'
	else
		echo 'stopped'
	fi
}

# load config
[ -n "$DAEMON" ] && case "$1" in
	""|load|start|restart|config|status)
		if [ ! -r "/mod/etc/conf/$DAEMON.cfg" ]; then
			echo "Error[$DAEMON]: not configured" 1>&2
			exit 1
		fi

		. /mod/etc/conf/$DAEMON.cfg
		;;
esac

