#!/bin/sh

user="logcheck"
uid="915"
group="logcheck"
gid="915"
descr="Logcheck system account"
homedir="/var/db/logcheck"
shell="/usr/local/bin/bash"
configfiles="logcheck.conf logcheck.logfiles"

case $2 in
PRE-INSTALL)
	if /usr/sbin/pw group show ${group} > /dev/null 2>&1; then
		echo "---> You already have a group \"${group}\", so I will use it."
	else
		/usr/sbin/pw group add "${group}" -g "${gid}"
		echo "---> Created group \"${group}\"."
	fi
	if /usr/sbin/pw user show ${user} > /dev/null 2>&1; then
		echo "---> You already have a user \"${user}\", so I will use it."
	else
		/usr/sbin/pw user add -n ${user} -c "${descr}" -d "${homedir}" -s "${shell}" -g ${group} -u "${uid}"
		echo "---> Created user \"${user}\"."
	fi
;;
POST-INSTALL)
	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

	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
