#!/bin/sh

# $FreeBSD$
#
# PROVIDE: smartd-daemon
# REQUIRE: LOGIN
# KEYWORD: shutdown nojail
#
# Define these smartd_daemon_* variables in one of these files:
#	/etc/rc.conf
#	/etc/rc.conf.local
#	/etc/rc.conf.d/smartd_daemon
#
# DO NOT CHANGE THESE DEFAULT VALUES HERE

. /etc/rc.subr

name=smartd_daemon
rcvar=smartd_daemon_enable

load_rc_config smartd_daemon

: ${smartd_daemon_enable:="NO"}

required_files=${smartd_daemon_config:="/usr/local/etc/smartd.conf"}
pidfile=${smartd_daemon_pidfile:="/var/run/smartd-daemon.pid"}

command="/usr/local/sbin/smartd"
command_args="-c ${required_files} -p ${pidfile}"

extra_commands="reload report"
reload_cmd="smartd_daemon_reload"
report_cmd="smartd_daemon_report"

start_precmd=smartd_daemon_prestart
start_cmd=smartd_daemon_start

smartd_daemon_prestart()
{
	case "${smartd_daemon_flags}" in
	-p*|*-p*)
	err 1 'smartd_daemon_flags includes the -p option, use smartd_daemon_pidfile instead'
		;;
	esac
}

smartd_daemon_start()
{
	check_startmsgs && echo "Starting $name."
	/usr/sbin/daemon -f ${command} -n ${smartd_daemon_flags} ${command_args}
}

smartd_daemon_reload()
{
	local status

	if ! status=`run_rc_command status 2>&1`; then
		echo $status
		return 1
	fi
	echo 'Reloading smartd.'
	kill -HUP $rc_pid
}

smartd_daemon_report()
{
	local status

	if ! status=`run_rc_command status 2>&1`; then
		echo $status
		return 1
	fi
	echo 'Checking SMART devices now.'
	kill -USR1 $rc_pid
}

run_rc_command "$1"
