#! /bin/sh
# Copyright (c) 1995-2001 SuSE GmbH, Nuernberg, Germany.
# Copyright (c) 2002-2003 SuSE Linux AG, Nuernberg, Germany.
#
# Author: Marius Tomaschewski <mt@suse.de>
#
# init.d/tinyproxy
#
#   and symbolic its link
#
# (usr/)sbin/rctinyproxy
#
# System startup script for the tiny http proxy
#
### BEGIN INIT INFO
# Provides:       tinyproxy
# Required-Start: $remote_fs $syslog $named
# Required-Stop:  $remote_fs $syslog $named
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start tiny http-proxy.
### END INIT INFO
#
# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

TINYPROXY_BIN=/usr/sbin/tinyproxy
test -x $TINYPROXY_BIN || exit 5

TINYPROXY_CFG=/etc/tinyproxy/tinyproxy.conf
test -x $TINYPROXY_CFG || exit 6

function read_pid_name ()
{
  local string=$(grep ^PidFile $TINYPROXY_CFG 2>/dev/null)
  set -- $string
  TINYPROXY_PID=${2:-/var/run/tinyproxy/tinyproxy.pid}
}
read_pid_name
test -n "$TINYPROXY_PID" || exit 6

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

case "$1" in
start)
	echo -n "Starting tiny http proxy: "
	startproc -p $TINYPROXY_PID $TINYPROXY_BIN $TINYPROXY_PARAMS
	rc_status -v
;;
stop)
	echo -n "Shutting down tiny http proxy: "
	killproc -p $TINYPROXY_PID -TERM $TINYPROXY_BIN
	rc_status -v
;;
try-restart)
	$0 status >/dev/null &&  $0 restart
	rc_status
;;
restart)
	$0 stop
	$0 start
	rc_status
;;
force-reload)
	echo -n "Reload service tiny http proxy: "
	killproc -p $TINYPROXY_PID -HUP $TINYPROXY_BIN
	rc_status -v
;;
reload)
	echo -n "Reload service tiny http proxy: "
	killproc -p $TINYPROXY_PID -HUP $TINYPROXY_BIN
	rc_status -v
;;
status)
	echo -n "Checking for tiny http proxy: "
	checkproc -p $TINYPROXY_PID $TINYPROXY_BIN
	rc_status -v
;;
probe)
	test $TINYPROXY_CFG -nt $TINYPROXY_PID && echo reload
;;
*)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
;;
esac
rc_exit

