#!/bin/sh

ITB_NAME=zImage-fit
case "$1" in
        slot-pre-install)
                # Only rootfs needs overlay cleanup
                test "$RAUC_SLOT_CLASS" = "rootfs" || exit 0

                # List of files to remove from overlay
                FILES_TO_CLEAN="
                /usr/bin/update-firmware
                /usr/bin/guid
                /usr/bin/serialnum
                /usr/bin/cloud-comm
                /usr/bin/bg95-watchdog
                /etc/rauc/version
                /etc/init.d/gateway-log
                "

                for f in $FILES_TO_CLEAN; do
                    overlay_file="/overlay/upper$f"
                    if [ -e "$overlay_file" ]; then
                        echo "Removing overlay copy: $overlay_file"
                        rm -f "$overlay_file"
                    fi
                done
                ;;
        slot-post-install)
                # only rootfs needs to be handled
                test "$RAUC_SLOT_CLASS" = "rootfs" || exit 0
                LITB=$(find $RAUC_BUNDLE_MOUNT_POINT -name *.itb)

                if [ ! -d /boot ]; then
                        mkdir /boot
                fi

                # if configuration backup is found, restore it
                CONF_RESTORE="/data/sysupgrade.tar"
                if [ -f "$CONF_RESTORE" ]; then
                        EXCLUDE_FROM=""
                        if [ -f "$RAUC_BUNDLE_MOUNT_POINT/excluded-config-files.txt" ]; then
                                EXCLUDED_FILES=$(cat $RAUC_BUNDLE_MOUNT_POINT/excluded-config-files.txt)
                                echo "Excluding the following files from restore:"
                                echo "$EXCLUDED_FILES"
                                EXCLUDE_FROM="--exclude-from=$RAUC_BUNDLE_MOUNT_POINT/excluded-config-files.txt"
                        fi
                        echo "Restoring configuration"
                        tar -C $RAUC_SLOT_MOUNT_POINT $EXCLUDE_FROM -xzf "$CONF_RESTORE"
                        rm -f $CONF_RESTORE
                fi

                UPDATE_UBOOT_BIN='false'
                UPDATE_UBOOT_ENV='false'
                UPDATE_ITB='false'

                # mount boot partition read only
                mount -o ro /dev/mmcblk1p1 /boot

                if [ -f "$RAUC_BUNDLE_MOUNT_POINT/u-boot.bin" ]; then
                        echo Comparing $RAUC_BUNDLE_MOUNT_POINT/u-boot.bin and /boot/u-boot.bin
                        cmp -s $RAUC_BUNDLE_MOUNT_POINT/u-boot.bin /boot/u-boot.bin
                        if [ ! $? == 0 ]; then
                                echo u-boot.bin will be updated
                                UPDATE_UBOOT_BIN='true'
                        else
                                echo u-boot.bin is up-to-date
                        fi
                fi

                if [ -f "$RAUC_BUNDLE_MOUNT_POINT/uboot.env" ]; then
                        echo Comparing $RAUC_BUNDLE_MOUNT_POINT/uboot.env and /boot/uboot.env
                        cmp -s $RAUC_BUNDLE_MOUNT_POINT/uboot.env /boot/uboot.env
                        if [ ! $? == 0 ]; then
                                echo uboot.env will be updated
                                UPDATE_UBOOT_ENV='true'
                        else
                                echo uboot.env is up-to-date
                        fi
                fi

                if [ -f "$LITB" ]; then
                        echo Comparing $LITB and /boot/${ITB_NAME}_$RAUC_SLOT_BOOTNAME.itb
                        cmp -s $LITB /boot/${ITB_NAME}_$RAUC_SLOT_BOOTNAME.itb
                        if [ ! $? == 0 ]; then
                                echo ${ITB_NAME}_$RAUC_SLOT_BOOTNAME.itb will be updated
                                UPDATE_ITB='true'
                        else
                                echo ${ITB_NAME}_$RAUC_SLOT_BOOTNAME.itb is up-to-date
                        fi
                fi

                if [ $UPDATE_UBOOT_BIN == 'true' ] || [ $UPDATE_UBOOT_ENV == 'true' ] || [ $UPDATE_ITB == 'true' ]; then
                        # remount so we can write to the partition
                        mount -o remount,rw /boot
                        if [ $UPDATE_UBOOT_BIN == 'true' ]; then
                                echo Copying $RAUC_BUNDLE_MOUNT_POINT/u-boot.bin to /boot/u-boot.bin
                                cp -vf $RAUC_BUNDLE_MOUNT_POINT/u-boot.bin /boot/u-boot.bin
                        fi
                        if [ $UPDATE_UBOOT_ENV == 'true' ]; then
                                echo Copying $RAUC_BUNDLE_MOUNT_POINT/uboot.env to /boot/uboot.env
                                cp -vf $RAUC_BUNDLE_MOUNT_POINT/uboot.env /boot/uboot.env
                        fi
                        if [ $UPDATE_ITB == 'true' ]; then
                                echo Copying $LITB to /boot/${ITB_NAME}_$RAUC_SLOT_BOOTNAME.itb
                                cp -vf $LITB /boot/${ITB_NAME}_$RAUC_SLOT_BOOTNAME.itb
                        fi
                        sync
                fi

                umount /boot

                UBOOT_ENV_MIGRATION='false'
                if [ ! -f /overlay/rauc-uboot.env ]; then
                        UBOOT_ENV_MIGRATION='true'
                fi

                # restore uboot environment
                if [ $UBOOT_ENV_MIGRATION == 'true' ]; then
                        # modify the current /etc/fw_env.config file then restore the environment variables. rauc will
                        # need to properly update the variables in preparation to boot to the new slot.
                        echo Restore uboot environment variables
                        echo "/$RAUC_UBOOT_ENV_DIR/rauc-uboot.env 0 0x4000" > /etc/fw_env.config
                        fw_setenv BOOT_A_LEFT "$BOOT_A_LEFT"
                        fw_setenv BOOT_DEV "$BOOT_DEV"
                        fw_setenv BOOT_ORDER "$BOOT_ORDER"
                        fw_setenv SELECTED_SLOT "$SELECTED_SLOT"
                        fw_setenv BOOT_B_LEFT "$BOOT_B_LEFT"
                fi
                ;;
        *)
                exit 1
                ;;
esac
exit 0
