#!/bin/sh
# Switches the manufacturer's failback on again when the installation of the update is complete (or has given up):
# no update is pending, dpkg is clean and the firmware installer left nothing behind. Called by eurogard-rosp-update
# when it is done and by eurogard-failsafe-commit.timer. Does nothing if the failback was not switched off by us. Never fails.
. "$(dirname "$0")/common.sh"

[ -e "${PENDING}" ] || exit 0

if update_pending; then
    exit 0
fi

status=$(sed -n 's/.*"status":"\([^"]*\)".*/\1/p' "${ROSP_DIR}/result.json" 2> /dev/null)
case "${status}" in
    failed|gave_up|lock_timeout)
        failback_restore "firmware update ${status}"
        exit 0
        ;;
esac

if [ -e /rosp-archives ]; then
    # the firmware installer of the manufacturer has not finished (or was cut off): the update unit resumes it
    exit 0
fi

if ! dpkg_clean; then
    exit 0
fi

failback_restore "installation complete"
exit 0
