#!/bin/sh
POWER_LOG=/tmp/power.log
CHG_INFO=$(cat /sys/module/musb_hdrc/parameters/charger_info)
KERN_INFO=$(uname -n|grep "aiv8167-rockman-emmc" 2>&1|wc -l)
VARIANT=$(cat /etc/versions/variant)
TARGET=$(cat /etc/versions/target)
#if [ x"$VARIANT" = x"user" -a x"$TARGET" = x"MREL" ]; then
#  POWER_LOG=/dev/null
#fi
touch $POWER_LOG
# enable OTG

if [ "$CHG_INFO" -eq "3" ]; then
  echo change to hostmode
  echo idle > /sys/devices/platform/mt_usb/swmode
  sleep 1
  echo host > /sys/devices/platform/mt_usb/swmode
  sleep 1
fi

# copy app file
if [ -e /usr/sony/bin/sonyapp-copylink ]; then
  echo "SACL:" >>$POWER_LOG
  /usr/sony/bin/sonyapp-copylink 1>>$POWER_LOG 2>&1
fi
# sic
if [ -e /usr/bin/sic ]; then
  echo "SIC:" >>$POWER_LOG
  /usr/bin/sic 1>>$POWER_LOG 2>&1
fi
# thermal config
if [ -e /etc/thermal/thermal.conf -a -e /data/thermal ]; then
  cp /etc/thermal/thermal.conf /data/thermal/
  systemd stop thermal_manager.service
  systemd start thermal_manager.service
fi
# LED config
if [ x"$TARGET" = x"PVT" -o x"$TARGET" = x"MREL" ]; then
  if [ ! -e /etc/versions/DVT ]; then
    echo 1 > /sys/class/leds/green/brightness
    echo 1 > /sys/class/leds/red/brightness
    LED_CTRL="softled"
  fi
fi
echo "1st suspend" >>$POWER_LOG
if [ -e /etc/autostart ]; then
  att=$(cat /etc/autostart|tr -cd '0-9')
  if [ x"$att" = x ]; then att=30; fi
  echo "autostart +$att" >>$POWER_LOG
  echo "+$att" > /sys/class/rtc/rtc0/wakealarm
  sleep 1
fi
sync
echo mem > /sys/power/state
# start weston if not
if ! systemctl status weston.service | grep -qE 'active\s+\(running\)'; then
  echo "start weston" >>$POWER_LOG
  systemctl start weston.service
fi
/usr/bin/cpu_mode full
/bin/power_manage usbreset rtcon safereboot $LED_CTRL 1>>$POWER_LOG 2>&1

