#!/bin/bash

# create bind user
getent group bind >/dev/null 2>&1 ||
	addgroup --system bind
getent passwd bind >/dev/null 2>&1 ||
	adduser --system --home /var/cache/bind --no-create-home --disabled-password --shell /bin/false --ingroup bind bind


INITSYS=`readlink -f /proc/1/exe`
if [ "$INITSYS" == "/lib/systemd/systemd" -o "$INITSYS" == "/usr/lib/systemd/systemd" ]; then

	# systemd
	if deb-systemd-helper --quiet was-enabled bind9-chroot.service; then
		deb-systemd-helper enable bind9-chroot.service >/dev/null || true
	else
		deb-systemd-helper update-state bind9-chroot.service >/dev/null || true
	fi

	systemctl restart rsyslog
	systemctl daemon-reload

	if ! [ -e /chroot/named/dev/log ]; then
		ln -s ../run/systemd/journal/dev-log /chroot/named/dev/log
	fi

elif [ "$INITSYS" == "/sbin/init" ]; then

	### sysvinit

	if [ -x "/etc/init.d/bind9-chroot" ]; then
		update-rc.d bind9-chroot defaults >/dev/null
	fi

	if [ -x "/etc/init.d/rsyslog" ]; then
		/etc/init.d/rsyslog restart
	fi

else
	echo "No init system found, check manually"
fi

echo "Checking systemd socket placeholders"
/usr/lib/3gsystools/chr_placeholder_check /chroot/named/run /systemd/notify file
/usr/lib/3gsystools/chr_placeholder_check /chroot/named/run /systemd/journal/dev-log file

echo "Checking fstab"
/usr/local/sbin/fstab-chr

exit 0
