#! /bin/sh

set -e

add_munin_system_user() {
	if ! getent passwd munin >/dev/null; then
		adduser --group --system --no-create-home \
			--home /var/lib/munin munin;
	fi	
}

fixperms() {
	chown munin:adm /var/log/munin
	chmod 0750 /var/log/munin

	chown munin:root /var/run/munin
	chmod 0755 /var/run/munin

	chown munin:munin /var/www/munin
	chmod 0755 /var/www/munin

	chown munin:munin /var/lib/munin
	chmod 0755 /var/lib/munin

	# Upgrading from LRRD, yes?
	# This'll generate a lot of chmod/grp's on
	# the dirs, but what the hell..
	if getent passwd lrrd >/dev/null; then
		for f in $(find /var/lib/munin -user lrrd); do
			chown munin $f ${f%/*}
		done
	fi
	if getent group lrrd >/dev/null; then
		for f in $(find /var/lib/munin -group lrrd); do
			chgrp munin $f ${f%/*}
		done
	fi
}

case "$1" in
	configure)
		add_munin_system_user
		fixperms
		;;
	abort-upgrade|abort-deconfigure|abort-remove)
		:
		;;
	*)
		echo "Called with unknown argument $1, bailing out."
		exit 1
		;;
esac

#DEBHELPER#
