#! /bin/sh
#
# Start or stop process accounting
#
# Initial version written by Ian Murdock <imurdock@debian.org>
# This version written by Dirk Eddelbuettel <edd@debian.org>   

set -e 

test -x /usr/sbin/accton || exit 0

# If you want to keep acct installed, but not started automatically, set this
# variable to 0. Because /etc/cron.daily/acct calls this file daily, it is
# not sufficient to stop acct once after booting if your machine remains up.
START_ACCT=1

case "$1" in
  start)
    # We start acct only if the switch variable tells us to
    if [ $START_ACCT -eq 1 ] 
    then
	# Have to turn this on to be able to test the return code
	set +e
	echo -n "Starting process accounting: "
	/usr/sbin/accton /var/account/pacct 2>/dev/null
	rv=$?
	if [ $rv -eq 0 ]
	then
	    echo "done."
	elif [ $rv -eq 38 ]
	then
	    echo "failed"
	    echo "Process accounting not available on this system."
	elif [ $rv -eq 16 ]
	then
	    echo "failed"
	    echo "Process accounting already running on this system."
	else
	    logger -f /var/log/daemon.log \
		    "Unexpected error code $rv received in /etc/init.d/acct"
	fi
	set -e 
    fi
    ;;
  stop)
    echo -n "Stopping process accounting: "
    # Have to turn this on to be able to test the return code
    set +e
    /usr/sbin/accton 2>/dev/null
    if [ $? -eq 0 ]
    then
	echo "done."
    else
      echo "failed."
      echo "Process accounting not available on this system."
    fi
    set -e
    ;;
  restart|force-reload) 
    echo -n "Stopping process accounting: "
    # Have to turn this on to be able to test the return code
    set +e
    /usr/sbin/accton 2>/dev/null
    if [ $? -eq 0 ]
    then
	echo "done."
    else
	echo "failed."
	echo "Process accounting not available on this system."
    fi
    set -e
    # We start acct only if the switch variable tells us to
    if [ $START_ACCT -eq 1 ] 
    then
	# Have to turn this on to be able to test the return code
	set +e
	echo -n "Starting process accounting: "
	/usr/sbin/accton /var/account/pacct 2>/dev/null
	rv=$?
	if [ $rv -eq 0 ]
	then
	    echo "done."
	elif [ $rv -eq 38 ]
	then
	    echo "failed"
	    echo "Process accounting not available on this system."
	elif [ $rv -eq 16 ]
	then
	    echo " failed"
	    echo "Process accounting already running on this system."
	else
	    logger -f /var/log/daemon.log \
		    "Unexpected error code $rv received in /etc/init.d/acct"
	fi
	set -e 
    fi
    ;;
  *)
    echo "Usage: /etc/init.d/acct {start|stop|restart|force-reload}"
    exit 1
esac

exit 0
