#!/bin/bash
### BEGIN INIT INFO
# Provides:          sysv-runonce
# Required-Start:    $local_fs
# Required-Stop:     $all
# Default-Start:     S 2
# Default-Stop:      0 6
# Short-Description: Script to prepare the system for one-time scripts
### END INIT INFO

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

DEBUG=0

LN="/bin/ln -s"
RM="/bin/rm -f"

. /lib/init/vars.sh
. /lib/lsb/init-functions

runlevel=`runlevel`
runlevel=${runlevel:2:1}      # Get the 3rd character of runlevel(8)'s output
if [ $runlevel = "k" ]; then  # An "unknown" runlevel returns 'k';
    runlevel="S"              # We're actually at runlevel 'S' for startup
fi

if [ $DEBUG = 1 ]; then
    RM="${RM} -v"
fi

case "$1" in
    start|stop)
        for program in /var/sysv-runonce/rc${runlevel}.d/*; do
            [ -x $program ] || break
            [ $DEBUG -eq 1 ] && echo "Running program: $program"
            $program $1
            if [ $? -eq 0 ]; then
                ${RM} $program
            fi
        done
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

exit 0
