#!/bin/bash
SCRIPT_PATH="$(cd "$(dirname "$0")" && pwd)"

if [ -x ${SCRIPT_PATH}/congatec/cgutlcmd ]; then
    CGUTLCMD=${SCRIPT_PATH}/congatec/cgutlcmd
else
    CGUTLCMD=/usr/bin/cgutlcmd
fi

if [ -x ${SCRIPT_PATH}/../i2c/i2crdrevision ]; then
    BOARD_REVISION=$(${SCRIPT_PATH}/../i2c/i2crdrevision)
else
    BOARD_REVISION=$(i2crdrevision)
fi

if which uspepc 2>/dev/null >/dev/null; then

    if uspepc -INFO -b 2>/dev/null | head -1 | grep -q "HW platform:.*Q7-US15W"; then

        # It's msc Q7 board
        if which bios.msc >/dev/null; then

            if [ -f ${SCRIPT_PATH}/flashq7.msc.rom ]; then
                echo "Stopping watchdog"
                if wdstop; then
                    echo "Flashing bios"
                    bios.msc -w ${SCRIPT_PATH}/flashq7.msc.rom && exit 0
                    # Oups
                    echo "Error: Flash failed"
                    exit 1
                else
                    echo "Can't stop watchdog"
                    exit 1
                fi
            else
                echo "Error: ROM-File is missing"
                exit 1
            fi
        else
            echo "Error: bios.msc is missing"
            exit 1
        fi
        exit 0

    fi
fi

if [ -x ${CGUTLCMD} ]; then

    boardname=$(${CGUTLCMD} cginfo /ot:board /dump | grep 'Board Name:' | sed -e 's/.*: *//')

    if [ "${boardname}" = "E900" ]; then
        if [ -f ${SCRIPT_PATH}/flash.e900.rom ]; then
            echo "Stopping watchdog"
            if wdstop; then
                echo "Flashing bios"
                ${CGUTLCMD} BFLASH ${SCRIPT_PATH}/flash.e900.rom
                if which wdstart >/dev/null 2>&1; then
                    wdstart
                else
                    echo "wdstart not found"
                fi
            else
                echo "Can't stop watchdog"
                exit 1
            fi
        else
            echo "Error: ROM-File is missing"
            exit 1
        fi
    elif [ "${boardname}" = "QA32" ]; then
        oembiosversion=$(${CGUTLCMD} cginfo /ot:board /dump | grep 'OEM BIOS Version:' | sed -e 's/.*: *//')
        if [ "${oembiosversion}" = "OMGAR2V5" ]; then
            echo "BIOS settings are up to date"
            exit 1
        fi

        ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/omga.module /t:6

        if [ -f ${SCRIPT_PATH}/flashq7.qa32.${BOARD_REVISION}.rom ]; then
            echo "Stopping watchdog"
            echo "Flashing bios"
            ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qa32.${BOARD_REVISION}.rom /t:1
        else
            echo "Error: ROM-File ist missing"
            exit 1
        fi

        ${CGUTLCMD} module /ot:board /OEM:OMGAR2V5

    elif [ "${boardname}" = "QC32" ]; then
        oembiosversion=$(${CGUTLCMD} cginfo /ot:board /dump | grep 'OEM BIOS Version:' | sed -e 's/.*: *//')
        biosversion=$(${CGUTLCMD} cginfo /ot:board /dump | grep 'System BIOS Version:' | sed -e 's/.*: *//')

        if [ "${oembiosversion}" = "OMGAR2V5" ]; then
            echo "BIOS settings are up to date"
            exit 2
        fi

        ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/omga.module /t:6

        if [ "${biosversion}" = "QC32R115" ]; then
            if [ -f ${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.115.rom ]; then
                echo "Flashing bios"
                ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.115.rom /t:1
            else
                echo "Error: ROM-File ist missing"
                exit 2
            fi
        elif [ "${biosversion}" = "QC32R116" ]; then
            if [ -f ${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.116.rom ]; then
                echo "Flashing bios"
                ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.116.rom /t:1
            else
                echo "Error: ROM-File ist missing"
                exit 1
            fi
        elif [ "${biosversion}" = "QC32R126" ]; then
            if [ -f ${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.126.rom ]; then
                echo "Flashing bios"
                ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.126.rom /t:1
            else
                echo "Error: ROM-File ist missing"
                exit 1
            fi
        elif [ "${biosversion}" = "QC32R134" ]; then
            if [ -f ${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.134.rom ]; then
                echo "Flashing bios"
                ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.134.rom /t:1
            else
                echo "Error: ROM-File ist missing"
                exit 1
            fi
        fi

        ${CGUTLCMD} module /ot:board /OEM:OMGAR2V5

    elif [ "${boardname}" = "QMEN" ]; then
        if [ -f ${SCRIPT_PATH}/flashq7.qa.rom ]; then
            echo "Stopping watchdog"
            if wdstop; then
                echo "Flashing bios"
                ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qa.rom /t:1
                if which wdstart >/dev/null 2>&1; then
                    wdstart
                else
                    echo "wdstart not found"
                fi
            else
                echo "Can't stop watchdog"
                exit 1
            fi
        else
            echo "Error: ROM-File ist missing"
            exit 1
        fi
    elif [ "${boardname}" = "QTOP" ]; then
        biosversion=$(${CGUTLCMD} cginfo /ot:board /dump | grep 'System BIOS Version:' | sed -e 's/.*: *//')
        if [ "${1}" = "full" ]; then
            if [ "${biosversion}" = "QTOPR116" ]; then
                echo "Full BIOS update already done"
                exit
            fi
            echo "Performing full BIOS update"
            if [ -f ${SCRIPT_PATH}/flashq7.qa6.full.116.rom ]; then
                echo "Stopping watchdog"
                if wdstop; then
                    echo "Flashing bios"
                    ${CGUTLCMD} bflash /ot:board /add /if:${SCRIPT_PATH}/flashq7.qa6.full.116.rom /t:1
                    if which wdstart >/dev/null 2>&1; then
                        wdstart
                    else
                        echo "wdstart not found"
                    fi
                else
                    echo "Can't stop watchdog"
                    exit 1
                fi
            else
                echo "Error: ROM-File ist missing"
                exit 1
            fi
        else
            # Module update
            if [ "${biosversion}" != "QTOPR116" ]; then
                echo "Version mismatch. Perform full update first"
                exit 1
            fi
            if [ -f ${SCRIPT_PATH}/flashq7.qa6.116.rom ]; then
                echo "Stopping watchdog"
                if wdstop; then
                    echo "Flashing bios"
                    ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qa6.116.rom /t:1
                    if which wdstart >/dev/null 2>&1; then
                        wdstart
                    else
                        echo "wdstart not found"
                    fi
                else
                    echo "Can't stop watchdog"
                    exit 1
                fi
            else
                echo "Error: ROM-File ist missing"
                exit 1
            fi
        fi
    else
        echo "Unknown board: ${boardname}"
        exit 1
    fi

else
    echo "Error: cgutlcmd ist missing. Terminating"
    exit 1
fi

exit 0
