#!/bin/sh
set -e

source /opt/fwu/ipmodule-functions

printf "started" > ${FWU_STATUS_FILE}

parse_arguments $1 $2 $3

BL_UPDATE="bl.img"
BS_UPDATE="bs.img"
BL_VERSION="bl.ver"
BS_VERSION="bs.ver"
SET_ATTR="rfs_regular_setattr.sh"
SET_ATTR_EXTRA="extra_packages_setattr.sh"

TARPIPE_POSTSCRIPT="tarpipe_postscript.sh"

check_file ${BL_UPDATE}
check_file ${BS_UPDATE}
check_file ${SET_ATTR}
check_file ${BL_VERSION}
check_file ${BS_VERSION}

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

if [ -e "${TARPIPE_POSTSCRIPT}" ]
then
  printf "Executing tarpipe postscript.\\n"
  ${CHMOD} 750 "${TARPIPE_POSTSCRIPT}"
  "./${TARPIPE_POSTSCRIPT}"
else
  printf "No tarpipe post-script, skipping.\\n"
fi

if [ -e "${RAWPIPE_POSTSCRIPT}" ]
then
  printf "Executing rawpipe postscript.\\n"
  ${CHMOD} 750 "${RAWPIPE_POSTSCRIPT}"
  "./${RAWPIPE_POSTSCRIPT}"
else
  printf "No rawpipe post-script, skipping.\\n"
fi

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

# 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_UZIMAGE}
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_UZIMAGE}

unmount_offline_system

printf "Checking boot updater ... "
if [ -e ${BOOT_UPDATER} ]
then
  printf "found.\\n"
  [ -x ${BOOT_UPDATER} ] || ${CHMOD} 750 ${BOOT_UPDATER}
  ${BOOT_UPDATER} --check-and-update --target-file=/dev/mtdblock5 --bootstrap-file=${BS_UPDATE} --bootstrap-version=$(cat ${BS_VERSION}) --bootloader-file=${BL_UPDATE} --bootloader-version=$(cat ${BL_VERSION})
else
  printf "not found. Skipping.\\n"
fi

# 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

# 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
