#!/bin/sh

# Based by martijn@inlv.org on init.d script that came with the Debian patch
# NOTE: do not "source" or "." this script, it exits!

source /etc/hddtemp.conf || exit $?

PATH=/bin:/sbin:/usr/bin:/usr/sbin
NAME=hddtemp
DAEMON=/usr/sbin/hddtemp
DESC="disk temperature monitoring daemon"

if test -n "$RUN_SYSLOG" -a "$RUN_SYSLOG" != "0"; then
	SYSLOG_ARG="-S $RUN_SYSLOG"
fi

DAEMON_ARG="-d -l $INTERFACE -p $PORT -s $SEPARATOR"

test -x "$DAEMON" || exit 0

case "$1" in
  start)
	# master switch
  	if test -n "$DAEMON_ARG" -o -n "$SYSLOG_ARG"; then
		echo -n "Starting $DESC" "$NAME: "
		CDROMS_LIST="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2>/dev/null | sed -e 's/^drive name:\t//g' -e 's/\t/ \/dev\//g')"
		CDROMS_LIST="$CDROMS_LIST $(grep -sl '^ide-scsi ' /proc/ide/hd*/driver | awk -F / '{ print "/dev/"$4 }')"
		for disk in $DISKS ; do
			echo $CDROMS_LIST $DISKS_NOPROBE | grep -wq $disk && continue
			if /usr/sbin/hddtemp -wn $OPTIONS $disk 2>/dev/null | /bin/grep -q '^[0-9]\+$' ; then
				DISKS_LIST="$DISKS_LIST $disk";
			fi
		done
		if test -n "$DISKS_LIST" -o -n "$DISKS_NOPROBE"; then
			$DAEMON $DAEMON_ARG $SYSLOG_ARG $OPTIONS $DISKS_NOPROBE $DISKS_LIST
			ret=$?
			echo "$DISKS_NOPROBE$DISKS_LIST."
			exit $ret
		else
			echo "no disks with monitoring capability were found."
			exit 0
		fi
	fi
	;;
  stop)
	# master switch
	if test "$RUN_DAEMON" = "true" -o "$RUN_DAEMON" = "yes" -o "$RUN_SYSLOG" != "0"; then
		echo "Stopping $DESC" "$NAME"
		killall $DAEMON
		exit $?
	fi
	;;
  restart)
	$0 stop && $0 start
	;;
  *)
	echo "Usage: /etc/rc.d/rc.$NAME {start|stop|restart}" >&2
	exit 1
	;;
esac

exit 0
