#!/bin/sh -x

# emulate runlevel transitions on upstart
# in case initctl gets stuck, sleep and force shutdown

if [ "x$1" = "x-t" ]
then
        shift; shift;
fi

case $1 in
        2)
        echo -n USER > /var/lib/dsme/saved_state
        touch /tmp/reboot
        initctl start --no-wait shutdown
        sleep 15
        /etc/init.d/minireboot
        ;;
        5)
        echo -n ACT_DEAD > /var/lib/dsme/saved_state
        touch /tmp/reboot
        initctl start --no-wait shutdown
        sleep 15
        /etc/init.d/minireboot
        ;;
        0)
        touch /tmp/shutdown
        initctl start --no-wait shutdown
        sleep 15
        /etc/init.d/minishutdown
        ;;
        6)
        dsmetool -b && sleep 5
        touch /tmp/reboot
        initctl start --no-wait shutdown
        sleep 15
        /etc/init.d/minireboot
        ;;
esac

