
@echo off
SET OSDST=%~d0
if not exist %~d0\OEM\Preload\Command\POP*.ini set OSDST=C:
if not exist %OSDST%\OEM\AcerLogs md %OSDST%\OEM\AcerLogs
SET LogPath=%OSDST%\OEM\AcerLogs\InstallationUWP.Log

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
IF '%errorlevel%' NEQ '0' (
    ECHO Requesting administrative privileges...
    GOTO UACPrompt
) ELSE GOTO gotAdmin

:UACPrompt
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
ECHO UAC.ShellExecute "%~s0", "%~s1", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
del /q "%temp%\getadmin.vbs"
EXIT /B 1

:gotAdmin
ECHO.>>%LogPath%
ECHO %DATE% %TIME%[Log START]  ============ %~dpnx0 ============ >> %LogPath%
SETLOCAL ENABLEDELAYEDEXPANSION
pushd "%~dp0"

ECHO %DATE% %TIME%[Log TRACE]  Installing, please wait... >> %LogPath% 2>&1
ECHO Installing, please wait...

:SetCustomDATA
REM for /f "skip=2 tokens=3" %%A in ('reg query HKLM\Software\OEM\Metadata /v Brand') do set Brand=%%A
REM ECHO %DATE% %TIME%[Log TRACE]  SET Custom DATA File NAME by Brand >> %LogPath%
REM SET CustomDATA=.\CDF\custom.data.acerlt.xml
REM if /I "%Brand%" == "Gateway" set CustomDATA=gateway-abb.json
REM if /I "%Brand%" == "Packard" set CustomDATA=packardbell-abb.json

:::: clear %OSDST%\OEM\FilterVCLibsList.txt
if exist %OSDST%\OEM\FilterVCLibsList.txt del /f /q %OSDST%\OEM\FilterVCLibsList.txt >NUL 2>&1
if exist CheckingVCLib2_Done.tag del /f /q CheckingVCLib2_Done.tag >NUL 2>&1
md "%temp%\scratchdir" >NUL 2>&1
SET TargetVCLib=Microsoft.VCLibs.140.00_
ECHO %DATE% %TIME%[Log TRACE]  SET ImgPath=/online >>%LogPath% 2>&1
SET ImgPath=/online
ECHO %DATE% %TIME%[Log TRACE]  reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\WinPE" >>%LogPath% 2>&1
reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\WinPE" >>%LogPath% 2>&1
ECHO.>>%LogPath%
if !errorlevel! equ 0 (
	ECHO !DATE! !TIME![Log TRACE]  Running in the WinPE, SET ImgPath=/image:%OSDST%\ >>%LogPath% 2>&1
	SET ImgPath=/image:%OSDST%\
	ECHO !DATE! !TIME![Log TRACE]  goto :InstallUWP >>%LogPath% 2>&1
	goto :InstallUWP
) else if /i "%SystemDrive%" neq "%OSDST%" (
	ECHO !DATE! !TIME![Log TRACE]  Running in the Offline, SET ImgPath=/image:%OSDST%\ >>%LogPath% 2>&1
	SET ImgPath=/image:%OSDST%\
	ECHO !DATE! !TIME![Log TRACE]  goto :InstallUWP >>%LogPath% 2>&1
	goto :InstallUWP
) else (
	ECHO !DATE! !TIME![Log TRACE]  This is Online installation. >>%LogPath% 2>&1
)

ECHO !DATE! !TIME![Log TRACE]  dir /s /b "%TargetVCLib%*_x64*.appx" >>%LogPath% 2>&1
dir /s /b "%TargetVCLib%*_x64*.appx" >>%LogPath% 2>&1
(for /f "delims=" %%I in ('dir /s /b "%TargetVCLib%*_x64*.appx"') do (
	pushd "%%~dpI"
	ECHO !DATE! !TIME![Log TRACE]  call :CheckingVCLib x64
	call :CheckingVCLib x64
	popd
)) >>%LogPath% 2>&1

ECHO !DATE! !TIME![Log TRACE]  dir /s /b "%TargetVCLib%*_x86*.appx" >>%LogPath% 2>&1
dir /s /b "%TargetVCLib%*_x86*.appx" >>%LogPath% 2>&1
(for /f "delims=" %%I in ('dir /s /b "%TargetVCLib%*_x86*.appx"') do (
	pushd "%%~dpI"
	ECHO !DATE! !TIME![Log TRACE]  call :CheckingVCLib x86
	call :CheckingVCLib x86
	popd
)) >>%LogPath% 2>&1

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::	
::::   DO Not Trim Trailing space............
::::
::::   Each Set Value in the end exist the Space for dism split paramter
::::   set APPXPackagePath_X86=!APPXPackagePath_X86!/dependencypackagepath:"%%d" 
::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:InstallUWP
IF /i "%PROCESSOR_ARCHITECTURE%" equ "AMD64" (
	call :GetParameter >>%LogPath% 2>&1
	REM ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
	REM dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
	ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
	dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
) else (	
	call :GetParameter >>%LogPath% 2>&1
	REM ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
	REM dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
	ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
	dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/region:all /scratchdir:"%temp%\scratchdir">>%LogPath% 2>&1
)
SET DISMErrCode=%errorlevel%
ECHO %DATE% %TIME%[Log TRACE]  got return code [%DISMErrCode%] >>%LogPath%
if %DISMErrCode% neq 0 (
	ECHO !DATE! !TIME![Log TRACE]  Return code is [%DISMErrCode%] >>%LogPath%
	ECHO !DATE! !TIME![Log TRACE]  Trying to checking if install on offline=[/image:%OSDST%\] >>%LogPath%
	if /i "%ImgPath%" equ "/image:%OSDST%\" (
		if exist CheckingVCLib2_Done.tag (
			ECHO !DATE! !TIME![Log TRACE]  CheckingVCLib2_Done.tag found. No need to call :CheckingVCLib2 again. >>%LogPath%
		) else (
			ECHO !DATE! !TIME![Log TRACE]  This is offline installation process call :CheckingVCLib2 x64 >>%LogPath%
			call :CheckingVCLib2 x64 >>%LogPath% 2>&1
			ECHO !DATE! !TIME![Log TRACE]  Retry to install, goto :InstallUWP >>%LogPath%
			ECHO !DATE! !TIME![Log TRACE]  Retry to install, goto :InstallUWP >CheckingVCLib2_Done.tag
			goto :InstallUWP
		)
	) else (
		ECHO !DATE! !TIME![Log TRACE]  This is online process. No need to call :CheckingVCLib2 >>%LogPath%
	)
	ECHO !DATE! !TIME![Log TRACE]  Return code is [%DISMErrCode%], pop up error message to remind user. >>%LogPath%
	call :ErrorMsg
) else (
	ECHO !DATE! !TIME![Log TRACE]  Return code is [%DISMErrCode%], leave. >>%LogPath%
)

timeout /t 5 >NUL 2>&1
ECHO Installaton process completed.
ECHO Press any key to exit
PAUSE >NUL

:END
if exist "%temp%\scratchdir" rd /s /q "%temp%\scratchdir"
popd
SETLOCAL DISABLEDELAYEDEXPANSION
ECHO %DATE% %TIME%[Log LEAVE]  ============ %~dpnx0 ============ >> %LogPath%
ECHO.>>%LogPath%
exit /b 0



:GetParameter
:GetPackagePath_Start
REM Prepare PackagePath
if exist %OSDST%\OEM\Appx_list.txt del /f /q %OSDST%\OEM\Appx_list.txt
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.appxbundle to %OSDST%\OEM\Appx_list.txt
dir /s /b *.appxbundle >> %OSDST%\OEM\Appx_list.txt

ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.msix to %OSDST%\OEM\Appx_list.txt
dir /s /b *.msix >> %OSDST%\OEM\Appx_list.txt

ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.msixbundle to %OSDST%\OEM\Appx_list.txt
dir /s /b *.msixbundle >> %OSDST%\OEM\Appx_list.txt

ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.appx to %OSDST%\OEM\Appx_list.txt
dir /s /b *.appx >> %OSDST%\OEM\Appx_list.txt

ECHO %DATE% %TIME%[Log TRACE]  type %OSDST%\OEM\Appx_list.txt
ECHO.
ECHO ================================
type %OSDST%\OEM\Appx_list.txt
ECHO ================================
ECHO.
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b Microsoft*.* to %OSDST%\OEM\DependencyAppx_list.txt
dir /s /b Microsoft*.*>%OSDST%\OEM\DependencyAppx_list.txt
ECHO %DATE% %TIME%[Log TRACE]  type %OSDST%\OEM\DependencyAppx_list.txt
ECHO.
ECHO ================================
type %OSDST%\OEM\DependencyAppx_list.txt
ECHO ================================
ECHO.
for /f "delims=" %%f in (%OSDST%\OEM\Appx_list.txt) do (
	ECHO ------------------------------------------------------------------------------
	ECHO !DATE! !TIME![Log TRACE]  Checking [%%~nf] if main Package...
	ECHO !DATE! !TIME![Log TRACE]  find /i "%%~nf" %OSDST%\OEM\DependencyAppx_list.txt
	find /i "%%~nf" %OSDST%\OEM\DependencyAppx_list.txt
	if !errorlevel! neq 0 (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxf] did not exist in the %OSDST%\OEM\DependencyAppx_list.txt, set PackagePath=%%f
		set PackagePath=%%f
	) else (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxf] exist in the %OSDST%\OEM\DependencyAppx_list.txt, not the main package.
	)
)
:GetPackagePath_END
ECHO %DATE% %TIME%[Log TRACE]  PackagePath is [%packagepath%]
ECHO.

REM Prepare LicensePath
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *License*.xml
dir /s /b *License*.xml
for /f "delims=" %%d in ('dir /s /b *License*.xml') do (
	set licensepath=%%d
)
ECHO %DATE% %TIME%[Log TRACE]  LicensePath is [%licensepath%]
ECHO.

REM Prepare DependencyPackagePath
:::: init DependencyPackage_X86 and DependencyPackage_X64
set DependencyPackage_X86=
set DependencyPackage_X64=
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b Microsoft*_x86*.*
dir /s /b Microsoft*_x86*.*
for /f "delims=" %%d in ('dir /s /b Microsoft*_x86*.*') do (
	ECHO ---
	ECHO !DATE! !TIME![Log TRACE]  find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt
	find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt
	if !errorlevel! neq 0 (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] can be added to dependencypackagepath
		set DependencyPackage_X86=!DependencyPackage_X86!/dependencypackagepath:"%%d" 
	) else ( ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] should be filtered from dependencypackagepath )
	ECHO !DATE! !TIME![Log TRACE]  Now DependencyPackage_X86 is [!DependencyPackage_X86!]
)
ECHO.

ECHO %DATE% %TIME%[Log TRACE]  dir /s /b Microsoft*_x64*.*
dir /s /b Microsoft*_x64*.*
for /f "delims=" %%d in ('dir /s /b Microsoft*_x64*.*') do (
	ECHO ---
	ECHO !DATE! !TIME![Log TRACE]  find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt
	find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt
	if !errorlevel! neq 0 (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] can be added to dependencypackagepath
		set DependencyPackage_X64=!DependencyPackage_X64!/dependencypackagepath:"%%d" 
	) else ( ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] should be filtered from dependencypackagepath )
	ECHO !DATE! !TIME![Log TRACE]  Now DependencyPackage_X64 is [!DependencyPackage_X64!]
)
ECHO.

ECHO %DATE% %TIME%[Log TRACE]  Merge _x86.appx and _x64.appx For X64
set DependencyPackage_X64=%DependencyPackage_X86%%DependencyPackage_X64%
ECHO %DATE% %TIME%[Log TRACE]  After merge DependencyPackage_X64 is [%DependencyPackage_X64%]
ECHO.
exit /b 0



:ErrorMsg
if exist %OSDST%\OEM\NAPP\D2D.tag (
	ECHO %DATE% %TIME%[Log TRACE]  %OSDST%\OEM\NAPP\D2D.tag found, exit /b 0 >>%LogPath%
	exit /b 0
)

ECHO MsgBox "ERROR! Driver UWP Install process failed with ERROR code %DISMErrCode%",16,"Driver UWP Installation Detection" >"%temp%\ErrorMsg.vbs"
CScript /nologo "%temp%\ErrorMsg.vbs"
CScript /nologo "%temp%\ErrorMsg.vbs"
CScript /nologo "%temp%\ErrorMsg.vbs"
exit /b 0




:CheckingVCLib
:GetVCLibs_Start
SET CheckingVCLib_Now=%TargetVCLib%*_%1*.appx
ECHO -----------------------------------------------------------------
ECHO %DATE% %TIME%[Log TRACE]  Current working directory is [!CD!]
ECHO %DATE% %TIME%[Log TRACE]  dir /b %CheckingVCLib_Now%
dir /b %CheckingVCLib_Now%
if %errorlevel% neq 0 ECHO %DATE% %TIME%[Log TRACE]  dir /b %CheckingVCLib_Now% not exist, exit /b 0 && exit /b 0

for /f "tokens=1,2 delims=_" %%I in ('dir /b %CheckingVCLib_Now%') do (
	ECHO !DATE! !TIME![Log TRACE]  set VCLibsName=[%%I], VCLibsVer=[%%J]
	set VCLibsName=%%I
	set VCLibsVer=%%J
	goto :GetVCLibs_End
)
:GetVCLibs_End
pushd "%~dp0"
ECHO %DATE% %TIME%[Log TRACE]  powershell.exe -noprofile -executionpolicy unrestricted -command ".\CheckingVCLib.ps1 %VCLibsName% %VCLibsVer% %1; exit $LASTEXITCODE"
powershell.exe -noprofile -executionpolicy unrestricted -command ".\CheckingVCLib.ps1 %VCLibsName% %VCLibsVer% %1; exit $LASTEXITCODE"
SET ExitCode=%errorlevel%
ECHO %DATE% %TIME%[Log TRACE]  Getting return code is %ExitCode%
popd
if %ExitCode% equ 5 (
	ECHO !DATE! !TIME![Log TRACE]  Skip below dependency package list from UWP source
	dir /b %CheckingVCLib_Now% >>%OSDST%\OEM\FilterVCLibsList.txt
	ECHO !DATE! !TIME![Log TRACE]  type %OSDST%\OEM\FilterVCLibsList.txt
	ECHO.
	ECHO ------------
	type %OSDST%\OEM\FilterVCLibsList.txt
	ECHO ------------
	ECHO.
) else (
	ECHO !DATE! !TIME![Log TRACE]  OS with older version, no need to filter.
)
ECHO -----------------------------------------------------------------
exit /b 0


:CheckingVCLib2
SET CheckingVCLib_Now=%TargetVCLib%*_%1*.appx
SET CheckingWinAppsDir=%TargetVCLib%*_%1*
ECHO !DATE! !TIME![Log TRACE]  dir /s /b "%CheckingVCLib_Now%"
dir /s /b "%CheckingVCLib_Now%"
for /f "delims=" %%I in ('dir /s /b "%CheckingVCLib_Now%"') do (
	pushd "%%~dpI"
	ECHO !DATE! !TIME![Log TRACE]  call :CheckingVCLib2_Main
	call :CheckingVCLib2_Main
	popd
)
exit /b 0

:CheckingVCLib2_Main
:GetVCLibs_Start2
ECHO -----------------------------------------------------------------
ECHO %DATE% %TIME%[Log TRACE]  Current working directory is [!CD!]

for /f "tokens=1,2,* delims=_" %%I in ('dir /b %CheckingVCLib_Now%') do (
	ECHO !DATE! !TIME![Log TRACE]  set VCLibsName=[%%I], VCLibsVer=[%%J], VCLibsRest=[%%K]
	set VCLibsName=%%I
	set VCLibsVer=%%J
	set VCLibsRest=%%K
	goto :GetVCLibs_End2
)
:GetVCLibs_End2
ECHO !DATE! !TIME![Log TRACE]  Set the ExitCode=[9] as default
set ExitCode=9
ECHO !DATE! !TIME![Log TRACE]  dir /b /ad "%OSDST%\Program Files\WindowsApps\%CheckingWinAppsDir%"
dir /b /ad "%OSDST%\Program Files\WindowsApps\%CheckingWinAppsDir%"
for /f "tokens=1,2,* delims=_" %%I in ('dir /b /ad "%OSDST%\Program Files\WindowsApps\%CheckingWinAppsDir%"') do (
	ECHO !DATE! !TIME![Log TRACE]  Current VCLibs in the OS :[%%I]_[%%J]_[%%K]
	if %VCLibsVer% LEQ %%J (
		ECHO !DATE! !TIME![Log TRACE]  %VCLibsVer% LEQ %%J, set ExitCode=[5]
		set ExitCode=5
	)
)
ECHO %DATE% %TIME%[Log TRACE]  The final return code is %ExitCode%
if %ExitCode% equ 5 (
	ECHO !DATE! !TIME![Log TRACE]  Skip [%TargetVCLib%*.appx] dependency package list from UWP source
	dir /b %TargetVCLib%*.appx >>%OSDST%\OEM\FilterVCLibsList.txt
	ECHO !DATE! !TIME![Log TRACE]  type %OSDST%\OEM\FilterVCLibsList.txt
	ECHO.
	ECHO ------------
	type %OSDST%\OEM\FilterVCLibsList.txt
	ECHO ------------
	ECHO.
) else (
	ECHO !DATE! !TIME![Log TRACE]  OS with older version, no need to filter.
)
ECHO -----------------------------------------------------------------
exit /b 0
