#!/bin/sh
#
# zfs-zed
#
# chkconfig:    2345 04 96
# description:  This script will start and stop the ZFS Event Daemon.
# probe: true
#
### BEGIN INIT INFO
# Provides:          zfs-zed
# Required-Start:    zfs-mount
# Required-Stop:     zfs-mount
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Stop-After:      zfs-share
# Short-Description: ZFS Event Daemon
# Description:       zed monitors ZFS events. When a zevent is posted, zed
#                    will run any scripts that have been enabled for the
#                    corresponding zevent class.
### END INIT INFO
#
# Released under the 2-clause BSD license.
#
# The original script that acted as a template for this script came from
# the Debian GNU/Linux kFreeBSD ZFS packages (which did not include a
# licensing stansa) in the commit dated Mar 24, 2011:
#   https://github.com/zfsonlinux/pkg-zfs/commit/80a3ae582b59c0250d7912ba794dca9e669e605a

# Source the common init script
. /etc/zfs/zfs-functions

ZED_NAME="zed"
ZED_PIDFILE="/usr/var/run/$ZED_NAME.pid"

extra_started_commands="reload"

# Exit if the package is not installed
[ -x "$ZED" ] || exit 0

# ----------------------------------------------------

do_start()
{
	check_module_loaded "zfs" || exit 0

	ZED_ARGS="$ZED_ARGS -p $ZED_PIDFILE"

	log_info_msg "Starting ZFS Event Daemon"
       	start_daemon "$ZED" "$ZED_ARGS"
	RETVAL=$?
	evaluate_retval
	return $RETVAL 
}

do_stop()
{
	local pools RET
	check_module_loaded "zfs" || exit 0

	log_info_msg "Stopping ZFS Event Daemon"
	killproc -p "$PIDFILE" "$ZED"
	RETVAL=$?
	evaluate_retval
}

do_status()
{
	check_module_loaded "zfs" || exit 0

	statusproc "$ZED"
	return "$?"
}

do_restart()
{
	do_stop	
	sleep 1
	do_start
}

#----------------------------------------------------

case "$1" in
  start		)
			do_start
			;;
  stop		)
			do_stop
			;;
  status	)
			do_status
			;;

  condrestart	)
			pid=`cat $ZED_PIDFILE 2>/dev/null`
			if [ -n "${pid}"  -a -d /proc/$pid ]; then
			  do_restart
			  fi
			;;

  restart	)
			do_restart
			;;

  *		)
			[ -n "$1" ] && echo "Error: Unknown command $1."
			echo "Usage: $0 {start|stop|status|restart}"
			exit 1
			;;
  esac
exit $?
