#! /bin/sh

set -e

prevver="$2"

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/lib/munin
	chmod 0755 /var/lib/munin

	chown munin:munin /var/lib/munin/plugin-state
	chmod 0775 /var/lib/munin/plugin-state
}

init_plugins() {
	if [ "$prevver" ]; then
		echo -n "Initializing new plugins.."
		munin-node-configure --shell --newer "${prevver%-*}" | sh
	else
		echo -n "Initializing plugins.."
		munin-node-configure --shell | sh
	fi
	echo "done."
}

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

#DEBHELPER#
