#!/bin/bash

# cron job to run this until the whole fleet is updated

# latest recovery version
LOCAL_VERSION="1"
LOCAL_MAJOR_VERSION="0"
LOCAL_MINOR_VERSION="2"

PCASTER_URL=https://plasmacaster.kaijubox.com
RECOVERY_UUID=2767b855-a084-4ac5-9238-1c2529d98214

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

SERIAL_NO=`serial_number`
# the DMI table is not accessible from chroot, use the hostname
if test -z ${SERIAL_NO}; then
  HOSTNAME=`hostname`
  SERIAL_NO="${HOSTNAME:1}"
fi

EVENT_URL=${PCASTER_URL}/hosts/r${SERIAL_NO}/event

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
}

source /data/settings/system/rae-recovery-release 2>/dev/null

if [[ ${LOCAL_VERSION} == ${VERSION} ]] &&
   [[ ${LOCAL_MAJOR_VERSION} == ${MAJOR_VERSION} ]] &&
   [[ ${LOCAL_MINOR_VERSION} == ${MINOR_VERSION} ]]; then
   echo "Recovery system up-to-date. Version ${LOCAL_VERSION}.${LOCAL_MAJOR_VERSION}.${LOCAL_MINOR_VERSION}"
   exit 0
else
    echo "Recovery system out-of-date. Current Version : ${VERSION}.${MAJOR_VERSION}.${MINOR_VERSION}. New Version ${LOCAL_VERSION}.${LOCAL_MAJOR_VERSION}.${LOCAL_MINOR_VERSION}"
    post_info_msg "[`date`] Recovery system out-of-date. Current Version : ${VERSION}.${MAJOR_VERSION}.${MINOR_VERSION}. New Version ${LOCAL_VERSION}.${LOCAL_MAJOR_VERSION}.${LOCAL_MINOR_VERSION}"
fi

# check if Network Agent is in test
NA_STATUS=`curl -X GET http://localhost:8888/REST/v1/agent/status 2>/dev/null`
if [[ ${NA_STATUS} == "INTEST" ]]; then
   echo -e "\nNetwork agent is in test. Exiting recovery system update"
   exit 0
else
   echo -e "\nNetwork Agent status : ${NA_STATUS}"
fi

# just in case
umount /dev/disk/by-uuid/${RECOVERY_UUID} 2>/dev/null; sleep 1;
mkdir -p /data/settings/system/

# begin the update
echo "Updating recovery system from rootfs."
post_info_msg "[`date`] Updating recovery system from rootfs."

# notify the user
beep -f 659 -l 460 -n -f 784 -l 340 &>/dev/null

# mount the recovery partition
mount UUID=${RECOVERY_UUID} /mnt/ ; sleep 1;
MOUNT_STATUS=$?

if test ${MOUNT_STATUS} -ne 0; then
  echo "[`date`] RECOVERY UPDATE [ERROR] : Mounting ${RECOVERY_UUID} on /mnt failed [${MOUNT_STATUS}]. Aborting recovery system update."
  post_error_msg "[`date`] RECOVERY UPDATE [ERROR] : Mounting ${RECOVERY_UUID} on /mnt failed [${MOUNT_STATUS}]. Aborting recovery system update."
  umount /dev/disk/by-uuid/${RECOVERY_UUID} 2>/dev/null; sleep 1;
  exit 0
fi

# use the static IP directly to avoid DNS failures
# chroot /mnt/ /bin/bash -c 'curl http://recovery.raehub.com/update/deploy-recovery-cloud | bash'
chroot /mnt/ /bin/bash -c 'curl http://52.71.224.4/update/deploy-recovery-cloud | bash'
CHROOT_STATUS=$?

if test ${CHROOT_STATUS} -ne 0; then
  echo "[`date`] RECOVERY UPDATE [ERROR] : chroot command failed [${CHROOT_STATUS}]. Aborting recovery system update."
  post_error_msg "[`date`] RECOVERY UPDATE [ERROR] : chroot command failed [${CHROOT_STATUS}]. Aborting recovery system update. "
  umount /dev/disk/by-uuid/${RECOVERY_UUID} 2>/dev/null; sleep 1;
  exit 0
fi

# don't forget to clean up
umount /dev/disk/by-uuid/${RECOVERY_UUID} 2>/dev/null; sleep 1;

# update completed
echo "Recovery system updated from rootfs. Rebooting system."
beep -f 659 -l 460 -n -f 784 -l 340 &>/dev/null
post_info_msg "[`date`] Recovery system updated from rootfs. Rebooting system."
sleep 1

# wave goodbye
reboot
