#!/bin/sh

#
# Configuration reset script for spxsysconf
#

reset() {
    # Redo the identifiers first, these are created by an earlier boot script
    # but as we need to reset the information we need to re-run the boot script
    rm -f /etc/hostname /etc/spinetix/identifiers \
        /etc/apache2/conf.d/30-spxserial.conf
    /etc/init.d/identify.sh start

    # Erase failsafe stored data, it may be in MTD or eMMC boot partitions
    if [ "$HOSTTYPE" = "arm" ]; then
        mtd="$(sed -e '/"failsafe-data"$/{s,:.*,,;p};d' /proc/mtd)"
        if [ -n "$mtd" ]; then
            flash_erase -q /dev/"$mtd" 0 0
        fi
        for bdev in mmcblk0boot0 mmcblk0boot1; do
            [ -b /dev/"$bdev" ] || continue
            blkid -c /dev/null -w /dev/null -s none /dev/"$bdev" || continue
            if [ -f /sys/block/"$bdev"/force_ro ]; then
               force_ro="$(< /sys/block/"$bdev"/force_ro)"
               echo 0 > /sys/block/"$bdev"/force_ro
               dd if=/dev/zero of=/dev/"$bdev" bs=4096 count=1 conv=fsync 2> /dev/null
               echo "$force_ro" > /sys/block/"$bdev"/force_ro
            fi
        done
        # Erase the fwconfig variable from U-Boot env if present to avoid
        # resets on future boots
        fwconfig="$(fw_printenv -n fwconfig 2>/dev/null)"
        if [ -n "$fwconfig" ]; then
            fw_setenv fwconfig
        fi
        # Erase the mainexargs variable from U-Boot env if present since
        # it contains the non-default kernel settings
        mainexargs="$(fw_printenv -n mainexargs 2>/dev/null)"
        if [ -n "$mainexargs" ]; then
            fw_setenv mainexargs
        fi
        # Erase the maxtemp variable from U-Boot env to revert to default
        maxtemp="$(fw_printenv -n maxtemp 2>/dev/null)"
        if [ -n "$maxtemp" ]; then
            fw_setenv maxtemp
        fi
    else
        [ -f /etc/spinetix/identifiers ] && . /etc/spinetix/identifiers
        for bdev in $SPX_FAILSAFE_DATA_BDEV1 $SPX_FAILSAFE_DATA_BDEV2; do
            if [ -b "$bdev" ]; then
                bdevro=/sys/block/${bdev##*/}/force_ro
                if [ -f "$bdevro" ]; then
                    force_ro="$(< "$bdevro")"
                    echo 0 > "$bdevro"
                fi
                dd if=/dev/zero of="$bdev" bs=4096 count=1 conv=fsync 2> /dev/null
                [ -f "$bdevro" ] && echo "$force_ro" > "$bdevro"
            fi
        done
        # Erase the fwconfig variable from grub env block if present to avoid
        # resets on future boots
        if [ -s /var/lib/spinetix/ldrenv/boot/grub.env ]; then
            if grub-editenv /var/lib/spinetix/ldrenv/boot/grub.env list | grep -q '^fwconfig=' ; then
                grub-editenv /var/lib/spinetix/ldrenv/boot/grub.env unset fwconfig
            fi
        fi
    fi
    # NOTE: /var/lib/spinetix/persistent-data is the persistent data
    # mount point which not be removed upon configuration reset!
    # Likewise the /var/lib/spinetix/persistent-data.noreset file
    # signals that the persistant data storage should not be reset.
    rm -f \
        /etc/network/spxproxy.conf /etc/network/modem.conf \
        /etc/chatscripts/modem /etc/ppp/peers/modem \
        /etc/spinetix/licenses /var/lib/ntp/drift \
        /var/lib/spinetix/recovery-splash-set \
        /etc/default/network
    # NOTE: /etc/default/ntpdate and /etc/network/interfaces are
    # handled via spxsysconf config-reset below.
    for f in /etc/ntp/local.conf /etc/snmp/snmpd.local.conf \
        /etc/default/media-mount /etc/default/bootpause ; do
        cp -a $f.spxsave $f
    done
    echo -n > /etc/network/resolv.conf.static
    echo -n > /etc/ppp/modem-password
    echo -n > /etc/chatscripts/modem-pin
    rm -f /etc/network/iface-*.conf
    /etc/spinetix/spxsysconf config-reset
    # Clear state of some system daemons
    rm -rf /var/lib/dhcpcd/*
    # Clear ssh host keys
    rm -f /etc/ssh/ssh_host*key*
    # Clear state files from some other packages
    rm -rf /etc/timestamp

    # NOTE: we do not clear logs any longer since it prevents diagnosing
    # problems after which the user did a "reset to factory defaults".
    #find /var/log -mindepth 1 -depth -type f -exec cp /dev/null \{\} \;
    #rm -f /var/lib/logrotate.status

    # Remove all iwd state and configuration data
    [ -d /var/lib/iwd ] && find /var/lib/iwd/ -mindepth 1 -depth -delete

    # Remove all ead state and configuration data
    [ -d /var/lib/ead ] && find /var/lib/ead/ -mindepth 1 -depth -delete

    # Remove cache files (trailing / ensures initial symlink is followed)
    find /var/cache/ -mindepth 1 -depth -type f -print0 | xargs -0r rm -f --

    # Cleanup snmp persistent data files
    find /var/lib/net-snmp/ -delete
}

#
# Main
#

case "$1" in
    reset)
        reset
        ;;
    *)
        echo "Usage $0 {reset}" >&2
        exit 1
        ;;
esac
