# Create .bginfo directory in user's home folder
$bginfoDir = "$env:USERPROFILE\.bginfo"
New-Item -ItemType Directory -Force -Path $bginfoDir

# Download BGInfo
$bginfoUrl = "https://download.sysinternals.com/files/BGInfo.zip"
$bginfoZip = "$env:TEMP\BGInfo.zip"
Invoke-WebRequest -Uri $bginfoUrl -OutFile $bginfoZip

# Extract BGInfo
Expand-Archive -Path $bginfoZip -DestinationPath "$env:TEMP\BGInfo" -Force

# Copy BGInfo files to .bginfo directory
Copy-Item "$env:TEMP\BGInfo\*" -Destination $bginfoDir -Force

# Create VBS scripts directory
$vbsDir = "$bginfoDir\scripts"
New-Item -ItemType Directory -Force -Path $vbsDir

# Download VBS scripts from GitHub
$vbsFiles = @(
    "cpuname.vbs",
    "ipaddr.vbs",
    "gpu.vbs",
    "igpu.vbs",
    "ram.vbs",
    "hdinfo.vbs",
    "networkspeed.vbs",
    "osversion.vbs",
    "sshstatus.vbs"
)

$baseUrl = "https://raw.githubusercontent.com/robojerk/Leanback_11/main/bginfo/scripts"
foreach ($file in $vbsFiles) {
    $url = "$baseUrl/$file"
    $output = "$vbsDir\$file"
    Invoke-WebRequest -Uri $url -OutFile $output
}

# Download config.bgi from GitHub
$configUrl = "https://raw.githubusercontent.com/robojerk/Leanback_11/main/bginfo/config.bgi"
Invoke-WebRequest -Uri $configUrl -OutFile "$bginfoDir\config.bgi"

# Create scheduled task to run BGInfo at startup
$action = New-ScheduledTaskAction -Execute "$bginfoDir\Bginfo.exe" -Argument "$bginfoDir\config.bgi /silent /timer:60"
$trigger = New-ScheduledTaskTrigger -AtStartup
$principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries

Register-ScheduledTask -TaskName "BGInfo" -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force

# Cleanup
Remove-Item $bginfoZip -Force
Remove-Item "$env:TEMP\BGInfo" -Recurse -Force

Write-Host "BGInfo has been installed and configured successfully!"
