:: ###################################################################
:: # bt_reflash.bat
:: #
:: # Reflash IndustrialPro Gateway or MobilityPro Gateway Wireless modems via TFTP on Windows PC
:: #
:: # Copyright: 2010 Sixnet, LLC
:: #   
:: ###################################################################

@echo off
setlocal enableextensions enabledelayedexpansion
set CWD=%CD%
set returnCode=0

:: Display this script purpose
echo Sixnet Advanced Firmware Update ...
echo ===================================
Echo.
Echo This program is used to reflash an IndustrialPro Gateway ^(BT-6000^) or
Echo MobilityPro Gateway ^(BT-5000v2^) Sixnet Wireless modems.
Echo.
Echo The modem non-volatile memory will be entirely re-written.
Echo.

:: The following is used to capture the list of Ethernet Adapters present on this PC
set numAdapter=0
set inEth=0
for /F "usebackq tokens=1,* delims=^^" %%F in (`ipconfig /all`) DO (
	set line=%%F
    :: echo !line!
	if "!line:~0,17!" == "Ethernet adapter " (
		set  /A numAdapter=!numAdapter! + 1
		set adapterName!numAdapter!=!line:~17,-1!
		set ipAlreadySet!numAdapter!=no
		set dhcpEnabled!numAdapter!=?
		set inEth=1
    ) else if "!line:~0,39!" == "   Physical Address. . . . . . . . . : " (
			if "!inEth!" == "1" set adapterPhy!numAdapter!=!line:~39!
    ) else if "!line:~0,44!" == "        Physical Address. . . . . . . . . : " (
			if "!inEth!" == "1" set adapterPhy!numAdapter!=!line:~44,17!
    ) else if "!line:~0,39!" == "   DHCP Enabled. . . . . . . . . . . : " (
			if "!inEth!" == "1" set dhcpEnabled!numAdapter!=!line:~39!
    ) else if "!line:~0,44!" == "        Dhcp Enabled. . . . . . . . . . . : " (
			if "!inEth!" == "1" set dhcpEnabled!numAdapter!=!line:~44,3!
    ) else if "!line:~0,39!" == "   IPv4 Address. . . . . . . . . . . : " (
			if "!inEth!" == "1" (
			    if "!line:~39,14!" == "192.168.88.180" set ipAlreadySet!numAdapter!=yes
			)
    ) else if "!line:~0,44!" == "        IP Address. . . . . . . . . . . . : " (
			if "!inEth!" == "1" (
			    if "!line:~44,14!" == "192.168.88.180" set ipAlreadySet!numAdapter!=yes
			)
    ) else if "!line!" EQU "" (
	    rem ignore blank lines
    ) else if not "!line:~0,3!" EQU "   " (
	    rem set inEth=0
    )
)
if ERRORLEVEL 1 set returnCode=2

:: Let the user decide which interface he/she wants to use for TFTP
if %numAdapter% EQU 0 (
    echo No Ethernet adapter has been detected on this PC.
	echo Advanced firmware upgrade cannot be performed.
	set returnCode=2
	goto END
) else if %numAdapter% EQU 1 (
    set selAdapter=1
) else (
:getAdapter
   echo Attention: %numAdapter% ethernet adapters have been found on this PC:
   echo =========
   For /L %%k in (1,1,%numAdapter%) Do (
       Echo     %%k "!adapterName%%k!"
       Echo           PHY:                        !adapterPhy%%k!
	   Echo           DHCP enabled:               !dhcpEnabled%%k!
	   Echo           TFTP default IP configured: !ipAlreadySet%%k!
       Echo.
   )
   set /P selAdapter=Please select which Ethernet adapter you want to use [1..%numAdapter%]:
   :: Exit if nothing selected or if 0 is selected
   if "!selAdapter!" EQU "" (
        Echo Advanced Firmware upgrade aborted.
		goto END
   )
   if !selAdapter! EQU 0 (
        Echo Advanced Firmware upgrade aborted.
		goto END
   )
   if !selAdapter! LSS 1 (
        Echo Invalid adapter number [1..%numAdapter%].
		goto getAdapter
   )
   if !selAdapter! GTR %numAdapter% (
        Echo Invalid adapter number [1..%numAdapter%].
		goto getAdapter
   )
)

if "!adapterName%selAdapter%:~-1,1!" == ":" (
    set ETH_IF="!adapterName%selAdapter%:~0,-1!"
) else (
    set ETH_IF="!adapterName%selAdapter%!"
)
echo Selected adapter is %selAdapter% %ETH_IF%


echo.

echo 1) Please save and close any network files you may currently have open.
echo 2) Connect your modem to the computer with an RJ45 Ethernet cable.
echo.
pause
echo.

if "!ipAlreadySet%selAdapter%!" EQU "yes" (
    Echo Interface %ETH_IF% appears already configured with IP address 192.168.88.180.
	Echo For most modems, having 192.168.88.180 is normally enough.
	set /p configAdapter=Shall the interface be configured for all possible IP addresses [Y/N]:
) else (
    set configAdapter=y
)

if "!configAdapter:~0,1!" EQU "n" goto startTftp
if "!configAdapter:~0,1!" EQU "N" goto startTftp

:configAdapter
echo Saving current IP settings ^(This make take some time^)...
netsh -c interface dump > "%APPDATA%\orig_ip_settings.txt"
echo Setting static IPs required for advanced upgrade ...
netsh interface ip set address name=%ETH_IF% static 192.168.222.180 255.255.255.0
netsh interface ip add address name=%ETH_IF% 192.168.88.180 255.255.255.0
netsh interface ip add address name=%ETH_IF% 192.168.222.167 255.255.255.0
netsh interface ip add address name=%ETH_IF% 192.168.222.168 255.255.255.0
netsh interface ip add address name=%ETH_IF% 192.168.222.178 255.255.255.0

:startTftp
cd Images
bttftpsrv -v
cd ..

if "!configAdapter:~0,1!" EQU "n" goto advFwCompleted
if "!configAdapter:~0,1!" EQU "N" goto advFwCompleted

:restoreAdapter
echo Restoring original IP settings ...
netsh -f "%APPDATA%\orig_ip_settings.txt"

:advFwCompleted
echo.
echo Advanced Firmware upgrade has completed.

:END
cd "%CWD%"
exit /B !returnCode!
