#!/bin/sh

# $Id: service,v 1.2 2004/08/10 15:35:31 john Exp $

# service script for Debian.  It uses invoke-rc.d to simulate the Red Hat
# 'service' command.
# Written by John Hasler, 2004
# You may treat this work as if it were in the public domain.
# I waive all rights.

# --status-all calls all scripts with "status".
# --full-restart calls specified script with "stop" and then "start".

VERSION="0.2"
PATH=/bin:/usr/bin:/sbin:/usr/sbin
INVOKERC="invoke-rc.d --force --quiet"
INITDIR="/etc/init.d"

USAGE="Usage: `basename $0` [[-h] | [--h] | [--help] | [-V] | [--version] | [--status-all]]
 | service-name action
'service-name' is one of the scripts in /etc/init.d. 'action' is one of stop,
start, restart, status, reload, force-stop, force-reload, or --full-restart."

cd "$INITDIR"

# It's an error to have zero or more than two arguments.
if [ $# -eq 0 ] || [ $# -gt 2 ] ; then
    echo "$USAGE" >&2
    exit 1
fi

# Handle single-argument cases.
if [ $# -eq 1 ] ; then
    case "$1" in
	--version | -V )
	    echo $VERSION >&2
	    exit 0
	    ;;
	--help | -h | --h )
	    echo $"$USAGE" >&2
	    exit 0
	    ;;
	# --status-all calls all scripts with the 'status' command.
	--status-all )
#	    for SERVICE in * ; do
#		case "$SERVICE" in
#		    # None of these are valid scripts.  Skip them.
#		    skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
#			;; 
#		    * )
#			# Make sure the script is executable.
#			if [ -x "$SERVICE" ] ; then
#                            $INVOKERC "$SERVICE" status
#                        fi
#                        ;;
#		esac
#	    done
	    echo "--status-all does nothing on Debian" >&2
	    exit 0
	    ;;    
	* )
	    echo $"$USAGE" >&2
	    exit 1
	    ;;
    esac
fi

SERVICE="$1"
ACTION="$2"

# Make sure the service exists and is executable.
if [ ! -x "$SERVICE" ]  ; then
    echo "${SERVICE}: unrecognized service" >&2
   exit 1
fi

# Handle two argument cases.
case "$ACTION" in
    stop | start | restart | status | reload | force-stop | force-reload )
	$INVOKERC "$SERVICE" "$ACTION" && exit $?
	;;
    --full-restart )
	$INVOKERC "$SERVICE" stop || exit $?
	$INVOKERC "$SERVICE" start || exit $?
	exit 0
	;;
    * )
	echo "${ACTION}: no such action" >&2
	exit 1
	;;
esac
