#! /bin/sh
##################################################################################
##################################################################################
# install results:
##################################################################################
export INSTALL_SUCCESS_NO_REBOOT=0
export INSTALL_SUCCESS_REBOOT=1
export INSTALL_WRONG_HARDWARE=2
export INSTALL_KERNEL_CHECKSUM=3
export INSTALL_FILESYSTEM_CHECKSUM=4
export INSTALL_URLADER_CHECKSUM=5
export INSTALL_OTHER_ERROR=6
export INSTALL_FIRMWARE_VERSION=7
export INSTALL_DOWNGRADE_NEEDED=8
force_update=n
force_down=n
for i in "$@" ; do
    case $i in
        -f)
            force_update=y
        ;;
        -d)
            force_down=y
        ;;
    esac
done
##################################################################################
# get Kernelversion for further handling (deny update <= 3.x)
##################################################################################
currKver="$(uname -r)"
kversion=""
case ${currKver} in
    [3-9].* )
        kversion="${currKver%%-*}"; kversion="${kversion%.*}";
        echo "install: have Kernel ${currKver} - set kversion '${kversion}'";
        ;;
    *)
        echo "install: updating Kernel '${currKver}' is not supported";
        exit "${INSTALL_FIRMWARE_VERSION}"
        ;;
esac
##################################################################################
echo "install: check and install new firmware ..."
##################################################################################
need_reboot="${INSTALL_SUCCESS_NO_REBOOT}"
##################################################################################
# Power LED blinken lassen
##################################################################################
/bin/update_led_on
# AVM watchdog compatibility blurb
if [ -x /sbin/avm_watchdog ]; then
    have_avm_watchdog() { /sbin/avm_watchdog --detect >/dev/null; }
    avm_watchdog() { /sbin/avm_watchdog "$@"; }
    echo_avm_watchdog_cmd() { echo "/sbin/avm_watchdog $*"; }
else
    # Old libwdt, /dev/watchdog is AVM device
    have_avm_watchdog() { test -c /dev/watchdog; }
    avm_watchdog() { echo "$*" > /dev/watchdog; }
    echo_avm_watchdog_cmd() { echo "echo $* > /dev/watchdog"; }
fi
##################################################################################
# install support functions
##################################################################################
abort_update() {
    local code="$1" reason="${2-}"
    echo "${reason:+$reason: }abort update -- set INFO led to off (modul=7, state=1)"
    /bin/update_led_off
    exit "${code}"
}
################################
# accepted list of OEMs:
################################
echo OEM="${OEM}"
# Fritz_Box_HW284x
echo testing acceptance for device Fritz_Box_HW284x ...
    if [ ! -z "${OEM}" ] ; then
        oem_found=0
        for i in  avm ; do
            if [ "$i" = "${OEM}" ] ; then
                echo "OK - OEM ${OEM} is supported"
                oem_found=1
                break
            fi
        done
        if [ "$oem_found" = "0" ] ; then 
            abort_update "${INSTALL_WRONG_HARDWARE}" "OEM ${OEM} not supported"
        fi
    fi
echo "testing acceptance for device Fritz_Box_HW284x done"
kernel_start=0x00200000
kernel_size=9437184
filesystem_start=0x05800000
filesystem_size=19922944
newFWver=08.25
# Versioninfo:	284.08.25
# Buildnummer:	r132295
# Checkpoint:	6346821bfd2b3fc4736bc4824e01ee5b5cb940b4
# Boxinfo:	HWID=284;OEM=avm;
#! /bin/sh
##################################################################################
#
# FW Version Pattern: major.middle.minor -- compare middle and minor
#
##################################################################################
versioncmp() {
  local a="$1.0" cmp=$2 b="$3.0";
  while test "${a%%.*}" -eq "${b%%.*}" -a "${a}" != 0 -a "${b}" != 0; do
    a="${a#*.}" b="${b#*.}";
  done
  test "${a%%.*}" "$cmp" "${b%%.*}"
}
##################################################################################
# ascertain update requirements
##################################################################################
currFWver=$(/etc/version -v)
case "$currFWver" in
    [0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]) ;;
    [0-9][0-9][0-9].[0-9][0-9].[0-9][0-9][0-9]) ;;
    *)
    abort_update "${INSTALL_FIRMWARE_VERSION}" "error parsing firmware version"
    ;;
esac
currFWver="${currFWver#*.}" # strip major-part (xx.)
echo "curr: xx.${currFWver}  new: xx.${newFWver}"
##################################################################################
# Downgrade with or w/o factorysettings or normal update ?
##################################################################################
if [ "${force_update}" = "y" ] ; then
    echo "Force: Accept Firmware Version: xx.${newFWver} "
    echo "Force: Downgrade with factorysettings ..."
    /bin/setfactorydefaults
    echo "Force: factorysettings done."
else
    ##################################################################################
    #    (newFW) -lt (currFW) :   trigger update interaction
    ##################################################################################
    if versioncmp "${newFWver}" -lt "${currFWver}" ; then
        echo "warning: Firmware downgrade detected"
        # behaviour for devices which basically are downgradable
        if [ "${force_down}" = "y" ] ; then
            echo "Force: Downgrade w/o factorysettings ..."
            # proceed (ignore downgrade, do not trigger interaction) 
        else
            abort_update "${INSTALL_DOWNGRADE_NEEDED}"
        fi
    fi
    echo "Accept Firmware Version: xx.${newFWver}"
fi
# next: prepare_update
#! /bin/sh
##################################################################################
# prepare install
##################################################################################
# do no longer overwrite/remove /var/post_install
if [ ! -f /var/post_install ] ; then
# create, if not present
  echo "#! /bin/sh" >/var/post_install
fi
# append sequence to /var/post_install
# stop Deamons:
#   - LED- and Operating hours counter
#   - telefon, telnetd
{
echo 'echo $0: start'
echo "sleep 1"
echo "killall run_clock"
echo "if pidof telefon &>/dev/null ; then killall telefon ; fi"
echo "if pidof telnetd &>/dev/null ; then killall telnetd ; fi"
echo "echo MODE=update > /dev/avm_power"
} >>/var/post_install
# disable Watchdog immediately before flashing
if have_avm_watchdog; then
  echo_avm_watchdog_cmd disable >> /var/post_install
fi
# still running ?
{
echo "echo still running:"
echo "ps"
echo "lsmod"
echo "sleep 1"
} >>/var/post_install
# next: prepare_update_flash
/bin/update_led_on
exit "${INSTALL_SUCCESS_REBOOT}"
