#!/bin/sh

set -o errexit
set -o xtrace

FWU_STATUS_FILE="/opt/userdata/.fwu-status"
SET_ATTR="rfs_setattr.sh"
OFFLINE_SYSTEM_DIR="/opt/fwu/system_offline"
CA_CERTIFICATES="${OFFLINE_SYSTEM_DIR}/gira/usr/share/ca-certificates.tar"
SH="/bin/sh"

echo -n "started" > ${FWU_STATUS_FILE}

### BEGIN POST SCRIPT COMMANDS ###

echo "Extracting xtables.tar..."
cd  ${OFFLINE_SYSTEM_DIR}/gira/usr/lib
tar xf xtables.tar
rm xtables.tar
cd -

echo "Preparing attribute script..."
[ -x ${SET_ATTR} ] || /bin/chmod +x ${SET_ATTR}
cp ${SET_ATTR} ${OFFLINE_SYSTEM_DIR}

echo "Executing attribute script..."
# Hint: The errexit option does not affect subshells
(cd ${OFFLINE_SYSTEM_DIR} && ${SH} ${SET_ATTR} && rm -f ${SET_ATTR})

echo "Unpacking certificates"
if [ -f "${CA_CERTIFICATES}" ]
then
  printf "[post-script] Extracting ca-certificates.\\n"
  pushd "${OFFLINE_SYSTEM_DIR}/gira/usr/share"
    tar -xf ${CA_CERTIFICATES}
    rm -f ${CA_CERTIFICATES}
  popd
fi

echo "Finishing..."
umount /opt/fwu/system_offline

sync
sleep 1
sync
sleep 1
sync

# If this is the update during commissioning, this file needs to be created.
# It must not be empty.
[ -f /opt/extparam/.commissioned ] || echo "commissioned" > /opt/extparam/.commissioned

bootloader_image="/opt/fwu/u-boot.imx"
if [ -f "${bootloader_image}" ]
then
  echo "Trying to exchange bootloader..."
  if [ -e exchange-bootloader.sh ]
  then
    chmod +x exchange-bootloader.sh
    ./exchange-bootloader.sh /dev/mmcblk0 "${bootloader_image}"
    echo "Bootloader has been exchanged!"
  else
    echo "Script to exchange bootloader is missing!"
    exit 1
  fi
else
  echo "Bootloader image is missing!"
  exit 1
fi

echo "Checking bootscript..."
# The bootscript extends the bootargs to a cmdline usable by both the
# Android and the Buildroot system.

if [ -e /opt/extparam/boot.scr ]
then
  # Create hashsum for comparing
  if ! [ -e /opt/extparam/boot.scr.md5 ]
  then
    (cd /opt/extparam && md5sum boot.scr > boot.scr.md5)
  fi

  installed_bootscript_md5=$(head -n 1 /opt/extparam/boot.scr.md5 | cut -d " " -f 1)
  contained_bootscript_md5=$(head -n 1 /opt/fwu/boot.scr.md5 | cut -d " " -f 1)

  echo "Installed U-Boot script hash is ${installed_bootscript_md5}"
  echo "Contained U-Boot script hash is ${contained_bootscript_md5}"

  # Overwrite bootscript, if it diffs
  if ! [ "${installed_bootscript_md5}" == "${contained_bootscript_md5}" ]
  then
    echo "Overwriting U-Boot script..."
    cp /opt/fwu/boot.scr /opt/extparam/boot.scr
    chmod 600 /opt/extparam/boot.scr
    cp /opt/fwu/boot.scr.md5 /opt/extparam/boot.scr.md5
    chmod 600 /opt/extparam/boot.scr.md5
  else
    echo "No need to overwrite U-Boot script, skipping!"
  fi
else # Commissioning does not ship the bootscript, so copy anyways
  echo "No bootscript found, copying..."
  cp /opt/fwu/boot.scr /opt/extparam/boot.scr
  chmod 600 /opt/extparam/boot.scr
  cp /opt/fwu/boot.scr.md5 /opt/extparam/boot.scr.md5
  chmod 600 /opt/extparam/boot.scr.md5
fi

# This needs to be readable by others so the Android system can use it.
if [ -f /opt/extparam/GIG1LXKXIP_wlan0_settings.bin ]
then
  chmod 644 /opt/extparam/GIG1LXKXIP_wlan0_settings.bin
fi

# Check the current firmware version. If it is not an Android package,
# we want to trigger a factory reset for the reboot.
CURRENT_FW=$(cat /opt/extparam/booted_firmware_version)
# Get the major version number using 'cut'. Note that this tool it not
# symlinked on every device, i.e. on older Android packages.
CURRENT_FW_MAJOR=$(echo "${CURRENT_FW}" | busybox cut -d. -f1)
if ! [ "${CURRENT_FW_MAJOR}" = "3" -o "${CURRENT_FW_MAJOR}" = "2" ]
then
  # Enable the factory reset
  echo -n enabled > /opt/extparam/factory_reset_condition
  # Unset 'platform' in case it carries a non-default value from previous
  # installations of this package.
  fw_setenv platform
fi

# Set supported platforms in extparam
# The first one is the default platform to reset the device to after a
# factory reset.
echo "linux android" > /opt/extparam/supported_platforms

# Trigger new apps to be copied from template dir into Android system
rm -f /opt/userdata/android/data/app/.templates-copied

# If this is the update during commissioning, this file needs to be created.
# It must not be empty.
[ -f /opt/extparam/.commissioned ] || echo "commissioned" > /opt/extparam/.commissioned

sync

echo "Done!"

### END POST SCRIPT COMMANDS ###

echo -n "stopped" > ${FWU_STATUS_FILE}
echo "The post-script has successfully done its job."
exit 0
