#!/bin/sh
# chkconfig: 2345 50 50
# description: no description
# processname: /usr/sbin/sudo_logsrvd
# pidfile: /run/sudo/sudo_logsrvd.pid
### BEGIN INIT INFO
# Provides: sudo_logsrvd
# Required-Start: $local_fs $network
# Should-Start: 
# Required-Stop: 
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: 
### END INIT INFO
# Generated by PolyPackage 1.0.0.20200814
# (c) 2019-2021 Todd C. Miller

prog="sudo_logsrvd"

	#-- definitions specific to service sudo_logsrvd
	svc_name="sudo_logsrvd service"
	user="root"
	pidfile="/run/sudo/sudo_logsrvd.pid"
	stop_signal="15"
	reload_signal=""
	pp_exec_cmd () { /usr/sbin/sudo_logsrvd; }

        #-- use system message logging, if available
        if [ -f /lib/lsb/init-functions -a ! -r /etc/redhat-release ]; then
            . /lib/lsb/init-functions
            pp_success_msg () { log_success_msg "$@"; }
            pp_failure_msg () { log_failure_msg "$@"; }
            pp_warning_msg () { log_warning_msg "$@"; }
        elif [ -f /etc/init.d/functions ]; then
            . /etc/init.d/functions
            pp_success_msg () { echo -n "$*"; success "$@"; echo; }
            pp_failure_msg () { echo -n "$*"; failure "$@"; echo; }
            pp_warning_msg () { echo -n "$*"; warning "$@"; echo; }
        else
            pp_success_msg () { echo ${1:+"$*:"} OK; }
            pp_failure_msg () { echo ${1:+"$*:"} FAIL; }
            pp_warning_msg () { echo ${1:+"$*:"} WARNING; }
        fi

        #-- prints a status message
        pp_msg () { echo -n "$*: "; }

        #-- prints usage message
        pp_usage () {
            echo "usage: $0 {start|stop|status|restart|reload|condrestart|try-restart|force-reload}" >&2
            return 2
        }

        #-- reloads the service, if possible
        #   returns 0=success 1=failure 3=unimplemented
        pp_reload () {
            test -n "$reload_signal" || return 3 # unimplemented
            pp_msg "Reloading ${svc_name}"
            if pp_signal -${reload_signal}; then
                pp_success_msg
                return 0
            else
                pp_failure_msg "not running"
                return 1
            fi
        }

        #-- delivers signal $1 to the pidfile
        #   returns 0=success 1=failure
        pp_signal () {
            if test -s "$pidfile"; then
                read pid < "$pidfile" 2>/dev/null
                kill "$@" "$pid" 2>/dev/null
            else
                return 1
            fi
        }

        #-- verifies that ${svc_name} is running
        #   returns 0=success 1=failure
        pp_running () {
            if test -s "$pidfile"; then
                read pid < "$pidfile" 2>/dev/null
                if test ${pid:-0} -gt 1 && kill -0 "$pid" 2>/dev/null; then
                    # make sure name matches
                    pid="`ps -p $pid 2>/dev/null | sed -n \"s/^ *\($pid\) .*$prog *$/\1/p\"`"
                    if test -n "$pid"; then
                        return 0
                    fi
                fi
            fi
            return 1
        }

        #-- prints information about the service status
        #   returns 0=running 1=crashed 3=stopped
        pp_status () {
            pp_msg "Checking for ${svc_name}"
	    if pp_running; then
                pp_success_msg "running"
                return 0
            elif test -s "$pidfile"; then
                pp_failure_msg "not running (crashed)"
                return 1
            else
                pp_failure_msg "not running"
                return 3
            fi
        }

        #-- starts the service
        #   returns 0=success 1=failure
        pp_start () {
            pp_msg "Starting ${svc_name}"
            if pp_status >/dev/null; then
                pp_warning_msg "already started"
                return 0
            elif pp_exec_cmd; then
                pp_success_msg
                return 0
            else
                pp_failure_msg "cannot start"
                return 1
            fi
        }

        #-- stops the service
        #   returns 0=success (always)
        pp_stop () {
            pp_msg "Stopping ${svc_name}"
            if pp_signal -${stop_signal}; then
                pp_success_msg
            else
                pp_success_msg "already stopped"
            fi
            rm -f "$pidfile"
            return 0
        }

        #-- stops and starts the service
        pp_restart () {
            pp_stop
            pp_start
        }

        case "$1" in
            start)          pp_start;;
            stop)           pp_stop;;
            restart)        pp_restart;;
            status)         pp_status;;
            try-restart|condrestart)
                            if pp_status >/dev/null; then
                                    pp_restart
                            fi;;
            reload)         pp_reload;;
            force-reload)   if pp_status >/dev/null; then
                                    pp_reload
                            else
                                    pp_restart
                            fi;;
            *)              pp_usage;;
        esac

