#!/bin/bash

# if the USB installer is detected, launch it
source /bin/rae-check-installer

# load common variables and functions
source /bin/rae-generic-recovery

# copy recovery version to /data
mount UUID=${DATA_UUID} ${DATA_DIR}
mkdir -p ${DATA_DIR}settings/system
cp /etc/rae-recovery-release ${DATA_DIR}settings/system/
umount UUID=${DATA_UUID}

## Request Reset With Button ##

#export reset button on GPIO86
if [ -d "/sys/class/gpio/gpio86/" ]; then
  echo 86 > /sys/class/gpio/unexport
fi
echo 86 > /sys/class/gpio/export

RESET_SET=0

# check if reset was requested with reset button
if [ -d "/sys/class/gpio/gpio86/" ]; then
  RESET_SET=`cat /sys/class/gpio/gpio86/value`

  if [ "$RESET_SET" -eq "0" ]; then
    source /etc/reset-version
    clear_certs=false
    clear_data=true

    echo "[`date`] Reset requested with reset button" | tee -a /var/log/rae-reset.log
    post_info_msg "[`date`] Reset requested with reset button"
    rae-itx-lcd 1= "Beginning" 2= "Factory Reset"
    echo 86 > /sys/class/gpio/unexport
    n=3000; while [ $n -gt 400 ]; do beep -f $n -l 5; n=$((n*97/100)); done
    should_reset
  fi
fi

if [ -d "/sys/class/gpio/gpio86/" ]; then
  echo 86 > /sys/class/gpio/unexport
fi

## Request Reset With Button End ##

# check if reset was requested with online hook
connect_timeout=3
max_time=5
retry=3
retry_delay=1
retry_max_time=15
rae_version=$( cat /etc/reset-version 2>/dev/null)
recovery_version=$(cat /etc/rae-recovery-release | head -n 1 2>/dev/null)

HTTP_RESPONSE=$(curl -m $max_time -s \
   --connect-timeout $connect_timeout \
   --retry $retry \
   --retry-delay $retry_delay \
   --retry-max-time $retry_max_time \
   --header "X-RAE-Ver: $rae_version" \
   --header "X-RAE-Recovery: $recovery_version" \
   -w "\nHTTPSTATUS:%{http_code}" $BOOT_URL)

#get status code from HTTP_RESPONSE
STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
printf "\nRAE reset status code : $STATUS\n"

#get body from HTTP_RESPONSE
BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')

#translate key=value pairs to environment variables
eval $BODY

mount UUID=${DATA_UUID} ${DATA_DIR}

case "$STATUS" in
  200)
    echo $BODY > ${DATA_DIR}reset.rae
    rae-itx-lcd 1= "Updating to" 2= "ver ${version#*:}"
    umount UUID=${DATA_UUID}
    should_reset
  ;;
  *)
    # do not do anything
  ;;
esac

# reset was requested with local file
if [ -f "${DATA_DIR}reset.rae" ]; then
  echo "[`date`] Reset requested with reset.rae file" | tee -a /var/log/rae-reset.log
  post_info_msg "[`date`] Reset requested with reset.rae file"
  source ${DATA_DIR}reset.rae
  rae-itx-lcd 1= "Updating to" 2= "ver ${version#*:}"
  umount UUID=${DATA_UUID} 2>/dev/null || /bin/true
  should_reset
fi
umount UUID=${DATA_UUID} 2>/dev/null || /bin/true

# No recovery request detected. Just boot to live image #
echo "Booting Live Image"

# load live kernel and initrd
mount UUID=${ROOTFS_UUID} /mnt

# transfer the reset log to live partition
cp -rfp /var/log/rae-reset.log /mnt/var/log/rae-reset.log

# load the live kernel
rae-itx-lcd 1= "Booting..." 2= "Please wait"
kexec -l /mnt/vmlinuz --initrd=/mnt/initrd.img --append="root=UUID=${ROOTFS_UUID} ro net.ifnames=0 biosdevname=0 video=off elevator=deadline console=tty0 console=ttyS0,115200"

# Unmount live partition
umount UUID=${ROOTFS_UUID}

# Go. Boot to live image!
kexec -e

echo "[`date`] Critical ERROR: Invalid live partition" | tee -a /var/log/rae-reset.log
post_error_msg "[`date`] Critical ERROR: Invalid live partition"
exit 1
