if [ -z "$MIGTEST" ]; then
    if [ -f /lib/ar71xx.sh ]; then
        PLATFORM=ar71xx
        . /lib/ar71xx.sh
    elif [ -f /lib/ipq806x.sh ]; then
        PLATFORM=ipq806x
        . /lib/ipq806x.sh
    fi  


    . /lib/functions.sh
    BOARD=$(${PLATFORM}_board_name)
fi

if [ -z "$MIG_BASE" ];then
    MIG_BASE="/etc/migration/"
fi

if [ -z "$MIG_DIRS" ]; then
    MIG_DIRS=$(find "$MIG_BASE" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
fi

if [ -z "$CURRENT_CONFIG_VERSION" ]; then
    CURRENT_CONFIG_VERSION=$(grep CONFIG_VERSION /etc/delos-version |
                            awk -F'=' '{print $2}')
    if [ "$CURRENT_CONFIG_VERSION" = "" ]; then
        #assume 2.0.0 due empty CONFIG_VERSION
        CURRENT_CONFIG_VERSION="2.0.0"
    fi
fi

if [ -z "$UCI_TOOL" ];then
    UCI_TOOL="$(command -v uci)"
fi

if [ -z "$LOG_TOOL" ]; then
    LOG_TOOL="$(command -v logger)" || LOG_TOOL=":"
fi

migrationIsUpgrade() {
    if [ -z "$MIGTEST" ]; then
        # if downgrading the first migration step is not contained in /rom, not part of the firmware
        if [ -d "/rom/etc/migration/$1" ]; then
            $LOG_TOOL "Upgrade migration."
            return 1
        else
            $LOG_TOOL "Downgrade migration."
            return 0
        fi
    else
        $LOG_TOOL "Emulate migrationIsUpgrade: TBD"
        return 0
    fi
}

#used in 99_delos_migration
updateMigrationScripts() {
    migrationIsUpgrade $1
    if [ "$?" -eq "1" ]; then
        $LOG_TOOL "Need migration of update scripts (/etc/migration)"
        rm -rf "$MIG_BASE"*
        cp -af /rom/etc/migration/* "$MIG_BASE"
    fi
}

#used in 00_delos
updateUciDefaultsScript() {
    rm -rf /etc/uci-defaults/99_delos_migration
    cp -af /rom/etc/uci-defaults/99_delos_migration /etc/uci-defaults/99_delos_migration 
}

forceCurrentMigrationScripts() {
    if [ -z "$MIGTEST" ];then
        cp -af /rom/etc/uci-defaults/99_delos_migration /etc/uci-defaults/99_delos_migration
        cp -af /rom/lib/migration/* /lib/migration/
    fi
}
