#!/bin/sh
set -e

case "$1" in
    configure|triggered)
	# what happens to this trigger when runit pkg is removed? maybe this
	# can be moved into runit-init and runit-run pkgs??
	if [ -z "${DPKG_ROOT:-}" ] ; then
		/lib/runit/trigger_sv setup
		/lib/runit/trigger_sv upgrade
	fi
    ;;
esac

chmod +x /etc/runit/core-services/*.sh

echo \
"#!/bin/sh
# system one time initialization tasks

PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /etc/runit/functions

if [ -x /lib/vdev/vdev_modprobe ]; then
        /lib/vdev/vdev_modprobe -b
fi

if [ -f /etc/runit/native.boot.run -o -n \"\$bootrun\" ] && [ -d /etc/runit/boot-run ]; then
        for f in /etc/runit/boot-run/*.sh; do
                [ -r \"\$f\" ] && . \"\$f\"
        done
else
        for script in /etc/rcS.d/S* ; do
                path=\$(realpath \"\$script\")
                name=\${path##*/}
                [ -d \"/etc/sv/\$name\" ] && continue
                [ -e \"/etc/runit/no.emulate.sysv.d/\$name\" ] && continue
                if [ \"\$name\" = 'alsa-utils' ]; then # arg for alsa-utils is the sound card
                        \"\$script\" start
                        continue
                fi
                # bootlogd's sysvinit script is too slow and make little sense with runit
                if [ \"\$name\" = 'bootlogd' ]; then
                        continue
                fi
                \"\$script\" start --force-sysv
        done
fi

# Start core services: one-time system tasks.
for f in /etc/runit/core-services/*.sh; do
	[ -r \$f ] && . \$f
done

# Now /run is mounted.
install -m000 /dev/null /run/runit.stopit
install -m000 /dev/null /run/runit.reboot

# That is it.  We do as little, as possible at stage1. Services, that do
# not have runscripts and invoked via init.d scripts are started at
# stage2. This way, daemon that hangs or otherwise misbehave do not
# block whole boot process.
#
# Thanks to Lorenz for suggestion.
" > /etc/runit/1
rm -f /etc/runit/1.dpkg-old 

echo \
"#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
SVDIR=/etc/service

mkdir -p /run/runit/supervise
mkdir -p /run/runit/sv
# mount will fail in unprivileged container, see #1030225
#mount -t tmpfs -o size=20M,mode=0755 runtimesv /run/runit/sv
#cp -a /etc/sv/* /run/runit/sv/  #TODO do this with 'CPSV_DIR=/run/runit/sv cpsv --sync'

if [ ! -z \$runitdir ] && [ -d /etc/runit/runsvdir/\"\$runitdir\" ]; then
        runsv_dir=\"\$runitdir\"
elif [ -r /etc/runit/default.runsvdir ]; then
        runsv_dir=\"\$(cat /etc/runit/default.runsvdir)\"
else
        runsv_dir=default
fi

# This script is also invoked in runit-systemd and runit-sysv packages.
# We must start sulogin and sysv scripts only if \`runit\` is current init
# system.
if [ -f /run/runit.stopit ] ; then
        # single mode
        if grep -q -w -i 'single' /proc/cmdline ; then
                chpst -P /sbin/sulogin -p /dev/tty1
        fi
        runsvchdir \"\$runsv_dir\" || runsvchdir default

        # Give sysv scripts 90 seconds (hardcoded in async-timeout source) to
        # start. If something goes wrong, they are detached from controlling
        # terminal.
        #
        # In this case tty1 would likely look ugly -- output from sysv scripts
        # is written over terminal session, but at least we would be able to
	# login.

        if [ \"\$runsv_dir\" != solo ] && [ ! -e /etc/runit/no.emulate.sysv ]; then
                /lib/runit/async-timeout /lib/runit/run_sysv_scripts '/etc/rc2.d'
        fi
else
        runsvchdir svmanaged
fi

if [ -x /sbin/vdevd ]; then
        while [ -n \"\$(lslocks -o command | grep \"^vdev_modprobe$\")\" ]; do
                sleep 0.1
        done
        /lib/vdev/vdev_print_input_devices

        while [ -n \"\$(lslocks -o command | grep \"^vdevd$\")\" ]; do
                sleep 0.1
        done
	if [ -n \"\$(ldconfig -p | grep libinput.so)\" ]; then
		/lib/vdev/vdev_wait
	fi
fi

exec env - PATH=\$PATH \\
runsvdir -P \"\${SVDIR}\" 'log: ...........................................................................................................................................................................................................................................................................................................................................................................................................'
" > /etc/runit/2
rm -f /etc/runit/2.dpkg-old

echo \
"#!/bin/sh
exec 2>&1

PATH=/sbin:/usr/sbin:/bin:/usr/bin

LAST=0

# While test -x is more natural, it does not consider file with
# executable bit set on 'noexec' filesystem as executable, and /run
# often is mounted as 'noexec'.
[ \$(stat -c %a /run/runit.reboot) = 100 ] && LAST=6

# unmount the eventfs fuse filesystem, if mounted
[ \"\$(stat -f -c '%T' /dev/metadata/udev/events/serial)\" = \"fuseblk\" ] && \\
        fusermount -u /dev/metadata/udev/events/serial

# used by /sbin/runlevel
printf \"\$LAST\" >/run/runit.runlevel

[ -x /etc/runit/rc.shutdown ] && /etc/runit/rc.shutdown

echo 'Waiting for services to stop...'
sv force-stop /etc/service/*
sv exit /etc/service/*

echo 'Running shutdown tasks...'
if [ -f /etc/runit/native.boot.run ] && [ -d /etc/runit/shutdown-run ]; then
        for f in /etc/runit/shutdown-run/*.sh; do
                [ -r \"\$f\" ] && . \"\$f\"
        done
else
        /lib/runit/run_sysv_scripts \"/etc/rc\$LAST.d\" stop
fi
" > /etc/runit/3
rm -f /etc/runit/3.dpkg-old

chmod +x /etc/runit/1
chmod +x /etc/runit/2
chmod +x /etc/runit/3

#DEBHELPER#
