#!/usr/bin/env /lib/runit/invoke-run
set -e

NAME="smartmontools"
DAEMON=/usr/sbin/smartd
SMARTCTL=/usr/sbin/smartctl

# Exit service if DAEMON is not installed
if [ ! -x $DAEMON ]; then
	exit 161
fi

# Load defaults
[ -r /etc/default/smartmontools ] && . /etc/default/smartmontools

# SMARTCTL check each device
if [ -x $SMARTCTL ]; then
	if [ ! -z "$enable_smart" ]; then
        for device in $enable_smart; do
            if ! $SMARTCTL --quietmode=silent --smart=on $device; then
                echo "SMARTCTL: $device failed"
            fi
        done
    fi        
fi

# If so configured, don't start smard
if [ ! "$start_smartd" = "yes" ]; then
	exit 161
fi

exec 2>&1

exec $DAEMON -n ${smartd_opts}
