#!/bin/sh
# $Owl: Owl/packages/owl-startup/service,v 1.5 2005/11/16 13:21:54 solar Exp $

# This script is mostly compatible with Red Hat's service script,
# version 0.91.  However, it is not so ugly and support for option
# --status-all has been intentionally dropped.

VERSION="service version 0.91-owl1"
SERVICEDIR=/etc/init.d

function usage()
{
	echo -n "Usage: ${0##*/} --version | " >&2
	echo "[ service_name [ command | --full-restart ] ]" >&2
	exit $1
}

function fatal()
{
	echo "${0##*/}: $*" >&2
	exit 1
}

function check_service()
{
	if [ -z "${SERVICE##*/*}" ]; then
		fatal "$SERVICE: Invalid service name"
	fi
	if [ ! -x "$SERVICEDIR/$SERVICE" ]; then
		fatal "$SERVICE: Unrecognized service"
	fi
}

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

if [ $# -eq 0 ]; then
	usage 1
fi

SERVICE=
OPTIONS=

while [ $# -gt 0 ]; do
	case "$1" in
	--help|-h)
		usage 0
		;;
	--version|-V)
		echo "$VERSION"
		exit 0
		;;
	-*)
		usage 1
		;;
	*)
		if [ $# -eq 2 -a "$2" = "--full-restart" ]; then
			SERVICE="$1"
			check_service
			cd / || exit
			"$SERVICEDIR/$SERVICE" stop
			exec "$SERVICEDIR/$SERVICE" start
			exit
		elif [ -z "$SERVICE" ]; then
			SERVICE="$1"
			check_service
		else
			OPTIONS="$OPTIONS $1"
		fi
		shift
		;;
	esac
done

cd / || exit
exec "$SERVICEDIR/$SERVICE" $OPTIONS
