#!/bin/sh

user="logcheck"
group="logcheck"
configfiles="logcheck.conf logcheck.logfiles"

case $2 in
POST-INSTALL)
    if [ "" = "CRON" ]; then
	if [ -f /usr/local/share/examples/logcheck/crontab.in ] ; then
		if /usr/bin/crontab -u "${user}" -l >/tmp/logchecktab$$ 2>&1 ; then
			if test -s /tmp/logchecktab$$; then
				echo "---> \"${user}\" already has a crontab.  Not overwriting it"
				echo "---> Please merge any changes from the standard crontab file"
				echo "--->     /usr/local/share/examples/logcheck/crontab.in"
			else
				/usr/bin/crontab -u "${user}" "/usr/local/share/examples/logcheck/crontab.in" || exit 1
				echo "---> Installed crontab(5) file for user \"${user}\""
			fi
		else
			if grep -q "are not allowed to use this program" /tmp/logchecktab$$ ; then
				echo "---> The logcheck user is not allowed to run crontab."
				echo "---> Please check the contents of /var/cron/allow and /var/cron/deny"
				echo "---> and grant access, if necessary."
				exit 1
			else
				/usr/bin/crontab -u "${user}" "/usr/local/share/examples/logcheck/crontab.in" || exit 1
				echo "---> Created crontab(5) file for user \"${user}\""
			fi
		fi
		rm -f /tmp/logchecktab$$
        fi
    else
        echo "---> Crontab file was not installed for user \"${user}\""
        if [ -f /usr/local/share/examples/logcheck/crontab.in ]; then
            echo "---> A sample crontab file can be found in /usr/local/share/examples/logcheck/crontab.in"
            echo "---> for manual installation."
        fi
    fi

	for f in ${configfiles}; do
		if [ ! -e /usr/local/etc/logcheck/${f} ]; then
			echo "---> It seems you have no previous version of /usr/local/etc/logcheck/${f}"
			cp /usr/local/etc/logcheck/${f}.sample /usr/local/etc/logcheck/${f}
			echo "---> Created one from ${f}.sample"
		fi
	done
;;
esac
