#!/usr/bin/sh

. /warewulf/config

echo "Warewulf prescript runner (/warewulf/wwprescripts)"
scriptdir=/warewulf/init.d
echo "Looking for prescripts in /warewulf/init.d/..."
ls -1 "${scriptdir}/" | while read -r name; do
    echo "Running prescript: ${name}..."
    sh "${scriptdir}/${name}"
done

init="${WWINIT}"
if [ -z "${init}" ]
then
    for candidate in /sbin/init /etc/init /bin/init
    do
        if [ -x "${candidate}" ]
        then
            init="${candidate}"
            break
        fi
    done
fi

if [ -n "${init}" ]
then
    echo
    echo "Running ${init}..."
    exec "${init}"
else
    echo "ERROR: init not defined and not found."
    echo "Rebooting in 1 minute..."
    sleep 60
    echo b > /proc/sysrq-trigger || /sbin/reboot -f
fi
