#!/bin/sh

DAEMON_cvboot=/opt/boot.elf

# loglevel deaktivieren fuer serielle schnittstelle
echo "0" > /proc/sys/kernel/printk

# Action
case "$1" in
    start)
        set -e
        if [ -d "/home/user/update" ] ; then
            echo "Found update in /home/user/update - copy to /"
            cp -r -f /home/user/update/* / &&
            rm -r -f /home/user/update
        fi &&
        if [ ! -f $DAEMON_cvboot ]; then
            echo "Error: can't start boot-sw: file $DAEMON_cvboot not found"
            exit "1"
        fi
        ps | awk '!/awk/&&/boot.elf/{print $0; exit 1}'
        if [ $? -eq 1 ]; then
        	echo boot.elf already running!
        	exit 0
        fi
        echo "You must call fflush(stdout); in your C program to see stdout!!!!" > /home/user/mbm.info_stderr.txt
        start-stop-daemon -S -b -x /bin/ash -- -c "$DAEMON_cvboot &> /home/user/mbm.stderr.log"
        echo "done."
        ;;
    stop)
        # stoppe prtsoft
        start-stop-daemon -K -x $DAEMON_cvboot
        echo -e "\001TD\"boot-sw stopped.\n \";1;1;0\027" > /dev/ttyO1
        ;;
    startprtsoft)
        set -e
        if [ ! -f $DAEMON_cvboot ]; then
            echo "Error: can't start boot-sw: file $DAEMON_cvboot not found"
            exit "1"
        fi
        start-stop-daemon -v -S -b -x /bin/ash -- -c "$DAEMON_cvboot -q &> /home/user/mbm.stderr.log"
        echo "done."
        ;;
    restart)
        echo "Restarting prtsoft.."
        $0 stop
        $0 start
        echo "done."
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

exit 0
