#!/bin/sh

. /etc/utils/shell-utils.sh
. /etc/init.d/splash-config.sh

DEBUG=0

case "$1" in
  start)
    echo ">>> Starting splash menu..."
    # check for /dev/fb0 device.
    while [ 1 ]; do
        if [ ! -e /dev/dri/card0 ] ; then
            log "NO /dev/dri/card0 device found! retrying..."
            sleep 0.1
            continue
        fi
        break
    done
    log "/dev/dri/card0 found! Starting splash utility..."
    ${SPLASH_UTILITY_PATH}/splash -d -i ${SPLASH_UTILITY_PATH}/splash.ini
    to=0
    while [ $to -lt 3000 ] ; do
        if [ -p ${SPLASH_PIPE} ] ; then
            break;
        fi
        to=$((to+100))
        usleep 100000
    done
    ${SPLASH} menu_off
    log "Starting splash..."
    #echo "Done."
    log "Done."
  ;;

  switch_root_restart)
    touch /ram/splash_switch_root
    to=0
    while [ $to -lt 3000 ] ; do
        if [ -p ${SPLASH_PIPE} ] ; then
            break;
        fi
        to=$((to+100))
        usleep 100000
    done
    ${SPLASH} menu_off
    log "Restarting splash after switch_root..."
    log "Done."
  ;;

  stop)
    #echo -n "Stopping splash..."
    log "Stopping splash..."
    ${SPLASH} esc
    killall splash >/dev/null 2>/dev/null
    to=0
    while [ $to -lt 60000 ] ; do
        splash_pid=$(pgrep -x splash)
        if [ "$splash_pid" == "" ] ; then
            break
        fi
        to=$((to + 100))
        usleep 100000
    done

    if [ -d /splash ] ; then
        rm /splash/splash.pipe
        umount /splash
    fi
    echo disabled > ${NO_SPLASH}
    echo "Done."
    log "Done."
  ;;

  *)
    echo "Usage: /etc/init.d/splash {start|stop}"
    log "Usage: /etc/init.d/splash {start|stop}"
    exit 1
    ;;
esac

exit 0
