
@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\Useralaunch.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...

md scratchdir
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
)

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

if exist %OSDST%\OEM\FilterVCLibsList.txt del /f /q %OSDST%\OEM\FilterVCLibsList.txt >NUL 2>&1
SET TargetVCLib=Microsoft.VCLibs.140.00_

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
	REM ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:scratchdir>>%LogPath% 2>&1
	REM dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:scratchdir>>%LogPath% 2>&1
	ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/region:all /scratchdir:scratchdir>>%LogPath% 2>&1
	dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X64!/region:all /scratchdir:scratchdir>>%LogPath% 2>&1
) else (	
	call :GetParameter
	REM ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:scratchdir>>%LogPath% 2>&1
	REM dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/CustomDataPath:"%CustomDATA%" /region:all /scratchdir:scratchdir>>%LogPath% 2>&1
	ECHO %DATE% %TIME%[Log TRACE]  dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/region:all /scratchdir:scratchdir>>%LogPath% 2>&1
	dism %ImgPath% /english /add-provisionedappxpackage /packagepath:"!packagepath!" /licensepath:"!licensepath!" !DependencyPackage_X86!/region:all /scratchdir: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%], 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 scratchdir rd /s /q scratchdir
popd
SETLOCAL DISABLEDELAYEDEXPANSION
ECHO %DATE% %TIME%[Log LEAVE]  ============ %~dpnx0 ============ >> %LogPath%
ECHO.>>%LogPath%
exit /b 0



:GetParameter
:GetPackagePath_Start
REM Prepare PackagePath
:Method1
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.appxbundle >> %LogPath%
dir /s /b *.appxbundle >> %LogPath% 2>&1
(for /f "delims=" %%d in ('dir /s /b *.appxbundle') do (
	set packagepath=%%d
))>NUL 2>&1

:Method2
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.msix >> %LogPath%
dir /s /b *.msix >> %LogPath% 2>&1
(for /f "delims=" %%d in ('dir /s /b *.msix') do (
	set packagepath=%%d
))>NUL 2>&1

:Method3
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.msixbundle >> %LogPath%
dir /s /b *.msixbundle >> %LogPath% 2>&1
(for /f "delims=" %%d in ('dir /s /b *.msixbundle') do (
	set packagepath=%%d
))>NUL 2>&1

:Method4
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b *.appx>> %LogPath% 2>&1
dir /s /b *.appx>%OSDST%\OEM\Appx_list.txt 2>&1
if %errorlevel% neq 0 (
	ECHO !DATE! !TIME![Log TRACE]  *.appx not found, goto :GetPackagePath_END >>%LogPath%
	goto :GetPackagePath_END
)
type %OSDST%\OEM\Appx_list.txt >> %LogPath%
ECHO %DATE% %TIME%[Log TRACE]  Getting Dependency Appx list>>%LogPath%
dir /s /b Microsoft*.appx>%OSDST%\OEM\DependencyAppx_list.txt 2>&1
type %OSDST%\OEM\DependencyAppx_list.txt >>%LogPath%
for /f "delims=" %%f in (%OSDST%\OEM\Appx_list.txt) do (
	ECHO ------------------------------------------------------------------------------ >>%LogPath%
	ECHO !DATE! !TIME![Log TRACE]  Checking [%%~nf] if main Package... >>%LogPath%
	ECHO !DATE! !TIME![Log TRACE]  find /i "%%~nf" %OSDST%\OEM\DependencyAppx_list.txt>>%LogPath% 2>&1
	find /i "%%~nf" %OSDST%\OEM\DependencyAppx_list.txt>>%LogPath% 2>&1
	if !errorlevel! neq 0 (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxf] did not exist in the %OSDST%\OEM\DependencyAppx_list.txt, set as PackagePath>>%LogPath%
		set PackagePath=%%f
	) else (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxf] exist in the %OSDST%\OEM\DependencyAppx_list.txt, not the main package. >>%LogPath%
	)
)


:GetPackagePath_END
ECHO %DATE% %TIME%[Log TRACE]  PackagePath is [%packagepath%]  >> %LogPath%
ECHO.>>%LogPath%

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

REM Prepare DependencyPackagePath
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b Microsoft*_x86*.appx >> %LogPath%
dir /s /b Microsoft*_x86*.appx >> %LogPath% 2>&1
(for /f "delims=" %%d in ('dir /s /b Microsoft*_x86*.appx') do (
	ECHO !DATE! !TIME![Log TRACE]  find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt >>%LogPath% 2>&1
	find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt >>%LogPath% 2>&1
	if !errorlevel! neq 0 (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] can be added to dependencypackagepath >>%LogPath%
		set DependencyPackage_X86=!DependencyPackage_X86!/dependencypackagepath:"%%d" 
	) else ( ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] should be filtered from dependencypackagepath >>%LogPath% )
	ECHO !DATE! !TIME![Log TRACE]  Now DependencyPackage_X86 is [!DependencyPackage_X86!] >>%LogPath%
))>NUL 2>&1
ECHO %DATE% %TIME%[Log TRACE]  dir /s /b Microsoft*_x64*.appx >> %LogPath%
dir /s /b Microsoft*_x64*.appx >> %LogPath% 2>&1
(for /f "delims=" %%d in ('dir /s /b Microsoft*_x64*.appx') do (
	ECHO !DATE! !TIME![Log TRACE]  find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt >>%LogPath% 2>&1
	find /i "%%~nxd" %OSDST%\OEM\FilterVCLibsList.txt >>%LogPath% 2>&1
	if !errorlevel! neq 0 (
		ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] can be added to dependencypackagepath >>%LogPath%
		set DependencyPackage_X64=!DependencyPackage_X64!/dependencypackagepath:"%%d" 
	) else ( ECHO !DATE! !TIME![Log TRACE]  [%%~nxd] should be filtered from dependencypackagepath >>%LogPath% )
	ECHO !DATE! !TIME![Log TRACE]  Now DependencyPackage_X64 is [!DependencyPackage_X64!] >>%LogPath%
))>NUL 2>&1
ECHO %DATE% %TIME%[Log TRACE]  Merge _x86.appx and _x64.appx For X64 >> %LogPath%
set DependencyPackage_X64=%DependencyPackage_X86%%DependencyPackage_X64%
ECHO %DATE% %TIME%[Log TRACE]  After merge DependencyPackage_X64 is [%DependencyPackage_X64%] >>%LogPath%
ECHO.>>%LogPath%
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" >ErrorMsg.vbs
CScript /nologo ErrorMsg.vbs
CScript /nologo ErrorMsg.vbs
CScript /nologo ErrorMsg.vbs
exit /b 0




:CheckingVCLib
:GetVCLibs_Start
SET CheckingVCLib_Now=%TargetVCLib%*_%1*.appx
ECHO.
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
	type %OSDST%\OEM\FilterVCLibsList.txt
) else (
	ECHO !DATE! !TIME![Log TRACE]  OS with older version, no need to filter.
)
ECHO -----------------------------------------------------------------
ECHO.
exit /b 0

