#!/bin/sh
# Runit expects $SVDIR to be mutable, which isn't good to do in /etc/ for a
# number of good reasons, but is hard to change due to compatibility reasons.
# We try here to symlink the $SVDIR/**/supervise/ dirs to /run/ so that the
# system still works if the filesystem $SVDIR's on is (or becomes) readonly.
# This is a hacky solution, and the right one would be to put SVDIR in /run/.

runsv_runtime_dir="/run/runit"

# This may fail if /run/ also isn't writable, but that's not a fatal error on
# our part.
install -m 0755 -d "${runsv_runtime_dir}"

# Create shutdown control files (i.e. ln /etc/runit/{reboot,stopit} to /run/)
# In the unlikely event of /run/ being wedged readonly, you'll still be able to
# poweroff, just not reboot.
for control_file in reboot stopit; do
    [ -L "/etc/runit/${control_file}" ] && continue
    rm -fv "/etc/runit/${control_file}"
    ln -Trs "${runsv_runtime_dir}/${control_file}" "/etc/runit/${control_file}"
done