@echo off
:::Auto require administrator to avoid special path limitation
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (

    echo Applying run as administrator

    goto UACPrompt

) else ( goto gotAdmin )

:UACPrompt

    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"

    exit /B

:gotAdmin

    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

    pushd "%CD%"

    CD /D "%~dp0"
echo got admin authoright

setlocal enabledelayedexpansion
set batPath=%~dp0%

set logFilePath=C:\ProgramData\FM101
md %logFilePath%

if exist %logFilePath%\Install_log.log (
	del %logFilePath%\Install_log.log 1>>nul 2>>nul
)

echo ############check driver is installed before or not ############
echo %date% %time%:"-------------------------------------------log start-----------------------------">> %logFilePath%\Install_log.log
echo %date% %time%:"---------------Check whether the modem exists before installation ---------------">> %logFilePath%\Install_log.log
cd %~dp0%
if exist %"batPath"%device_exist.log (
	del %"batPath"%device_exist.log 1>>nul 2>>nul
)
if exist %"batPath"%driver_temp.log (
	del %"batPath"%driver_temp.log 1>>nul 2>>nul
)
if exist %"batPath"%device_status.log (
	del %"batPath"%device_status.log 1>>nul 2>>nul
)

devcon64 status "USB\VID_2cb7&PID_01A2" | find "matching device" >> %"batPath"%device_exist.log
devcon64 status "USB\VID_2cb7&PID_01A3" | find "matching device" >> %"batPath"%device_exist.log
devcon64 status "USB\VID_2cb7&PID_01A4" | find "matching device" >> %"batPath"%device_exist.log
for /f "tokens=1,2,3,4" %%i in (%"batPath"%device_exist.log) do (
	set ExistDevice=%%i

	if "!ExistDevice!" EQU "1" (
		goto break
	)
)

:CheckRecovery
del %"batPath"%device_exist.log 1>>nul 2>>nul
devcon64 status "USB\VID_05C6&PID_9008" | find "matching device" >> %"batPath"%device_exist.log
for /f "tokens=1,2,3,4" %%i in (%"batPath"%device_exist.log) do (
	set NeedRecovery=%%i
	if "!NeedRecovery!" EQU "1" (
		echo "Modem NOT Exist , need do recovery after install driver !!"
		goto break
	)
)

echo "Modem NOT Exist , not install drivers,the program will stop executing !!"
echo %date% %time%:   Modem NOT Exist , not install drivers,The program will stop executing !! >> %logFilePath%\Install_log.log
	timeout /t 10
exit

:break
echo Modem Exist: !ExistDevice!  >> %logFilePath%\Install_log.log
if "!ExistDevice!" EQU "1" (
	echo "Modem Device Exist, check whether uninstall !!"
	echo %date% %time%:   Modem Device Exist, check whether uninstall !! >> %logFilePath%\Install_log.log
	call "UninstallTool.exe" -silent -101
        timeout /t 2 >> nul
	echo "Drivers have been uninstalled completely !!"
	echo %date% %time%:   Drivers have been uninstalled completely !! >> %logFilePath%\Install_log.log
	set /a Installmethod=0
) else (	
	echo "Modem Device NOT Exist , install drivers at once !!"
	echo %date% %time%:   Modem Device NOT Exist , install drivers at once !! >> %logFilePath%\Install_log.log
	set /a Installmethod=1
)

if exist %"batPath"%device_exist.log (
	del %"batPath"%device_exist.log 1>>nul 2>>nul
)

::GOTO :EOF
md "C:\Windows\Wwan"
echo ############ Install Modem Driver ############

:InstallfbserDriver
if exist "%~dp0FwUpdateDriver"\fbser.inf (
    pnputil.exe /add-driver "%~dp0FwUpdateDriver"\fbser.inf /install > driver_temp.log
) else (
    pnputil.exe /add-driver "%~dp0FbSer"\fbser.inf /install > driver_temp.log
)
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [fbser] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [fbser] successful !!"
	echo %date% %time%:   [STEP  PASS] Install driver [fbser] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [fbser] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [fbser] successful !!"
		echo %date% %time%:   [STEP  PASS] Install driver [fbser] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [fbser] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [fbser] failed !!"
		echo %date% %time%:   [STEP  FAIL] Install driver [fbser] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
devcon64 rescan 1>>nul 2>>nul
timeout /t 2 >> nul

:InstallFBResetDriver
pnputil.exe /add-driver "%~dp0FBReset"\FBReset.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [FBReset] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [FBReset] successful !!"
	echo %date% %time%:  [STEP  PASS] Install driver [FBReset] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [FBReset] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [FBReset] successful !!"
		echo %date% %time%:  [STEP  PASS] Install driver [FBReset] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [FBReset] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [FBReset] failed !!"
		echo %date% %time%:  [STEP  FAIL] Install driver [FBReset] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
devcon64 rescan 1>>nul 2>>nul
timeout /t 2 >> nul

devcon64 status "USB\VID_2cb7&PID_01A3" | find "matching device" >> %"batPath"%device_exist.log
devcon64 status "USB\VID_05C6&PID_9008" | find "matching device" >> %"batPath"%device_exist.log
for /f "tokens=1,2,3,4" %%i in (%"batPath"%device_exist.log) do (
	set ExistDevice=%%i

	if "!ExistDevice!" EQU "1" (
		GOTO :CheckDeviceAgain
	)
)

GOTO :EOF

:CheckDeviceAgain
if exist %"batPath"%device_exist.log (
	del %"batPath"%device_exist.log 1>>nul 2>>nul
)



:CheckfbgnssDriver
if exist "%~dp0FbGNSS"\fbgnss.inf (
	GOTO :InstallfbgnssDriver
) else (
	GOTO :InstallFbWwanConfigDriver
)

:InstallfbgnssDriver
pnputil.exe /add-driver "%~dp0FbGNSS"\fbgnss.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [fbgnss] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [fbgnss] successful !!"
	echo %date% %time%:  [STEP  PASS] Install driver [fbgnss] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [fbgnss] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [fbgnss] successful !!"
		echo %date% %time%:  [STEP  PASS] Install driver [fbgnss] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [fbgnss] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [fbgnss] failed !!"
		echo %date% %time%:  [STEP  FAIL] Install driver [fbgnss] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
devcon64 rescan 1>>nul 2>>nul
timeout /t 2 >> nul

echo "[STEP Check] Checking the fbgnssDriver is running or not !!"
echo %date% %time%:   [STEP Check] Checking the fbgnssDriver is running or not >> %logFilePath%\Install_log.log
set /a waitcounter=0

:waitforGNSS
if %Installmethod% NEQ 1 (
	devcon64 status "USB\VID_2CB7&PID_01A3&MI_02" | find "Driver is" > device_status.log
	for /f "tokens=1,2,3" %%i in (device_status.log) do set DeviceStatus=%%k
	
	echo "DeviceStatus : !DeviceStatus!"
	echo %date% %time%:   DeviceStatus : !DeviceStatus! >> %logFilePath%\Install_log.log
	if "!DeviceStatus!" == "running." (
		call :CheckFbWwanConfigDriver
	) else (
		if %waitcounter% LEQ 10 (
			set /a waitcounter=%waitcounter% + 1
			timeout /t 1 >> nul
			call :waitforGNSS
		) else (
			echo "[ Critical ] **********************************************"
			echo "[ Critical ] fbgnssDriver status is abnormal, please check it !!"
			echo "[ Critical ] **********************************************"
			
			echo %date% %time%:   [ Critical ] ********************************************** >> %logFilePath%\Install_log.log
			echo %date% %time%:   [ Critical ] fbgnssDriver status is abnormal, please check it !! >> %logFilePath%\Install_log.log
			echo %date% %time%:   [ Critical ] ********************************************** >> %logFilePath%\Install_log.log
			call :CheckFbWwanConfigDriver
		)
	)
) else (
	call :CheckFbWwanConfigDriver
)

del device_status.log 1>>nul 2>>nul

GOTO :EOF

:CheckFbWwanConfigDriver
if exist "%~dp0FbWwanConfigDriver"\fbwwanConfig.inf (
	GOTO :InstallFbWwanConfigDriver
) else (
	GOTO :InstallFwUpdateDriver
)

:InstallFbWwanConfigDriver
pnputil.exe /add-driver "%~dp0FbWwanConfigDriver"\fbwwanConfig.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [FbWwanConfigDriver] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [FbWwanConfigDriver] successful !!"
	echo %date% %time%:  [STEP  PASS] Install driver [FbWwanConfigDriver] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [FbWwanConfigDriver] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [FbWwanConfigDriver] successful !!"
		echo %date% %time%:  [STEP  PASS] Install driver [FbWwanConfigDriver] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [FbWwanConfigDriver] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [FbWwanConfigDriver] failed !!"
		echo %date% %time%:  [STEP  FAIL] Install driver [FbWwanConfigDriver] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
devcon64 rescan 1>>nul 2>>nul
timeout /t 2 >> nul


:InstallFwUpdateDriver
pnputil.exe /add-driver "%~dp0FwUpdateDriver"\FwUpdateDriver.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [FwUpdateDriver] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [FwUpdateDriver] successful !!"
	echo %date% %time%:   [STEP  PASS] Install driver [FwUpdateDriver] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [FwUpdateDriver] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [FwUpdateDriver] successful !!"
		echo %date% %time%:   [STEP  PASS] Install driver [FwUpdateDriver] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [FwUpdateDriver] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [FwUpdateDriver] failed !!"
		echo %date% %time%:   [STEP  FAIL] Install driver [FwUpdateDriver] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
devcon64 rescan 1>>nul 2>>nul
timeout /t 2 >> nul

if "!NeedRecovery!" EQU "1" (
	goto FwRecovery
) else (
	goto CopyVersionDLL
)

:FwRecovery
::Copy the exe and FLZ to dest path
md C:\Windows\Wwan\Firmware\
copy "%~dp0FwUpdateDriver"\FwPackage.flz C:\Windows\Wwan\Firmware\
echo %date% %time%:   Copy the FwPackage.flz to C:\Windows\Wwan\Firmware!! >> %logFilePath%\Install_log.log
md C:\Windows\Wwan\FwSwitchbin\
copy "%~dp0FwUpdateDriver"\FwUpdate.ini C:\Windows\Wwan\FwSwitchbin\
echo %date% %time%:   Copy the FwUpdate.ini to C:\Windows\Wwan\Firmware!! >> %logFilePath%\Install_log.log
copy "%~dp0FwUpdateDriver"\FwSwitchService.exe C:\Windows\Wwan\FwSwitchbin\
echo %date% %time%:   Copy the FwSwitchService.exe to C:\Windows\Wwan\Firmware!! >> %logFilePath%\Install_log.log
::Add the registry Key
::HKLM, SYSTEM\CurrentControlSet\Control\Fibocom\FwUpdateDriver, First_Install, %RegTypeDW%, 1
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Fibocom\FwUpdateDriver /v First_Install /t REG_DWORD /d 1 /f
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Fibocom\FwUpdateDriver /v SubsysID /t REG_SZ /d "2CB701A3" /f

::Start FwSwitchService
sc create "Fibocom WWAN Flash Service" binpath="C:\Windows\Wwan\FwSwitchbin\FwSwitchService.exe" start=auto
sc start "Fibocom WWAN Flash Service"

:CopyVersionDLL
del device_status.log 1>>nul 2>>nul
echo "Driver Install Successfully!"
echo "If firmware upgrade is updating, please waiting for finish and restart system.Othewise,please restart your system after the installation successfully!"
echo %date% %time%:  If firmware upgrade is updating, please waiting for finish and restart system.Othewise,please restart your system after the installation successfully! !! >> %logFilePath%\Install_log.log
timeout /t 10

GOTO :EOF

::pause