#!/bin/sh -e
# 
# smartmontools init.d startup script
#
# (C) 2003 Guido Gnther <agx@sigxcpu.org>
# 
# based on the init script that comes with smartmontools which is
# Copyright (C) 2002 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 "."
}

case "$1" in
  start)
        [ ! -z "$enable_smart" ] && enable_smart
	if [ "$start_smartd" = "yes" ]; 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 "."
	;;
  restart|force-reload)
        $0 stop
        $0 start
        ;;
  *)
	echo "Usage: /etc/init.d/smartmontools {start|stop|restart|force-reload}"
	exit 1
esac

exit $RET
