#!/bin/sh
. $MIGTEST/lib/migration/migration.sh

$LOG_TOOL "upgrade from v5.0.4 to v5.1.0"

. /lib/functions.sh

# PIB migration for 75x0
migrate_pib_2_6_0_01() (
    [ -e /lib/firmware/plc/$board/fwconfig ] && . /lib/firmware/plc/$board/fwconfig || exit 1

    USR_PIB=$PLCFW_PATH/user.pib
    NEW_PIB=$PLCFW_PATH/user.pib.tmp
    TMP_PIB=/tmp/user.pib
    eval ORG_PIB="\"$PLCFW_PATH/\${PLCFW_PIB_$(uci_get_state delos baptization MT):-$PLCFW_PIB}\""

    # validate new user PIB and user PIB
    chkpib -q "$NEW_PIB" && mv "$NEW_PIB" "$USR_PIB"
    chkpib -q "$USR_PIB" || {
        rm -f "$USR_PIB"
        exit 0
    }
    # migrate only valid user PIB

    move_data() { # fromfile tofile offset type [size]
        setpib "$2" "$3" "$4" "$(getpib "$1" "$3" "$4" ${5:+"$5"})"
    }

    cp "$USR_PIB" "$TMP_PIB"

    # copy changed parts from new PIB template
    # prescalers
    move_data "$ORG_PIB" "$TMP_PIB" 0x1158 data 0x5bc
    move_data "$ORG_PIB" "$TMP_PIB" 0x1714 data 0x5bc

    mv "$TMP_PIB" "$USR_PIB"
)

if [ -f /lib/ar71xx.sh ]; then
    PLATFORM=ar71xx
    . /lib/ar71xx.sh
elif [ -f /lib/ipq806x.sh ]; then
    PLATFORM=ipq806x
    . /lib/ipq806x.sh
fi
board=$(${PLATFORM}_board_name)

case "$board" in
dlan-pro-1200-ac|dlan-1000-ac|dlan-pro-1200-n)
    migrate_pib_2_6_0_01
    ;;
esac

exit 0