#!/bin/sh
# ====================[ restart-service                    ]====================
#                     [ Time-stamp: "2009-04-24 18:42:15 leycec" ]             
#
# --------------------( SYNOPSIS                           )--------------------
# Start, stop, and restart Gentoo-specific "/etc/init.d/"-style services,
# conveniently.
#
# --------------------( CHANGELOG                          )--------------------
# 2009-07-08  Cecil Curry  <http://raiazome.com>
#   * Permitted script to be called as, alternatively, "stop-service" or
#     "start-service" and perform the service action corresponding to that.
#
# 2009-04-20  Cecil Curry  <http://raiazome.com>
#   * Created.

# ....................{ CONSTANTS                          }....................
SCRIPT_NAME=$(basename "$0")
SCRIPT_VERSION="0.0.1"

# ....................{ I/O HANDLING                       }....................
mutter() {
  echo -n "${SCRIPT_NAME}: $*"
}

utter() {
  if [ -n "$IS_CRON" ]
  then logger -p cron.notice "${SCRIPT_NAME}: $*"
  else echo "${SCRIPT_NAME}: $*"
  fi
}

curse() {
  if [ -n "$IS_CRON" ]
  then logger -p cron.err "${SCRIPT_NAME}! $*"
  else echo "${SCRIPT_NAME}! $*" 1>&2
  fi
}

# ....................{ ERROR HANDLING                     }....................
try() {
  $*
  [ $? = 0 ] || exit 1
}

die() {
  curse $*
  exit 1
}

# ....................{ SERVICE ALIASES                    }....................
[ -n "$1" ] || die "no service name to restart passed!"
case "$1" in   # alias service name abbreviations to their proper names
  rs)   SERVICE_NAME="rtorrent-screen";;
  cron) SERVICE_NAME="vixie-cron";;
  *)    SERVICE_NAME=$1; break;;
esac

# ....................{ IMPLEMENTATION                     }....................
SERVICE_FILE="/etc/init.d/$SERVICE_NAME"
if [ ! -x "$SERVICE_FILE" ]; then
  SERVICE_NAME="${SERVICE_NAME}d"
  SERVICE_FILE="/etc/init.d/$SERVICE_NAME"

  if [ ! -x "$SERVICE_FILE" ]; then
    die "'$SERVICE_FILE' not found!"
  fi
fi  

case "$SCRIPT_NAME" in
  stop-service)    SERVICE_ACTION="stop";    SERVICE_ACTION_VERB="stopping";;
  start-service)   SERVICE_ACTION="start";   SERVICE_ACTION_VERB="starting";;
  restart-service) SERVICE_ACTION="restart"; SERVICE_ACTION_VERB="restarting";;
  *) die "$SCRIPT_NAME not a recognized service action!"
esac


utter "$SERVICE_ACTION_VERB '$SERVICE_FILE'..."
try "$SERVICE_FILE" $SERVICE_ACTION

# --------------------( COPYRIGHT AND LICENSE              )--------------------
# The information below applies to everything in this distribution,
# except where noted.
#              
# Copyleft 2009 by B.w.Curry.
#   
#   http://www.raiazome.com
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
