#!/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 >/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
		logger -s -t flashbios "Stopping watchdog"
		if wdstop; then
		    logger -s -t flashbios "Flashing bios"
		    bios.msc -w ${SCRIPT_PATH}/flashq7.msc.rom && exit 0
		    # Oups
		    logger -s -t flashbios "Error: Flash failed"
		    exit 1
		else
		    logger -s -t flashbios "Can't stop watchdog"
		    exit 1
		fi
	    else
		logger -s -t flashbios "Error: ROM-File is missing"
		exit 1
	    fi
        else
            logger -s -t flashbios "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
            logger -s -t flashbios "Stopping watchdog"
            if wdstop; then
                logger -s -t flashbios "Flashing bios"
                ${CGUTLCMD} BFLASH ${SCRIPT_PATH}/flash.e900.rom
		if which wdstart >/dev/null 2>&1; then
		    wdstart
		else
		    logger -s -t flashbios "wdstart not found"
		fi
            else
                logger -s -t flashbios "Can't stop watchdog"
                exit 1
            fi
        else
            logger -s -t flashbios "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}" = "OMGAR2V4" ]; then
	    logger -s -t flashbios "BIOS settings are up to date"
	    exit 1
	fi
	
	${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/omga.module /t:6
	${CGUTLCMD} module /ot:board /OEM:OMGAR2V4
        if [ -f ${SCRIPT_PATH}/flashq7.qa32.${BOARD_REVISION}.rom ] ; then
            logger -s -t flashbios "Stopping watchdog"
            logger -s -t flashbios "Flashing bios"
            ${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qa32.${BOARD_REVISION}.rom /t:1
        else
            logger -s -t flashbios "Error: ROM-File ist missing"
            exit 1
        fi
    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}" = "OMGAR2V4" ]; then
	    logger -s -t flashbios "BIOS settings are up to date"
	    exit 2
	fi

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

	if  [ "${biosversion}" = "QC32R115" ]; then
	    if [ -f ${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.115.rom ] ; then
            	logger -s -t flashbios "Flashing bios"
            	${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.115.rom /t:1
	    else
            	logger -s -t flashbios "Error: ROM-File ist missing"
            	exit 2
	    fi
	elif  [ "${biosversion}" = "QC32R116" ]; then
	    if [ -f ${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.116.rom ] ; then
            	logger -s -t flashbios "Flashing bios"
            	${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.116.rom /t:1
	    else
            	logger -s -t flashbios "Error: ROM-File ist missing"
            	exit 1
	    fi
	elif  [ "${biosversion}" = "QC32R126" ]; then
	    if [ -f ${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.126.rom ] ; then
            	logger -s -t flashbios "Flashing bios"
            	${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.126.rom /t:1
	    else
            	logger -s -t flashbios "Error: ROM-File ist missing"
            	exit 1
	    fi
	elif  [ "${biosversion}" = "QC32R134" ]; then
	    if [ -f ${SCRIPT_PATH}/flashq7.qc32.134.rom ] ; then
            	logger -s -t flashbios "Flashing bios"
            	${CGUTLCMD} module /ot:board /add /if:${SCRIPT_PATH}/flashq7.qc32.${BOARD_REVISION}.134.rom /t:1
	    else
            	logger -s -t flashbios "Error: ROM-File ist missing"
            	exit 1
	    fi
	fi
    elif [ "${boardname}" = "QMEN" ] ; then
        if [ -f ${SCRIPT_PATH}/flashq7.qa.rom ] ; then
            logger -s -t flashbios "Stopping watchdog"
            if wdstop; then
            	logger -s -t flashbios "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
		    logger -s -t flashbios "wdstart not found"
		fi
            else
            	logger -s -t flashbios "Can't stop watchdog"
            	exit 1
            fi
        else
            logger -s -t flashbios "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
            	logger -s -t flashbios "Stopping watchdog"
            	if wdstop; then
            	    logger -s -t flashbios "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
			logger -s -t flashbios "wdstart not found"
		    fi
            	else
            	    logger -s -t flashbios "Can't stop watchdog"
            	    exit 1
            	fi
            else
            	logger -s -t flashbios "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
            	logger -s -t flashbios "Stopping watchdog"
            	if wdstop; then
            	    logger -s -t flashbios "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
			logger -s -t flashbios "wdstart not found"
		    fi
            	else
            	    logger -s -t flashbios "Can't stop watchdog"
            	    exit 1
            	fi
            else
            	logger -s -t flashbios "Error: ROM-File ist missing"
            	exit 1
            fi
        fi
    else
        logger -s -t flashbios "Unknown board: ${boardname}"
        exit 1
    fi
    
else
    logger -s -t flashbios "Error: cgutlcmd ist missing. Terminating"
    exit 1
fi


exit 0
