#!/bin/sh
#
# Simple service execution tool

USAGE=$"Usage: ""$0 [service_name [start|stop|restart|..]]"
VERSION="${0##*/} ver. 0.61"

INITDDIR="/etc/init.d"
SERVICE=
OPTIONS=

if [ $# -eq 0 ]; then
   echo "${USAGE}" >&2
   exit 1
fi

while [ $# -gt 0 ]; do
   case "$1" in
   --help | -h)
      echo "${USAGE}" >&2
      exit 0
      ;;
   --version | -V)
      echo "${VERSION}" >&2
      exit 0
      ;;
   *)
      if [ -z "${SERVICE}" ]; then
         SERVICE="${1}"
      else
         OPTIONS="${OPTIONS} ${1}"
      fi
      shift
      ;;
   esac
done

if [ -d /run/systemd ]; then
   systemctl ${OPTIONS} ${SERVICE}.service
elif [ -x "${INITDDIR}/${SERVICE}" ]; then
   env -i PATH="$PATH" TERM="$TERM" "${INITDDIR}/${SERVICE}" ${OPTIONS}
else
   echo "${SERVICE}: "$"unrecognized service" >&2
   exit 1
fi
