#!/bin/sh
# postinst script for TrutzBox-master-scripts-backuprestore
#
# see: dh_installdeb(1)

set -e

PACKAGE_NAME=trutzbox-master-scripts-backuprestore
BACKUP_ROOT=/var/lib/comidio/trutzbox/backup

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

case "$1" in
    configure)
        # feature reboot: require reboot after installation of this package
        # reason: make sure that ntpdate is not executed
        DPKG_MAINTSCRIPT_PACKAGE=trutzbox-master-scripts-backuprestore
        ##### Code Borrow Start
        # This code was borrowed from package "update-notifier" file "/usr/share/update-notifier/notify-reboot-required"
#        eval_gettext() {
#            if [ -x /usr/bin/gettext ]; then
#                echo $(gettext "$1")
#            else
#                echo "$1"
#            fi
#        }
#        export TEXTDOMAIN=update-notifier
#        export TEXTDOMAINDIR=/usr/share/locale
#
#        if [ "$0" = "/etc/kernel/postinst.d/update-notifier" ]; then
#            DPKG_MAINTSCRIPT_PACKAGE=linux-base
#        fi
#
        # Wake the applet up
#        echo "*** $(eval_gettext "System restart required") ***" > /var/run/reboot-required
#        echo "$DPKG_MAINTSCRIPT_PACKAGE" >> /var/run/reboot-required.pkgs
        ##### Code Borrow End

        # only write to rescue partition if it exists on this platform.
        # ARM (NanoPi R5C) has no RESCUE-OS partition yet (the rescue port is
        # a separate project) - the image build used to work around this with
        # a /tmp/.dont_write_to_rescue trigger file; the platform check makes
        # that crutch unnecessary while keeping the trigger for manual use.
        # Quiet on purpose: the line that used to be printed here read like an
        # error on every update of an ARM TrutzBox, and the list of removed
        # files on x64 like a loss (12.10.2026). Nothing is missing on ARM,
        # the reset there is the business of trutz-rescue.
        if [ ! -e /dev/disk/by-label/RESCUE-OS ]; then
            :
        elif [ ! -e /tmp/.dont_write_to_rescue ]; then
            # copy files for factory-reset, firmware-update and image-build to rescue-partition
            RESQ_ROOT=/tmp/resq
            if [ -z $( mount | grep "/dev/disk/by-label/RESCUE-OS" -q ) ]; then
                mkdir -p "${RESQ_ROOT}"
                mount "/dev/disk/by-label/RESCUE-OS" "${RESQ_ROOT}"
            fi;
            # initialize target directory, where the factory-reset-scripts are expected
            rm -rf  "${RESQ_ROOT}/etc/comidio/trutzbox-master-scripts-backuprestore/system-update.config.sh"
            mkdir -p "${RESQ_ROOT}/etc/comidio/trutzbox-master-scripts-backuprestore/"
            cp -a /etc/comidio/trutzbox-master-scripts-backuprestore/system-update.config.sh "${RESQ_ROOT}/etc/comidio/trutzbox-master-scripts-backuprestore/"
            rm -rf  "${RESQ_ROOT}/usr/lib/comidio/trutzbox"
            mkdir -p "${RESQ_ROOT}/usr/lib/comidio/trutzbox"
            cp -r /usr/lib/comidio/trutzbox/* "${RESQ_ROOT}/usr/lib/comidio/trutzbox"
            echo "Scripts for reset and firmware update renewed in the rescue system."
            umount "${RESQ_ROOT}"
            rm -rf "${RESQ_ROOT}"
        else
            rm /tmp/.dont_write_to_rescue
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
