$ErrorActionPreference = "Stop" $BaseUrl = "https://stoicsoft.net/phonerc/downloads" $InstallDir = if ($env:PHONERC_COMPANION_HOME) { $env:PHONERC_COMPANION_HOME } else { Join-Path $env:LOCALAPPDATA "PhoneRC Companion" } if (Get-Command py -ErrorAction SilentlyContinue) { $PythonExe = "py"; $PythonPrefix = @("-3") } elseif (Get-Command python -ErrorAction SilentlyContinue) { $PythonExe = "python"; $PythonPrefix = @() } else { throw "Python 3.8 or newer is required. Install Python, then try again." } & $PythonExe @PythonPrefix -c "import sys; raise SystemExit(sys.version_info < (3, 8))" if ($LASTEXITCODE -ne 0) { throw "Python 3.8 or newer is required." } if (-not (Get-Command git -ErrorAction SilentlyContinue)) { throw "Git is required to install the Windows Bluetooth dependency." } $TempDir = Join-Path ([IO.Path]::GetTempPath()) ("phonerc-" + [guid]::NewGuid()) New-Item -ItemType Directory -Path $TempDir | Out-Null try { $OuterZip = Join-Path $TempDir "companion.zip" $OuterDir = Join-Path $TempDir "outer" Write-Host "Downloading PhoneRC Companion for Windows..." Invoke-WebRequest "$BaseUrl/phonerc-companion-windows.zip" -OutFile $OuterZip Expand-Archive $OuterZip -DestinationPath $OuterDir $InnerZip = Get-ChildItem $OuterDir -Filter *.zip -Recurse | Select-Object -First 1 if ($InnerZip) { $PackageDir = Join-Path $TempDir "package" Expand-Archive $InnerZip.FullName -DestinationPath $PackageDir $SourceDir = Join-Path $PackageDir "shortcut-launcher" } else { $SourceDir = Join-Path $OuterDir "shortcut-launcher" } if (-not (Test-Path (Join-Path $SourceDir "shortcut_launcher.py"))) { throw "Invalid download." } New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null $ExistingConfig = Join-Path $InstallDir "config.json" $SavedConfig = Join-Path $TempDir "config.json" if (Test-Path $ExistingConfig) { Copy-Item $ExistingConfig $SavedConfig } Copy-Item (Join-Path $SourceDir "*") $InstallDir -Recurse -Force if (Test-Path $SavedConfig) { Copy-Item $SavedConfig $ExistingConfig -Force } $VenvDir = Join-Path $InstallDir ".venv" & $PythonExe @PythonPrefix -m venv $VenvDir $VenvPython = Join-Path $VenvDir "Scripts\python.exe" & $VenvPython -m pip install --disable-pip-version-check -r (Join-Path $InstallDir "requirements.txt") $SelfTest = (& $VenvPython (Join-Path $InstallDir "shortcut_launcher.py") --self-test | Out-String) Write-Host $SelfTest if ($SelfTest -notmatch '(?m)^companion supported: yes\r?$') { throw "PhoneRC Companion prerequisites are incomplete; automatic startup was not configured." } $Pythonw = Join-Path $VenvDir "Scripts\pythonw.exe" $Launcher = Join-Path ([Environment]::GetFolderPath("Startup")) "PhoneRC Companion.cmd" Set-Content $Launcher "@start `"PhoneRC Companion`" /min `"$Pythonw`" `"$InstallDir\shortcut_launcher.py`" --both" -Encoding ASCII $LauncherScript = Join-Path $InstallDir "shortcut_launcher.py" Start-Process $Pythonw -ArgumentList @("`"$LauncherScript`"", "--both") -WorkingDirectory $InstallDir Write-Host "PhoneRC Companion is installed, running, and will start with Windows." Write-Host "Installed at: $InstallDir" } finally { Remove-Item $TempDir -Recurse -Force -ErrorAction SilentlyContinue }