#! /bin/sh
# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author: Kurt Garloff <garloff@suse.de>
#
# init.d/nessusd
#
#   and its symbolic link
#
# /sbin/rcnessusd
#
# System startup script for the nessus backend nessusd
#
### BEGIN INIT INFO
# Provides: nessusd
# Required-Start: $network 
# X-SuSE-Should-Start: named sshd lshd inetd nscd dhcp $remote_fs
# Required-Stop:  $network
# X-SuSE-Should-Stop: named sshd lshd inetd nscd dhcp $remote_fs
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description: Start nessusd to allow security scans from this host
### END INIT INFO

NESSUS_BIN=/usr/sbin/nessusd
NESSUS_CFG=/etc/nessus/nessusd.conf
NESSUS_PID=/var/run/nessusd.pid

test -x $NESSUS_BIN || exit 5

# 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_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 nessusd "
	## Start daemon with startproc(8). If this fails
	## the echo return value is set appropriate.
	err=0
	if test ! -d /var/lib/nessus/users; then 
		echo -e "\n${warn}Please create user accounts with nessus-adduser${norm}"
		# return LSB exit code: 6 - not configured
		err=6
	fi
	if test ! -d /var/lib/nessus/CA; then
		echo -e "\n ${warn}Please create your SSL certificate with nessus-mkcert${norm}"
		# return LSB exit code: 6 - not configured
		err=6
	fi
	if [ $err != 0 ]; then
		exit $err;
	fi
	unset err
	startproc $NESSUS_BIN

	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	echo -n "Shutting down nessusd"
	## Stop daemon with killproc(8) and if this fails
	## set echo the echo return value.

	killproc -TERM $NESSUS_BIN

	# Remember status and be verbose
	rc_status -v
	;;

    try-restart|condrestart)
        ## Do a restart only if the service was active before.
        ## Note: try-restart is now part of LSB (as of 1.9).
        ## RH has a similar command named condrestart.
        if test "$1" = "condrestart"; then
                echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        # Remember status and be quiet
        rc_status
        ;;

    restart)
	## If first returns OK call the second, if first or
	## second command fails, set echo return value.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload|reload)
	## First possibility: A few services accepts a signal
	## to reread the (changed) configuration.

	echo -n "Reload service nessusd"
	#touch $NESSUS_PID
	killproc -HUP $NESSUS_BIN
	rc_status -v
	;;
    status)
	echo -n "Checking for nessusd: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	checkproc $NESSUS_BIN
	rc_status -v
	;;
    probe)
	## Optional: Probe for the necessity of a reload,
	## give out the argument which is required for a reload.

	test $NESSUS_CFG -nt $NESSUS_PID && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
