#!/bin/sh
# $Id: service,v 1.9 2004/01/25 17:44:48 ldv 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.

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

VERSION="service version 0.91-alt"
SERVICEDIR="/etc/init.d"
SERVICE=
OPTIONS=

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

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

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

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

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
				"$SERVICEDIR/$SERVICE" start
				exit $?
			elif [ -z "$SERVICE" ]; then
				SERVICE="$1"
				check_service
			else
				OPTIONS="$OPTIONS $1"
			fi
			shift
			;;
	esac
done

cd / || exit
"$SERVICEDIR/$SERVICE" ${OPTIONS}
