#! /bin/bash

# smartmontools sysconfig command line options generator
# Copyright (C) 2012 Stanislav Brabec <sbrabec@suse.cz>

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later
# version. 
# You should have received a copy of the GNU General Public License (for
# example COPYING); if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.

# Following code should be in sync with sysv init script for older distros!

# source configuration file.
smartd_opts=
[ -r @localstatedir@/lib/smartmontools/smartd_opts ] && . @localstatedir@/lib/smartmontools/smartd_opts
smartd_opts_old="$smartd_opts"
[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools
smartd_opts=
if test -n "$SMARTD_CHECK_INTERVAL" -a "$SMARTD_CHECK_INTERVAL" != 1800 ; then
	smartd_opts=" -i $SMARTD_CHECK_INTERVAL"
fi
if test -n "$SMARTD_LOG_FACILITY" -a "$SMARTD_LOG_FACILITY" != "daemon" ; then
	smartd_opts="$smartd_opts -l $SMARTD_LOG_FACILITY"
fi
if test -n "$SMARTD_DRIVEDB" ; then
	smartd_opts="$smartd_opts -B $SMARTD_DRIVEDB"
fi
if test "$SMARTD_SAVESTATES" = "no" ; then
	smartd_opts="$smartd_opts -s -"
fi
if test "$SMARTD_ATTRLOG" = "no" ; then
	smartd_opts="$smartd_opts -A -"
fi
if test -n "$SMARTD_EXTRA_OPTS" ; then
	smartd_opts="$smartd_opts $SMARTD_EXTRA_OPTS"
fi

if test "$smartd_opts" = "$smartd_opts_old" ; then
	exit 0
fi

mkdir -p @localstatedir@/lib/smartmontools

echo "# Generated by @prefix@/lib/smartmontools/generate_smartd_opts
smartd_opts=\"$smartd_opts\"" >@localstatedir@/lib/smartmontools/smartd_opts

# SMARTD_SKIP_INIT is used during installation.
if test -z "$SMARTD_SKIP_INIT" ; then
	# Behavior of both "Command" and "ServiceRestart" is undefined. Restart service here.
	@SERVICE@ smartd try-restart
fi
