#!/bin/sh
#
# Script to get the current runstate and bootreason
# Default output:
#   line#1: run state (USER/ACT_DEAD/TEST/LOCAL/MALF/UNKNOWN)
#   line#2: boot reason (pwr_key,...)
#
# Command line switches:
#   -r or --runstate for getting only runstate
#   -b or --bootreason for geetting only bootreason
#
#

RUNSTATE="UNKNOWN"
BOOTREASON="unknown"
set +e

# get runstate from dsme
if [ -r /var/lib/dsme/saved_state ]; then
    RUNSTATE=`cat /var/lib/dsme/saved_state`
fi

# get bootreason from /proc/cmdline
fgrep -q 'bootreason=' /proc/cmdline
if [ $? -eq 0 ]; then
    BOOTREASON=`sed 's/^.*bootreason=\([^ ]\+\).*$/\1/' /proc/cmdline`
fi

# print them out
if [ "x$1" = "x-r" -o "x$1" = "x--runstate" ]; then
    echo $RUNSTATE
elif [ "x$1" = "x-b" -o "x$1" = "x--bootreason" ]; then
    echo $BOOTREASON
else
    echo $RUNSTATE
    echo $BOOTREASON
fi
