#!/bin/sh
set -e
case "$1" in
	configure)
		if ! id s6log >/dev/null 2>&1; then
			useradd -r -s /usr/sbin/nologin s6log 2>/dev/null || true
		fi
		if id s6log >/dev/null 2>&1; then
			install -d -o s6log -g s6log -m 0755 /var/66 /var/log/66
			chown -R s6log:s6log /var/66 /var/log/66 2>/dev/null || true
		fi

		# wait-oneshotd is obsolete (66-scandir-gate). Stale resolve CDB still
		# lists it in .dependencies; 66 enable walks those deps and fatals if
		# the frontend is missing. 66 free does NOT drop resolves (it is stop -u);
		# only 66 remove does. Use a temporary stub so cleanup can run, then
		# remove contaminated resolves and delete the stub before dh enables.
		# NOTE: never put the debhelper token in a comment — dh expands it.
		stub=/usr/share/66/service/wait-oneshotd
		install -d /usr/share/66/service
		cat >"$stub" <<'EOF'
[Main]
Type = oneshot
Version = 0.0.1
Description = "temporary purge stub — removed before enable"
User = ( root )
Options = ( !log )

[Start]
Execute = (
	true
)
EOF

		rm -f /etc/66/service/wait-oneshotd
		for dir in /usr/share/66/service /etc/66/service; do
			[ -d "$dir" ] || continue
			for f in "$dir"/*; do
				[ -f "$f" ] || continue
				[ "$(basename "$f")" = wait-oneshotd ] && continue
				grep -q wait-oneshotd "$f" 2>/dev/null || continue
				sed -i -E \
					-e 's/[[:space:]]*wait-oneshotd[[:space:]]*/ /g' \
					-e 's/Depends = \( +/Depends = ( /g' \
					-e 's/Contents = \( +/Contents = ( /g' \
					-e 's/ +\)/ )/g' \
					-e 's/Depends = \( \)/Depends = ( )/g' \
					-e 's/Contents = \( \)/Contents = ( )/g' \
					"$f" || true
			done
		done

		if command -v 66 >/dev/null 2>&1; then
			for tree in \
				mount setup devmanager misc-essential getty misc live \
				boot enabled-services logind \
				cryptsetup cryptsetup-shutdown \
				fake-hwclock fake-hwclock-shutdown \
				live-shutdown shutdown recovery default
			do
				66 tree resolve "$tree" >/dev/null 2>&1 || \
					66 tree create "$tree" >/dev/null 2>&1 || true
			done

			# Drop resolve DB for every service dh will re-enable, plus any
			# leftover that still names wait-oneshotd (CDB under svc or .resolve).
			# 66 free != drop resolve; only 66 remove (or rm -rf) clears it.
			{
				printf '%s\n' \
					cleanup dmesg early-getty-down console-fallback halt hostname \
					killprocs kmod-static-nodes locale modules \
					mount-cgroups mount-devfs mount-filesystems mount-procfs \
					mount-rw mount-swap mount-sysfs mount-tmpfs populate-run \
					random-seed rc.local reboot sendsigs setup-loopback sysctl \
					tty1 tty2 tty3 tty4 tty5 tty6 ttyS \
					umountfs umountnfs.sh umountroot \
					live live-shutdown cryptsetup cryptsetup-shutdown \
					devmanager fake-hwclock fake-hwclock-shutdown logind \
					setup getty misc misc-essential mount recovery shutdown \
					boot enabled-services default \
					wait-oneshotd
				for dir in /var/lib/66/system/service/svc/*/ \
					/var/lib/66/system/.resolve/service/*/
				do
					[ -e "$dir" ] || continue
					grep -rq --binary-files=text wait-oneshotd "$dir" 2>/dev/null || continue
					basename "$dir"
				done
			} | awk 'NF && !seen[$0]++' | while read -r s; do
				[ -n "$s" ] || continue
				66 disable "$s" 2>/dev/null || true
				66 remove -P "$s" 2>/dev/null || true
				rm -rf "/var/lib/66/system/service/svc/$s" \
					"/var/lib/66/system/.resolve/service/$s" \
					/run/66/state/*/"$s" 2>/dev/null || true
			done
		fi

		rm -f "$stub" /etc/66/service/wait-oneshotd
		# Drop admin frontends that shadow stubs (e.g. invalid Type=bundle).
		for s in boot cryptsetup cryptsetup-shutdown default enabled-services \
			fake-hwclock fake-hwclock-shutdown getty live live-shutdown \
			misc misc-essential mount recovery setup shutdown
		do
			rm -f "/etc/66/service/$s"
		done
		;;
esac
#DEBHELPER#

case "$1" in
	configure)
		# Tree-named empty stubs must not stay enabled from older packages.
		if command -v 66 >/dev/null 2>&1; then
			for s in live setup boot enabled-services default mount getty \
				misc misc-essential recovery shutdown cryptsetup \
				cryptsetup-shutdown fake-hwclock fake-hwclock-shutdown \
				live-shutdown
			do
				66 disable "$s" 2>/dev/null || true
				66 remove -P "$s" 2>/dev/null || true
				rm -rf "/var/lib/66/system/service/svc/$s" \
					"/var/lib/66/system/.resolve/service/$s" \
					/run/66/state/*/"$s" 2>/dev/null || true
			done
		fi
		# Re-enable dh-66 services whose configure soft-failed (missing deps in
		# the middle of an ISO bootstrap). Safe to re-run.
		if [ -x /usr/lib/66-helper/66-helper ]; then
			/usr/lib/66-helper/66-helper dpkg-sync 2>/dev/null || true
		fi
		# oneshotd execve needs +x on remaining oneshot run files.
		for run in /var/lib/66/system/service/svc/*/run \
			/var/lib/66/system/service/svc/*/finish \
			/var/lib/66/system/service/svc/*/run.user \
			/run/66/state/*/*/run \
			/run/66/state/*/*/finish \
			/run/66/state/*/*/run.user
		do
			[ -f "$run" ] || continue
			chmod a+rx "$run" 2>/dev/null || true
		done
		mount -o remount,exec /run 2>/dev/null || true
		;;
esac
exit 0
