# Reduce default thread stack size for processes forked from this context.
# (by default this value is 8192 (kB))
ulimit -s 1024

dev_mode_enabled=0
read roku_dev_mode rest < /proc/cmdline
if [ "$roku_dev_mode" = 'dev=1' ]
then
    dev_mode_enabled=1
fi

# Developer mode command insertion or full autostart replacement:
if [ $dev_mode_enabled = 1 ] && [ -f /nvram/autostart ]
then
    echo
    echo "=== AUTOSTART from /nvram/autostart"
    source /nvram/autostart
    # allowed to return here
fi

APP_CORE_PATTERN=core

if [ $dev_mode_enabled = 1 ] && [ -f /etc/autostart-dev ]
then
    echo
    echo "=== AUTOSTART from /etc/autostart-dev"
    source /etc/autostart-dev
    # allowed to return here
fi

source /etc/aqpq_script

# Force startup daemon to run only once.
if [ -e /bin/ZipMountServer ]; then
    if [ $(pidof ZipMountServer | wc -c) = 0 ]; then
        /bin/ZipMountServer
    fi
fi

# Does the user want a core server?
#

COREIP=`sed -n 's/.*coreserver=\([^ ]*\).*/\1/p' /proc/cmdline`
if [ -n "$COREIP" ]; then
 echo Using local coreserver
 COREMOUNT=/tmp/cores
 umount $COREMOUNT
 mkdir -m 0777 -p $COREMOUNT
 COREEXPORT=`sed -n 's/.*coreexport=\([^ ]*\).*/\1/p' /proc/cmdline`
 if [ -z "$COREEXPORT" ]; then
  COREEXPORT=/opt/nfsmounts/cores
 fi
 mount -t nfs -o nolock $COREIP:$COREEXPORT $COREMOUNT
 APP_CORE_PATTERN=$COREMOUNT/%e.%t
 ulimit -c unlimited
fi

# Load the Bluetooth MAC address (if hci0 exists)
[ -d /sys/class/bluetooth/hci0 ] && setbtmac &

echo Starting the application...
su app -c "mkdir -p /tmp/app_perf"
cat /proc/uptime > /tmp/app_perf/start_app

while [ 1 ]; do
    # Tell the MCU what time it is:
    hwclock -w

    echo "/etc/autostart: app begin" > /dev/kmsg

    echo "$APP_CORE_PATTERN" >/proc/sys/kernel/core_pattern # make sure core dumps are enabled
    # FW-15149: QWS_DISPLAY environment variable instead of command line args
    export QWS_DISPLAY="directfb:flip=wait,onsync:nopartialflip:bgcolor=#000000:forcepremultiplied"
    $APP_DEV_ENVR Application -qws $APP_DEV_ARGS

    if [ ! -f /tmp/restart-application ]; then
        echo "Application exit without intent to restart" > /dev/kmsg

        # the command wdog returns the status of the watchdog when invoked without parameters
        wdt=`wdog | grep -o -E 'watchdog timeout\s+[0-9]+\s+sec'`

        if [ $? -eq 0 ]; then
            wd_timeout=`echo $wdt | awk '{print $3}'`
        
            if [ $wd_timeout -eq 0 ]; then
                # the command wdog can set the watchdog parameters
                echo "Setting watchdog to [timeout=8,   pretimeout=6]" > /dev/kmsg
                wdog 8 6
            else
                echo "Watchdog already set to $wd_timeout" > /dev/kmsg
            fi
        fi

        break
    fi

    # Checking if application crashed before suspend magic
    core_pattern=`cat /proc/sys/kernel/core_pattern`
    if [ $core_pattern != "-" ]; then
        echo "Application crashed during restart"
        echo "Application crashed during restart" > /dev/kmsg
        echo b > /proc/sysrq-trigger
        break
    fi

    echo "/etc/autostart: app done" > /dev/kmsg

    # Make the frontpanel led blink
    roku_led frontpanel booting
    # Tell the ZipMountServer to clear all mounts.
    plethora msg-send /zip-client '{ "action":"clear" }' 4 1024
    plethora msg-receive /zip-server
    killall dbgserver
    killall -9 WLCommandRunner
    killall -9 mongoose
    # Remove PlayReady session keys
    rm -rf /tmp/playready
    /bin/suspend.sh
done
