#!/bin/bash

#UUIDs
CERTS_UUID=fc7a83b1-e3f9-451a-a6f7-5db07fec397f
ROOTFS_UUID=4be968ba-b0f2-45c7-b571-18ef21074ee3
DATA_UUID=ae735454-aa32-440a-995f-d5d17bd97fb0

LIVE_DIR=/tmp-live/
CERTS_DIR=/tmp-certs/
DATA_DIR=/tmp-data/

clear_certs=false
clear_data=false

serial_number() {
  echo `dmidecode -s system-serial-number | awk '{print substr($1,6)}'`
}

### check stack ###

mount UUID=${DATA_UUID} ${DATA_DIR}
RECOVERY_STACK=`cat ${DATA_DIR}recovery.env 2>/dev/null`

if [[ $RECOVERY_STACK = "staging" ]]; then
  PCASTER_URL=https://plasmacaster-staging.kaijubox.com
  SERIAL_NO=`serial_number`-staging
else
  PCASTER_URL=https://plasmacaster.kaijubox.com
  SERIAL_NO=`serial_number`
fi
umount UUID=${DATA_UUID}

### check stack end ###

EVENT_URL=$PCASTER_URL/hosts/r$SERIAL_NO/event
BOOT_URL=$PCASTER_URL/hosts/r$SERIAL_NO/boot

post_info_msg() {
  curl -f -H 'Content-Type: application/json' --data "{\"level\": \"info\", \"name\": \"recovery\",  \"message\": \"$1\"}" -X PUT $EVENT_URL
  sleep 2
}

post_error_msg() {
  curl -f -H 'Content-Type: application/json' --data "{\"level\": \"error\", \"name\": \"recovery\",  \"message\": \"$1\"}" -X PUT $EVENT_URL
  sleep 2
}

should_reset() {
  
  # always execute the generic command first
  # never execute reboot in a generic command or you'll end up in a loop
  if [[ -n "$generic" ]]; then
    echo "[`date`] Executing generic command :  $generic" | tee -a /var/log/rae-reset.log
    post_info_msg "[`date`] Executing generic command :  $generic"
    eval $generic
  fi

  # check for FW update optout
  mount UUID=${DATA_UUID} ${DATA_DIR}
  if [ -f "${DATA_DIR}settings/system/fw-optout" ]; then
    if [[ "$fw_force" = "true" ]]; then
      echo "[`date`] Forcing firmware update" | tee -a /var/log/rae-reset.log
      post_info_msg "[`date`] Forcing firmware update"
      rae-itx-lcd 1= "Forcing FW" 2= "Update"
    else
      echo "[`date`] System excluded from FW updates" | tee -a /var/log/rae-reset.log
      post_info_msg "[`date`] System excluded from FW updates"
      rae-itx-lcd 1= "FW Updates" 2= "Disabled"

      if [ -f "${DATA_DIR}reset.rae" ]; then
        rm -rf ${DATA_DIR}reset.rae
        echo "[`date`] Removing reset.rae file due to FW opt-out" | tee -a /var/log/rae-reset.log
        post_info_msg "[`date`] Removing reset.rae file due to FW opt-out"
      fi
      umount UUID=${DATA_UUID} 2>/dev/null || /bin/true
      return 0
    fi
  fi
  umount UUID=${DATA_UUID} 2>/dev/null || /bin/true

  # always set the version flag even if not needed e.g version=none
  if [[ -n "$version" ]]; then
    beep -f 659 -l 460 -n -f 784 -l 340
    echo "[`date`] Resetting image to version : $version, clear data($clear_data), clear certs($clear_certs)" | tee -a /var/log/rae-reset.log
    post_info_msg "[`date`] Resetting image to version : $version, clear data($clear_data), clear certs($clear_certs)"
    /bin/rae-docker-recovery $version $clear_data $clear_certs
  else
    rm -rf ${DATA_DIR}reset.rae
    echo "[`date`] Removing reset.rae file. No image installation requested" | tee -a /var/log/rae-reset.log
    post_info_msg "[`date`] Removing reset.rae file. No image installation requested"
    rae-itx-lcd 1= "Rebooting" 2= "Please wait"
    sleep 1; reboot
  fi
}
