#!/bin/sh -e
# 
# smartmontools init.d startup script
#
# (C) 2003,04 Guido Gnther <agx@sigxcpu.org>
# 
# loosely based on the init script that comes with smartmontools which is
# copyrighted 2002 by Bruce Allen <smartmontools-support@lists.sourceforge.net>

SMARTCTL=/usr/sbin/smartctl
SMARTD=/usr/sbin/smartd
SMARTDPID=/var/run/smartd.pid
[ -x $SMARTCTL ] || exit 0
[ -x $SMARTD ] || exit 0
RET=0

[ -r /etc/default/smartmontools ] && . /etc/default/smartmontools

smartd_opts="--pidfile $SMARTDPID $smartd_opts"

enable_smart() {
  echo -n "Enabling S.M.A.R.T. for:"
  for device in $enable_smart; do
        echo -n " $device"
	$SMARTCTL --quietmode=errorsonly --smart=on $device || \
	    { echo -n "(failed)"; RET=2; }
  done
  echo "."
}

check_start_smartd_option() {
  if [ ! "$start_smartd" = "yes" ]; then
    echo "Not starting S.M.A.R.T. daemon smartd, disabled via /etc/default/smartmontools"
    return 1
  else
    return 0
  fi
}

case "$1" in
  start)
        [ -n "$enable_smart" ] && enable_smart
	if check_start_smartd_option; then
	    echo -n "Starting S.M.A.R.T. daemon: smartd"
            if start-stop-daemon --start --quiet --pidfile $SMARTDPID \
	    		--exec $SMARTD -- $smartd_opts; then 
	    	echo "."
	    else
	        echo " (failed)"
		RET=1
	    fi
	fi
	;;
  stop)
	echo -n "Stopping S.M.A.R.T. daemon: smartd"
	start-stop-daemon --stop --quiet --oknodo --pidfile $SMARTDPID
	echo "."
	;;
  reload|force-reload)
	echo -n "Reloading S.M.A.R.T. daemon: smartd"
	if start-stop-daemon --stop --quiet --signal 1 \
			--pidfile $SMARTDPID; then
	     echo "."
	else
	     echo " (failed)"
	     RET=1
	fi
  	;;
  restart)
	if check_start_smartd_option; then
	    echo -n "Restarting S.M.A.R.T. daemon: smartd"
	    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $SMARTDPID
            if start-stop-daemon --start --quiet --pidfile $SMARTDPID \
	    		--exec $SMARTD -- $smartd_opts; then 
	    	echo "."
	    else
	        echo " (failed)"
		RET=1
	    fi
	fi
        ;;
  *)
	echo "Usage: /etc/init.d/smartmontools {start|stop|restart|reload|force-reload}"
	exit 1
esac

exit $RET
