@ECHO OFF

SET defusername=admin
SET defpassword=admin
SET yafuflashpath=.\Win\Yafuflash.exe

:begin
for /f "delims=" %%A in ('dir ".\Image\*.ima" /b') do set "imagepath=%%A"


ECHO ====================================================
ECHO =            BMC firmware update tool              =
ECHO ====================================================

ECHO Image file name: %imagepath%


If "%1"=="" (
	ECHO Usage: %0 [ip address] 
	pause
	goto end
)

If NOT "%1"=="-local" (goto checkcd)
If "%2"=="" (SET username=%defusername%) Else (SET username=%2)
If "%3"=="" (SET password=%defpassword%) Else (SET password=%3)
If "%4"=="-p" (.\Win\Yafuflash.exe -nw -ip 169.254.0.17 -U %username% -P %password%  .\Image\%imagepath% -preserve-config)
If "%4"=="-preserve" (.\Win\Yafuflash.exe -nw -ip 169.254.0.17 -U %username% -P %password%  .\Image\%imagepath% -preserve-config)
If "%4"=="-preserve-config" (.\Win\Yafuflash.exe -nw -ip 169.254.0.17 -U %username% -P %password%  .\Image\%imagepath% -preserve-config)
If "%4"=="-f" (.\Win\Yafuflash.exe -nw -ip 169.254.0.17 -U %username% -P %password%  .\Image\%imagepath% -force-boot)
If "%4"=="-force" (.\Win\Yafuflash.exe -nw -ip 169.254.0.17 -U %username% -P %password%  .\Image\%imagepath% -force-boot)
If "%4"=="-force-boot" (.\Win\Yafuflash.exe -nw -ip 169.254.0.17 -U %username% -P %password%  .\Image\%imagepath% -force-boot)
If "%4"=="" (.\Win\Yafuflash.exe -nw -ip 169.254.0.17 -U %username% -P %password%  .\Image\%imagepath%)
pause
goto end

:checkcd
If "%1"=="-cd" (
	.\Win\Yafuflash.exe -cd .\Image\%imagepath%
	pause
	goto end
)

SET ipaddr=%1

For /f "delims=^. tokens=1-4" %%i in ('ECHO %ipaddr: =%^|Findstr /V "\<[0][0-9] \.[0\.][0-9] [^0-9\.] [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\. ^[0-9]*\.*[0-9]*\.*[0-9]*\.*$"') Do (
    Set /A Reutrn="(%%i>>8)|(%%j>>8)|(%%k>>8)|(%%l>>8)"
    Set IP=%%i.%%j.%%k.%%l
)

If "%Reutrn%" EQU "0" (
	ECHO IP is legitimate:%IP%
) Else (
	ECHO IP is invalid:%IP%
	goto end
)

If "%2"=="" (SET username=%defusername%) Else (SET username=%2)

If "%3"=="" (SET password=%defpassword%) Else (SET password=%3)

If "%4"=="-p" (.\Win\Yafuflash.exe -nw -ip %ipaddr% -U %username% -P %password%  .\Image\%imagepath% -preserve-config)

If "%4"=="-preserve" (.\Win\Yafuflash.exe -nw -ip %ipaddr% -U %username% -P %password%  .\Image\%imagepath% -preserve-config)

If "%4"=="-preserve-config" (.\Win\Yafuflash.exe -nw -ip %ipaddr% -U %username% -P %password%  .\Image\%imagepath% -preserve-config)

If "%4"=="-f" (.\Win\Yafuflash.exe -nw -ip %ipaddr% -U %username% -P %password%  .\Image\%imagepath% -force-boot)

If "%4"=="-force" (.\Win\Yafuflash.exe -nw -ip %ipaddr% -U %username% -P %password%  .\Image\%imagepath% -force-boot)

If "%4"=="-force-boot" (.\Win\Yafuflash.exe -nw -ip %ipaddr% -U %username% -P %password%  .\Image\%imagepath% -force-boot)

If "%4"=="" (.\Win\Yafuflash.exe -nw -ip %ipaddr% -U %username% -P %password%  .\Image\%imagepath%)

pause

:end

