#!/bin/sh
# Copyright (c) 2009 Alon Swartz <alon@turnkeylinux.org>

### BEGIN INIT INFO
# Provides:          inithooks
# Required-Start:    $local_fs $network $named
# Required-Stop:     $local_fs $network $named
# Default-Start:     2
# Default-Stop:      
# X-Interactive:     true
# Short-Description: Executes initialization hooks at boot time
# Description:       Executes firstboot and everyboot scripts
### END INIT INFO

DESC="Initialization hooks"
NAME=inithooks

# Check if VT's are supported
fgconsole >/dev/null 2>&1 && INITHOOKS_CHVT=y

. /lib/lsb/init-functions

[ -r /etc/default/inithooks ] && . /etc/default/inithooks

# don't run the hooks if noinithooks is passed as a boot param
for x in $(cat /proc/cmdline); do
    if [ $x = "noinithooks" ]; then
        exit 0
    fi
done

case "$1" in
  start)
    log_begin_msg "Starting $DESC"
    if [ "$INITHOOKS_CHVT" ]; then
        setterm -blank 0
        FGCONSOLE=$(fgconsole)
        openvt -f -c 8 -s -w -- ${INITHOOKS_PATH}/run
        chvt $FGCONSOLE
    else 
        $INITHOOKS_PATH/run
    fi
    log_action_end_msg $?
    ;;

  stop)
    exit 0
    ;;

  restart|reload|force-reload)
    echo "Error: argument '$1' not supported" >&2
    exit 3
    ;;

  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start}" >&2
    exit 1
    ;;
esac

exit 0
