#!/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

                # 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

                if [ -f "$RAUC_BUNDLE_MOUNT_POINT/kernel.itb" ]; then
                        # check RAUC_SLOT_NAME to determine which slot is being installed
                        case "$RAUC_SLOT_NAME" in
                                rootfs.0)
                                    # update kernel itb for slot a (/dev/ubi0_0)
                                    echo "Installing kernel.itb to slot a"
                                    ubiupdatevol /dev/ubi0_0 "$RAUC_BUNDLE_MOUNT_POINT/kernel.itb"
                                    ;;
                                rootfs.1)
                                    # update kernel itb for slot b (/dev/ubi0_2)
                                    echo "Installing kernel.itb to slot b"
                                    ubiupdatevol /dev/ubi0_2 "$RAUC_BUNDLE_MOUNT_POINT/kernel.itb"
                                    ;;
                                *)
                                    echo "Unknown slot name: $RAUC_SLOT_NAME"
                                    exit 1
                                    ;;
                        esac
                fi
                ;;
        *)
                exit 1
                ;;
esac
exit 0
