#!/bin/bash

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


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

	# systemd

	[ -h /etc/systemd/system/multi-user.target.wants/gogs-chroot.service ] && rm /etc/systemd/system/multi-user.target.wants/gogs-chroot.service

	systemctl daemon-reload

	if deb-systemd-helper --quiet was-enabled chr-git.target; then
		deb-systemd-helper enable chr-git.target >/dev/null || true
	else
		deb-systemd-helper update-state chr-git.target >/dev/null || true
	fi

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

	systemctl restart rsyslog

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

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

	### sysvinit

	if [ -x "/etc/init.d/gogs-chroot" ]; then
		update-rc.d gogs-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/git/run /systemd/notify file
/usr/lib/3gsystools/chr_placeholder_check /chroot/git/run /systemd/journal/dev-log file

echo "Checking admin mysql socket placeholder"
/usr/lib/3gsystools/chr_placeholder_check /chroot/git/run /mysqld dir

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

exit 0
