#!/bin/sh

PREREQ=""

prereqs()
{
	echo "$PREREQ"
}

case $1 in
	prereqs)
		prereqs
		exit 0
		;;
esac

# Create non-empty symlinked files in temporary directories under /run.
# bilibop-specific temporary directory (in /run), and populate it.

### BEGIN ###

if [ -f /etc/udev/udev.conf ]; then
	. /etc/udev/udev.conf
fi
udev_root="${udev_root:-/dev}"
udev_root="${udev_root%/}"

if [ -f ${rootmnt}/etc/bilibop/bilibop.conf ]; then
	. ${rootmnt}/etc/bilibop/bilibop.conf
fi
BILIBOP_RUNDIR="/run/${BILIBOP_COMMON_BASENAME:=bilibop}"

mkdir -p "${BILIBOP_RUNDIR}"

# Create a fake device map (optional, but enabled by default):
[ "${BILIBOP_RULES_FAKE_DEVICE_MAP}" = "false" ] ||
echo "(hd0)	${udev_root}/${BILIBOP_COMMON_BASENAME}/disk" >${BILIBOP_RUNDIR}/grub-device.map

# This last action is not absolutely necessary, but avoids some udev complaints.
# The goal is to create a '70-persistent-*.rules' file in /run/udev/rules.d but
# only if the corresponding one in /etc/udev/rules.d is a symlink pointing to the
# one in /run/udev/rules.d:
ETC_RULES_DIR="${rootmnt}/etc/udev/rules.d"
RUN_RULES_DIR="/run/udev/rules.d"
# But be sure the directory exists:
[ -d "${RUN_RULES_DIR%/*}" ] || mkdir ${RUN_RULES_DIR%/*}
[ -d "${RUN_RULES_DIR}" ] || mkdir ${RUN_RULES_DIR}
for rules in cd.rules net.rules; do
	persistent="${ETC_RULES_DIR}/70-persistent-${rules}"
	unpersistent="${RUN_RULES_DIR}/70-persistent-${rules}"
	if [ -h "${persistent}" -a "$(readlink -f ${persistent})" = "${unpersistent}" ]; then
		echo "# 70-(un)persistent-${rules}" >${unpersistent}
	fi
done

:
### END ###
