#!/bin/bash


if which wdstop >/dev/null ; then
    
    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 /usr/bin/flashq7.msc.rom ]; then
		    logger -s -t flashbios "Stopping watchdog"
		    if wdstop; then
			logger -s -t flashbios "Flashing bios, cross your fingers"
			bios.msc -w /usr/bin/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 which cgutlcmd >/dev/null ; then
        
	boardname=$(cgutlcmd cginfo /ot:board /dump| grep 'Board Name:' | sed -e 's/.*: *//')
        
        if [ "${boardname}" = "E900" ] ; then
            if [ -f /usr/bin/flash.e900.rom ] ; then
                logger -s -t flashbios "Stopping watchdog"
                if wdstop; then
                    logger -s -t flashbios "Flashing bios, cross your fingers"
                    cgutlcmd BFLASH /usr/bin/flash.e900.rom
                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}" = "QMEN" ] ; then
            if [ -f /usr/bin/flashq7.qa.rom ] ; then
            	logger -s -t flashbios "Stopping watchdog"
            	if wdstop; then
            	    logger -s -t flashbios "Flashing bios, cross your fingers"
            	    cgutlcmd module /ot:board /add /if:/usr/bin/flashq7.qa.rom /t:1
            	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
            if [ -f /usr/bin/flashq7.qa6.rom ] ; then
            	logger -s -t flashbios "Stopping watchdog"
            	if wdstop; then
            	    logger -s -t flashbios "Flashing bios, cross your fingers"
            	    cgutlcmd module /ot:board /add /if:/usr/bin/flashq7.qa6.rom /t:1
            	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
            logger -s -t flashbios "Unknown board: ${boardname}"
            exit 1
        fi
        
    else
        logger -s -t flashbios "Error: cgutlcmd ist missing. Terminating"
        exit 1
    fi
    
    
else
    logger -s -t flashbios "Error: Cannot stop watchdog. Terminating"
    exit 1
fi

exit 0
