-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathinstall.ps1
More file actions
executable file
·157 lines (140 loc) · 4.4 KB
/
install.ps1
File metadata and controls
executable file
·157 lines (140 loc) · 4.4 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Install mpvacious using Powershell on Windows.
# Based on: https://github.com/tomasklaen/uosc/tree/b77c1f95a877979bd5acef63bad84b03275a18af/installers
# Fetch the latest version from GitHub API
try {
$LatestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/Ajatt-Tools/mpvacious/releases/latest" -ErrorAction Stop
$LatestVersion = $LatestRelease.tag_name
$ZipURL = "https://github.com/Ajatt-Tools/mpvacious/releases/latest/download/mpvacious_$LatestVersion.zip"
} catch {
# Abort if API request fails since we don't know URL to zip
Write-Output "Error: Couldn't fetch the latest version from GitHub API."
Write-Output "Aborting!"
Exit 1
}
$ConfURL = "https://github.com/Ajatt-Tools/mpvacious/releases/latest/download/subs2srs.conf"
$Files = "scripts/mpvacious", "scripts/subs2srs"
# Determine install directory
if (Test-Path env:MPV_CONFIG_DIR) {
Write-Output "Installing into (MPV_CONFIG_DIR):"
$ConfigDir = "$env:MPV_CONFIG_DIR"
}
elseif (Test-Path "$PWD/portable_config") {
Write-Output "Installing into (portable config):"
$ConfigDir = "$PWD/portable_config"
}
elseif ($PWD.Path.Split('\/') -contains "portable_config") {
$Current = Get-Item $PWD
while ($Current -ne $null -and $Current.Name -ne "portable_config") {
$Current = $Current.Parent
}
Write-Output "Installing into (portable config):"
$ConfigDir = $Current.FullName
}
else {
Write-Output "Installing into (current user config):"
$ConfigDir = "$env:APPDATA/mpv"
if (!(Test-Path $ConfigDir)) {
Write-Output "Creating folder: $ConfigDir"
New-Item -ItemType Directory -Force -Path $ConfigDir > $null
}
}
$MpvScriptsDir = "$ConfigDir/scripts"
if (!(Test-Path $MpvScriptsDir)) {
Write-Output "Creating folder: $MpvScriptsDir"
New-Item -ItemType Directory -Force -Path $MpvScriptsDir > $null
}
Write-Output "-> $ConfigDir"
$BackupDir = "$ConfigDir/.mpvacious-backup"
$ZipFile = "$ConfigDir/mpvacious_tmp.zip"
function DeleteIfExists($Path) {
if (Test-Path $Path) {
Remove-Item -LiteralPath $Path -Force -Recurse > $null
}
}
Function Abort($Message) {
Write-Output "Error: $Message"
Write-Output "Aborting!"
DeleteIfExists($ZipFile)
Write-Output "Deleting potentially broken install..."
foreach ($File in $Files) {
DeleteIfExists("$ConfigDir/$File")
}
Write-Output "Restoring backup..."
foreach ($File in $Files) {
$FromPath = "$BackupDir/$File"
if (Test-Path $FromPath) {
$ToPath = "$ConfigDir/$File"
$ToDir = Split-Path $ToPath -parent
New-Item -ItemType Directory -Force -Path $ToDir > $null
Move-Item -LiteralPath $FromPath -Destination $ToPath -Force > $null
}
}
Write-Output "Deleting backup..."
DeleteIfExists($BackupDir)
Exit 1
}
# Ensure install directory exists
if (!(Test-Path -Path $ConfigDir -PathType Container)) {
if (Test-Path -Path $ConfigDir -PathType Leaf) {
Abort("Config directory is a file.")
}
try {
New-Item -ItemType Directory -Force -Path $ConfigDir > $null
}
catch {
Abort("Couldn't create config directory.")
}
}
Write-Output "Backing up..."
foreach ($File in $Files) {
$FromPath = "$ConfigDir/$File"
if (Test-Path $FromPath) {
$ToPath = "$BackupDir/$File"
$ToDir = Split-Path $ToPath -parent
try {
New-Item -ItemType Directory -Force -Path $ToDir > $null
}
catch {
Abort("Couldn't create backup folder: $ToDir")
}
try {
Move-Item -LiteralPath $FromPath -Destination $ToPath -Force > $null
}
catch {
Abort("Couldn't move '$FromPath' to '$ToPath'.")
}
}
}
# Install new version
Write-Output "Downloading archive..."
try {
Invoke-WebRequest -OutFile $ZipFile -Uri $ZipURL > $null
}
catch {
Abort("Couldn't download: $ZipURL")
}
Write-Output "Extracting archive..."
try {
Expand-Archive $ZipFile -DestinationPath $MpvScriptsDir -Force > $null
}
catch {
Abort("Couldn't extract: $ZipFile")
}
Write-Output "Deleting archive..."
DeleteIfExists($ZipFile)
Write-Output "Deleting backup..."
DeleteIfExists($BackupDir)
# Download default config if one doesn't exist yet
try {
$ScriptOptsDir = "$ConfigDir/script-opts"
$ConfFile = "$ScriptOptsDir/subs2srs.conf"
if (!(Test-Path $ConfFile)) {
Write-Output "Config not found, downloading default subs2srs.conf..."
New-Item -ItemType Directory -Force -Path $ScriptOptsDir > $null
Invoke-WebRequest -OutFile $ConfFile -Uri $ConfURL > $null
}
}
catch {
Abort("Couldn't download the config file, but mpvacious should be installed correctly.")
}
Write-Output "mpvacious has been installed."