switch Windows to to Env:USERPROFILE
[webi-installers/.git] / _webi / webi.ps1
1 #!/usr/bin/env pwsh
2
3 # If a command returns an error, halt the script.
4 $ErrorActionPreference = 'Stop'
5
6 # Ignore progress events from cmdlets so Invoke-WebRequest is not painfully slow
7 $ProgressPreference = 'SilentlyContinue'
8
9 # TODO get arch
10 $Env:WEBI_UA = 'Windows/10 amd64'
11 $exename = $args[0]
12
13 # Switch to userprofile
14 pushd $Env:USERPROFILE
15
16 # Make paths if needed
17 if (!(Test-Path -Path .local\bin))
18 {
19     New-Item -Path .local\bin -ItemType Directory
20 }
21 if (!(Test-Path -Path .local\xbin))
22 {
23     New-Item -Path .local\xbin -ItemType Directory
24 }
25 Set-Content -Path .local\bin\webi.bat -Value "@echo off`r`npushd %USERPROFILE%`r`npowershell -ExecutionPolicy Bypass .local\bin\webi.ps1 %1`r`npopd"
26 if (!(Test-Path -Path .local\opt))
27 {
28     New-Item -Path .local\opt -ItemType Directory
29 }
30 # TODO windows version of mktemp -d
31 if (!(Test-Path -Path .local\tmp))
32 {
33     New-Item -Path .local\tmp -ItemType Directory
34 }
35
36 # TODO SetStrictMode
37 # TODO Test-Path variable:global:Env:WEBI_HOST ???
38 IF($Env:WEBI_HOST -eq $null -or $Env:WEBI_HOST -eq "")
39 {
40     $Env:WEBI_HOST = "https://webinstall.dev"
41 }
42
43 if (!(Test-Path -Path .local\bin\pathman.exe))
44 {
45     & curl.exe -fsSL -A "$Env:WEBI_UA" "$Env:WEBI_HOST/packages/pathman/install.ps1" -o .\.local\tmp\pathman-setup.ps1
46     powershell .\.local\tmp\pathman-setup.ps1
47     # TODO del .\.local\tmp\pathman-setup.bat
48 }
49
50 # Run pathman to set up the folder
51 & "$Env:USERPROFILE\.local\bin\pathman.exe" add "$Env:USERPROFILE\.local\bin"
52 #& "$Env:USERPROFILE\.local\bin\pathman.exe" add %USERPROFILE%\.local\bin
53
54 # {{ baseurl }}
55 # {{ version }}
56
57 # Fetch <whatever>.ps1
58 # TODO detect formats
59 $PKG_URL = "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,exe,tar"
60 echo "Downloading $PKG_URL"
61 # Invoke-WebRequest -UserAgent "Windows amd64" "$PKG_URL" -OutFile ".\.local\tmp\$exename.install.ps1"
62 & curl.exe -fsSL -A "$Env:WEBI_UA" "$PKG_URL" -o .\.local\tmp\$exename.install.ps1
63
64 # Run <whatever>.ps1
65 powershell .\.local\tmp\$exename.install.ps1
66
67 # Done
68 popd