-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwsl-install-striptracks.ps1
131 lines (108 loc) · 4.91 KB
/
wsl-install-striptracks.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<#
.Synopsis
This script installs striptracks.sh in a Windows Subsystem for Linux (WSL) environment.
.Description
radarr-striptracks is a Docker Mod for the Linuxserver.io Radarr/Sonarr containers.
This installation script makes it work outside of Docker in a virtual WSL environment
on Windows.
.Link
https://github.com/TheCaptain989/radarr-striptracks
.Example
wsl-install-striptracks -Release "v2.9.0"
This changes the default release.
#>
#requires -Version 3
[CmdletBinding(PositionalBinding = $False)]
#region Initialize parameters
param (
# WSL user password (for sudo command)
# This is not stored and only used to pass to sudo for required package installations
[Parameter(Mandatory = $true, HelpMessage = "Enter your WSL user password (this will not be stored)")]
[SecureString]$Password = (Read-Host -AsSecureString "Enter your WSL user password (this will not be stored)"),
# Directory to install striptracks to
[string]$Directory = "$env:ProgramData\striptracks",
# GitHub repository owner
[string]$Owner = "TheCaptain989",
# GitHub repository name
[string]$Repository = "radarr-striptracks",
# GitHub respository release tag
[string]$Release = "latest",
# GitHub API root URL
[string]$GhApiRoot = "https://api.github.com"
)
#endregion
# Uneditable initial parameters
$GhApiHeaders = @{"Accept"="application/vnd.github+json"; "X-GitHub-Api-Version"="2022-11-28"}
$ZipFile = "$Directory\striptracks-$Release.zip"
# Functions
function Test-WSL {
# Check that WSL is installed
$local:WSLStatus = wsl --status
if ($LASTEXITCODE -ne 0) {
switch ($LASTEXITCODE) {
50 { Write-Error -Message "WSL does not appear to be installed. Run 'wsl --install' first." -Category NotInstalled -TargetObject $WSLStatus }
default { Write-Error -Message "Error $LASTEXITCODE when attempting to check WSL status." -TargetObject $WSLStatus }
}
}
return $LASTEXITCODE
}
function Install-LinuxPackages {
# Install the required Linux packages
$local:PlanTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
$local:WSLStatus = wsl -- echo "$PlanTextPassword" `| sudo -S bash -c "apt update && apt install mkvtoolnix jq" 2>&1
if ($LASTEXITCODE -ne 0) {
switch ($LASTEXITCODE) {
1 { Write-Error -Message "Your password is incorrect." -Category AuthenticationError -TargetObject $WSLStatus }
default { Write-Error -Message "Error $LASTEXITCODE when attempting to install required Linux packages." -TargetObject $WSLStatus }
}
}
return $LASTEXITCODE
}
function Expand-ZipFile {
# Unzip the necessary script files from the archive
Add-Type -AssemblyName System.IO.Compression.FileSystem
$ZipObj = [System.IO.Compression.ZipFile]::OpenRead($ZipFile)
$ZipEntries = $ZipObj.Entries | Where-Object { $_.FullName -like "*/wsl/wsl-*.cmd" -or $_.Name -eq "striptracks.sh" }
foreach ($Entry in $ZipEntries) {
[IO.Compression.ZipFileExtensions]::ExtractToFile($Entry, "$Directory\$($Entry.Name)", $true)
}
$ZipObj.Dispose()
return $LASTEXITCODE
}
# Save working directory
$OrgDirectory = $pwd
# Check that WSL is installed
Write-Output "Checking WSL status..."
if ((Test-WSL) -ne 0) { return }
# Install the required Linux packages
Write-Output "Installing required Linux packages..."
if ((Install-LinuxPackages) -ne 0) { return }
# Create the new directory if it doesn't already exist and change to it
if (-not (Test-Path $Directory)) {
Write-Output "Creating $Directory"
New-Item -ItemType Directory $Directory | Out-Null
}
Set-Location -Path $Directory
# Query GitHub for release version
Write-Output "Getting striptracks release info..."
$ApiResponse = (Invoke-WebRequest -Headers $GhApiHeaders -Uri "$GhApiRoot/repos/$Owner/$Repository/releases/$Release").Content | ConvertFrom-Json
$ModVersion = $ApiResponse.tag_name
# Download striptracks ZIP archive
Write-Output "Downloading striptracks ZIP archive..."
Invoke-WebRequest -Headers $GhApiHeaders -Uri $ApiResponse.zipball_url -OutFile $ZipFile
# Unzip files
Write-Output "Extracting files from ZIP archive..."
if ((Expand-ZipFile) -ne 0) { return }
# Edit some script files
(Get-Content -Path "$Directory\wsl-striptracks.cmd") -replace "set STRIPTRACKS_ROOT=%ProgramData%\\striptracks", "set STRIPTRACKS_ROOT=$Directory" | Set-Content -Path "$Directory\wsl-striptracks.cmd"
# This method preserves Linux newline endings
Set-Content -Path "$Directory\striptracks.sh" -NoNewline -Value (((Get-Content -Path "$Directory\striptracks.sh") -replace "{{VERSION}}", $ModVersion -join "`n") + "`n")
# Close and remove the ZIP archive
Write-Output "Deleting ZIP archive"
Remove-Item -Path $ZipFile
# Make the striptracks.sh script executable
Write-Output "Making striptracks.sh executable"
wsl chmod +x striptracks.sh
# Exit
Set-Location -Path $OrgDirectory.Path
Write-Output "striptracks has been installed to $Directory"