#!/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=ext3
# KFR obsolete?
#BOOTLOADER_IMAGE=u-boot.imx
CA_CERTIFICATES="${OFFLINE_SYSTEM_DIR}/usr/share/ca-certificates.tar"
XTABLES="${OFFLINE_SYSTEM_DIR}/usr/lib/xtables.tar"

check_file ${SET_ATTR}
#check_file ${CA_CERTIFICATES}
#check_file ${XTABLES}

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

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

if [ -e /opt/fwu/system_offline/boot/exchange-bootloader.sh ]; then
 ( cd /opt/fwu/system_offline/boot && ./exchange-bootloader.sh )
fi

${UNMOUNT} /opt/fwu/system_offline

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

if [ "${COMMISSION}" != "TRUE" ]; then
  FIRMWARE_VERSION=$(${DEVICESTACK} -cfv)
  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...\\n"
  printf "commissioned\\n" > ${COMMISSIONED_FILE}
fi

# 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
printf "Running file system check on offline system.\\n"
MMC_SYSTEM_OFFLINE=$(cat /opt/extparam/offline_root_blkdev)
fsck.${FILESYSTEM_TYPE} -y ${MMC_SYSTEM_OFFLINE}
EXIT_CODE=$?
if ! [ ${EXIT_CODE} -eq 0 ]; then
  printf "Filesystemcheck returned non-zero (code: |%s|). Stopping.\\n" "${EXIT_CODE}"
  exit 3
fi

userdata_root_blk=$(cat /opt/extparam/userdata_root_blkdev)
if [ "${userdata_root_blk}" = "/dev/mmcblk1p1" ]; then
   printf "resetting /opt/extparam/userdata_root_blkdev\\n"
   printf "/dev/mmcblk0p4\\n" > /opt/extparam/userdata_root_blkdev
fi

# 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
