#!/bin/sh
set -e

source /opt/fwu/ipmodule-functions

printf "started" > ${FWU_STATUS_FILE}

parse_arguments $1 $2 $3

SET_ATTR="rfs_regular_setattr.sh"
SET_ATTR_EXTRA="extra_packages_setattr.sh"
FILESYSTEM_TYPE=jffs2
BOOTLOADER_IMAGE=bl.img
CA_CERTIFICATES="${OFFLINE_SYSTEM_DIR}/usr/share/ca-certificates.tar"
XTABLES="${OFFLINE_SYSTEM_DIR}/usr/lib/xtables.tar"

check_file ${SET_ATTR}

check_executable ${SH}
check_executable ${CHMOD}
check_executable ${CHOWN}
check_executable ${MOUNT}
check_executable ${UNMOUNT}

# This identifies the offline system and sets the variables OFFLINE_KERNEL_BLK, FIRST_KERNEL_OFFSET, SECOND_KERNEL_OFFSET, etc.
identify_offline

printf "Preparing attribute script...\\n"
[ -x ${SET_ATTR} ] || ${CHMOD} 750 ${SET_ATTR}
cp ${SET_ATTR} ${OFFLINE_SYSTEM_DIR}

printf "Executing attribute script...\\n"
(cd ${OFFLINE_SYSTEM_DIR} && ${SH} ${SET_ATTR} && rm -f ${SET_ATTR})

printf "Checking for extra attribute script ... "
if [ -e "${SET_ATTR_EXTRA}" ]
then
  printf "found.\\n"
  [ -x ${SET_ATTR_EXTRA} ] || ${CHMOD} 750 ${SET_ATTR_EXTRA}
  mv ${SET_ATTR_EXTRA} ${OFFLINE_SYSTEM_DIR}
  printf "Executing extra attribute script...\\n"
  (cd ${OFFLINE_SYSTEM_DIR} && ${SH} ${SET_ATTR_EXTRA} && rm -f ${SET_ATTR_EXTRA})
else
  printf "nothing found. Skipping.\\n"
fi

if [ -e "${CA_CERTIFICATES}" ]
then
  printf "[post-script] Extracting ca-certificates.\\n"
  OLDPATH=$(pwd)
  cd "${OFFLINE_SYSTEM_DIR}/usr/share"
  tar -xf ${CA_CERTIFICATES}
  rm -f ${CA_CERTIFICATES}
  cd ${OLDPATH}
fi

if [ -e "${XTABLES}" ]
then
  printf "[post-script] Extracting xtables.\\n"
  OLDPATH=$(pwd)
  cd "${OFFLINE_SYSTEM_DIR}/usr/lib"
  tar -xf ${XTABLES}
  rm -f ${XTABLES}
  cd ${OLDPATH}
fi

# Install the default and backup kernel
printf "Writing first kernel to %s at %s...\\n" "${OFFLINE_KERNEL_MTD}" "${FIRST_KERNEL_OFFSET}"
nandwrite --markbad --pad --start=${FIRST_KERNEL_OFFSET} ${OFFLINE_KERNEL_MTD} ${KERNEL_UIMAGE}
printf "Writing second kernel to %s at %s...\\n" "${OFFLINE_KERNEL_MTD}" "${SECOND_KERNEL_OFFSET}"
nandwrite --markbad --pad --start=${SECOND_KERNEL_OFFSET} ${OFFLINE_KERNEL_MTD} ${KERNEL_UIMAGE}


# Clean up extra_packages*.tar
rm -f extra_packages_*.tar

if [ "${COMMISSION}" != "TRUE" ]; then
  FIRMWARE_VERSION=$(${DEVICESTACK} --current-firmware-version)
  cp ${DEVICECONFIG} ${DEVICECONFIG}.${FIRMWARE_VERSION}
  if [ -f ${DEVICECONFIG}.${FIRMWARE_VERSION} ]; then
    printf "Created backup file: %s.%s\\n" "${DEVICECONFIG}" "${FIRMWARE_VERSION}"
  else
    printf "Failed to create backup file: %s.%s\\n" "${DEVICECONFIG}" "${FIRMWARE_VERSION}"
  fi
fi

#if [ ! -f "${COMMISSIONED_FILE}" ]; then
#  printf "Disable commissioning mode... ${COMMISSIONED_FILE}\\n"
#  printf "commissioned\\n" > ${COMMISSIONED_FILE}
#fi

# Update bootloader and script only in commissioning mode; check if we need to update the boot script for firmware version lesser than 4
major_version=$(/opt/gira/bin/devicestack -cfv | cut -d . -f 1)
if [ "${COMMISSION}" == "TRUE" ]; then
  if [ -e /opt/fwu/system_offline/boot/exchange-bootloader.sh ]; then
   ( cd /opt/fwu/system_offline/boot && ./exchange-bootloader.sh )
  fi
elif [ $major_version -lt 4 ]; then
	# update bootloader script for firmware versions less than 4 b/c the kernel storage has been changed
  if [ -f /opt/fwu/system_offline/boot/boot.scr ]; then
	  dd if=/opt/fwu/system_offline/boot/boot.scr of=/dev/mtdblock5 bs=16k seek=15 2>/dev/null
  else
    dd if=/opt/fwu/system_offline/boot/u-boot.scr of=/dev/mtdblock5 bs=16k seek=15 2>/dev/null
  fi
fi


unmount_offline_system

# remove permissions from /opt/extparam for other users
${CHMOD} -R o-rwx /opt/extparam

# change owner of already existing logic engine files
if [ -e /opt/userdata/logicengine ]
then
  ${CHOWN} -R 500:500 /opt/userdata/logicengine/
fi

sync_buffer

# This 'stopped' within the FWU_STATUS_FILE is interpreted by the DeviceStack
# as "Everything is fine the Firmwareupdate completed successfully."
# So do not do anything below that shall mark the update as failure.
printf "stopped" > ${FWU_STATUS_FILE}
printf "The post-script has successfully done it's job and will now exit with return code 0.\\n"
exit 0
