#!/bin/sh

# Contact: Pekka Lundstrom  <pekka.lundstrom@jollamobile.com>
#
# Copyright (c) 2014, Jolla Ltd.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# preinit plugin - get_bootstate
# This is part of /sbin/preinit and finds out why Jolla device booted up
# It will output "USER", "ACT_DEAD" or "TEST"
# If wakeup happened by usb or rtc, then we boot to ACT_DEAD, else USER
# jolla.test_mode in cmdline can override usb detection values

check_bogus=0
bootreason_str="Normal boot"
actdead_bootparam=""

if [ -f /var/lib/environment/actdead/bootparam.conf ]; then
    source /var/lib/environment/actdead/bootparam.conf
    actdead_bootparam=$ACTDEAD_PARAMETER_STRING
fi

if grep -q 'jolla.test_mode=USER' /proc/cmdline; then
    # Device is in QA test mode forcing USER
    BOOTSTATE="USER"
elif grep -q 'jolla.test_mode=ACT_DEAD' /proc/cmdline; then
    # Device is in QA test mode forcing ACT_DEAD
    BOOTSTATE="ACT_DEAD"
elif grep -q 'androidboot.mode=charger' /proc/cmdline; then
    BOOTSTATE="ACT_DEAD"
    bootreason_str="androidboot.mode=charger"
    check_bogus=1
elif [ ! -z "$actdead_bootparam" ] &&
     grep -q $actdead_bootparam /proc/cmdline; then
    # Device specific boot parameter
    BOOTSTATE="ACT_DEAD"
    bootreason_str=$actdead_bootparam
    check_bogus=1
else
    BOOTSTATE="USER"
fi

# Bootreason can be bogus (pwr down by low voltage peak or battery removal)
# We try to detect that and boot to USER in that case
# ACTDEAD boot would cause only shutdown if there wasn't real reason
if [ "$BOOTSTATE" == "ACT_DEAD" ] &&
   [ $check_bogus -eq 1 ] &&
   [ -f /var/log/systemboot.log ]; then
   # In uncontrolled shutdown we don't have log entry for shutdown
   tail -1 /var/log/systemboot.log | grep -q Shutdown
   if [ $? -ne 0 ]; then
       BOOTSTATE="USER"
       echo "x-no-time-stamp Startup:  preinit ignored $bootreason_str" >> /var/log/systemboot.log
   fi
fi
echo $BOOTSTATE
exit 0
