::::::::::::::::::::::::::::::::::::::::::::
:: DriverInstaller.cmd - Version 4
:: Automatically check & get admin rights
::::::::::::::::::::::::::::::::::::::::::::
 @echo off
 CLS
 ECHO.
 ECHO =============================
 ECHO Running Admin shell
 ECHO =============================

:init
 setlocal DisableDelayedExpansion
 set cmdInvoke=1
 set winSysFolder=System32
 set "batchPath=%~0"
 for %%k in (%0) do set batchName=%%~nk
 set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
 setlocal EnableDelayedExpansion

:checkPrivileges
  NET FILE 1>NUL 2>NUL
  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
  if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
  ECHO.
  ECHO **************************************
  ECHO Invoking UAC for Privilege Escalation
  ECHO **************************************

  ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
  ECHO args = "ELEV " >> "%vbsGetPrivileges%"
  ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
  ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
  ECHO Next >> "%vbsGetPrivileges%"

  if '%cmdInvoke%'=='1' goto InvokeCmd 

  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  goto ExecElevation

:InvokeCmd
  ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
  ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"

:ExecElevation
 "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
 exit /B

:gotPrivileges
 setlocal & cd /d %~dp0
 if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

 ::::::::::::::::::::::::::::
 ::START Uninstalling the existing instances of UCMCx driver
 ::::::::::::::::::::::::::::

setlocal ENABLEEXTENSIONS
set KEY_NAME="HKLM\Software\Microsoft\Windows\CurrentVersion\uninstall"
set VALUE_NAME=UninstallString
set exePath="NULL"

:loopStart
for /F "usebackq tokens=3" %%A IN (`reg query "%KEY_NAME%" /s /f cyucmclient.inf 2^>nul ^| find "%VALUE_NAME%"`) do (
  set exePath=%%A
)
if %exePath% == "NULL" goto exitLoop
echo %exePath%

for /F "usebackq tokens=5" %%A IN (`reg query "%KEY_NAME%" /s /f cyucmclient.inf 2^>nul ^| find "%VALUE_NAME%"`) do (
  set infPath=%%A
)
echo %infPath%

"%exepath%" /U %infPath% /D /S

set exePath="NULL"
goto loopStart

:exitLoop


 ::::::::::::::::::::::::::::
 ::START installing UCMCx driver
 ::::::::::::::::::::::::::::

setlocal

if not "%1"=="" (
set silent=%1
) else (
set silent=NULL
)

set workingDir=%CD%

rem echo *******finding ProductType, 1=Work Station, 2= Domain Controller, 3= Server*******
for /f "skip=1 delims=" %%p in ('WMIC Os get ProductType') do if not defined ProductType set ProductType=%%p
if %ProductType%==1 (
  set IsClientOS=true
) else (
  set IsClientOS=false
)
echo ProductType is %ProductType%
echo IsClientOS is equal to %IsClientOS%



rem echo *******finding OS Architecture*******
for /f "skip=1 delims=" %%x in ('WMIC Os get OSArchitecture') do if not defined OSArchitecture set OSArchitecture=%%x
rem if not x%OSArchitecture:64=%==x%OSArchitecture% set OSArchitecture=64
echo %OSArchitecture% | findstr /C:"64"

if %errorlevel%==0 (
  set OSARCH=x64
) else (
  set OSARCH=x86
)
echo OSARCH is %OSARCH%

rem echo *******finding OS version*******
for /f "tokens=4-5 delims=. " %%i in ('ver') do set OSVERSION=%%i.%%j
if "%IsClientOS%" == "true" (
if "%OSVERSION%" == "5.1" set OSName=XP
if "%OSVERSION%" == "5.2" set OSName=XP
if "%OSVERSION%" == "6.0" set OSName=Vista
if "%OSVERSION%" == "6.1" set OSName=Windows7
if "%OSVERSION%" == "6.2" set OSName=Windows8
if "%OSVERSION%" == "6.3" set OSName=Windows8.1
if "%OSVERSION%" == "10.0" set OSName=Windows10
) else (
if "%OSVERSION%" == "5.0" set OSName=W2K
if "%OSVERSION%" == "5.2" set OSName=W2K3
if "%OSVERSION%" == "6.0" set OSName=W2K8
if "%OSVERSION%" == "6.1" set OSName=W2K8R2
if "%OSVERSION%" == "6.2" set OSName=W2K12
if "%OSVERSION%" == "6.3" set OSName=W2K12R2
if "%OSVERSION%" == "10.0" set OSName=Windows10
)

echo OS Version is %OSVERSION% and OS Name is %OSName%

set productionFolderPath=%workingDir%\Drivers\production\%OSName%-%OSARCH%
echo %productionFolderPath%

pushd %productionFolderPath%

rem echo *******Running DPInste.exe*******
if "%silent%"=="/q" (
	"dpinst.exe" /q
 ) else (
	"dpinst.exe" /q
)

popd

set returncode=0
echo  errorlevel is %errorlevel%

rem 0x40000000   = 1073741824 ***Reboot Required***
if %errorlevel% GEQ 1073741824 set returncode=5
	
rem 0x80000000   = 2147483648 ***Driver Package could not be installed***
if %errorlevel% GEQ 2147483648 set returncode=1

@echo on
EXIT /B %returncode%