build: improved install scripts

The install directory can now be force with `MPV_CONFIG_DIR` environment variable.

Linux installer now detects common Flatpak and Snap packages and installs into their config locations.

All installers now backup current files and restore them when anything goes wrong.
This commit is contained in:
tomasklaen
2023-10-17 22:12:02 +02:00
parent 48ee23eeec
commit 03ff5cf9ec
3 changed files with 194 additions and 78 deletions

View File

@@ -59,6 +59,17 @@ Notable features:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/tomasklaen/uosc/HEAD/installers/unix.sh)"
```
On Linux, we try to detect what package manager variant of the config location you're using, with precedent being:
```
~/.var/app/io.mpv.Mpv (flatpak)
~/snap/mpv
~/snap/mpv-wayland
~/.config/mpv
```
To install into any of these locations, make sure the ones above it don't exist.
### Manual
1. Download & extract [`uosc.zip`](https://github.com/tomasklaen/uosc/releases/latest/download/uosc.zip) into your mpv config directory. (_See the [documentation of mpv config locations](https://mpv.io/manual/master/#files)._)

View File

@@ -2,57 +2,122 @@
zip_url=https://github.com/tomasklaen/uosc/releases/latest/download/uosc.zip
conf_url=https://github.com/tomasklaen/uosc/releases/latest/download/uosc.conf
zip_file=/tmp/uosc.zip
files=("scripts/uosc" "fonts/uosc_icons.otf" "fonts/uosc_textures.ttf" "scripts/uosc_shared" "scripts/uosc.lua")
dependencies=(curl unzip)
# Exit immediately if a command exits with a non-zero status
set -e
cleanup() {
if [ -f "$zip_file" ]; then
echo "Deleting: $zip_file"
rm -f $zip_file
fi
}
abort() {
cleanup
echo "Error: $1"
echo "Aborting!"
rm -f $zip_file || true
echo "Deleting potentially broken install..."
for file in ${files[@]}
do
rm -rf "$config_dir/$file" || true
done
echo "Restoring backup..."
for file in ${files[@]}
do
from_path="$backup_dir/$file"
if [[ -e "$from_path" ]]; then
to_path="$config_dir/$file"
to_dir="$(dirname "${to_path}")"
mkdir -pv $to_dir || true
mv $from_path $to_path || true
fi
done
echo "Deleting backup..."
rm -rf $backup_dir || true
exit 1
}
# Check OS
OS="$(uname)"
if [ "${OS}" == "Linux" ]; then
data_dir="${XDG_CONFIG_HOME:-$HOME/.config}/mpv"
elif [ "${OS}" == "Darwin" ]; then
data_dir=~/Library/Preferences/mpv
else
abort "This install script works only on linux and macOS."
# Check dependencies
missing_dependencies=()
for name in ${dependencies[@]}
do
if [ ! -x "$(command -v $name)" ]; then
missing_dependencies+=($name)
fi
done
if [ ! ${#missing_dependencies[@]} -eq 0 ]; then
echo "Missing dependencies: ${missing_dependencies[@]}"
exit 1
fi
# Ensure directory exists
mkdir -pv $data_dir
# Determine install directory
OS="$(uname)"
if [ ! -z "${MPV_CONFIG_DIR}" ]; then
echo "Installing into (MPV_CONFIG_DIR):"
config_dir="${MPV_CONFIG_DIR}"
elif [ "${OS}" == "Linux" ]; then
# Flatpak
if [ -d "$HOME/.var/app/io.mpv.Mpv" ]; then
echo "Installing into (flatpak io.mpv.Mpv package):"
config_dir="$HOME/.var/app/io.mpv.Mpv/config/mpv"
# Remove old and deprecated folders & files
echo "Deleting old and deprecated uosc files and directories."
rm -rf "$data_dir/scripts/uosc_shared" || abort "Couldn't cleanup old files."
rm -rf "$data_dir/scripts/uosc" || abort "Couldn't cleanup old files."
rm -f "$data_dir/scripts/uosc.lua" || abort "Couldn't cleanup old files."
# Snap mpv
elif [ -d "$HOME/snap/mpv" ]; then
echo "Installing into (snap mpv package):"
config_dir="$HOME/snap/mpv/current/.config/mpv"
# Snap mpv-wayland
elif [ -d "$HOME/snap/mpv-wayland" ]; then
echo "Installing into (snap mpv-wayland package):"
config_dir="$HOME/snap/mpv-wayland/common/.config/mpv"
# ~/.config
else
echo "Config location:"
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/mpv"
fi
elif [ "${OS}" == "Darwin" ]; then
config_dir=~/Library/Preferences/mpv
else
abort "This install script works only on Linux and macOS."
fi
backup_dir="$config_dir/.uosc-backup"
echo "$config_dir"
mkdir -p $config_dir || abort "Couldn't create config directory."
echo "Backing up..."
rm -rf $backup_dir || abort "Couldn't cleanup backup directory."
for file in ${files[@]}
do
from_path="$config_dir/$file"
if [[ -e "$from_path" ]]; then
to_path="$backup_dir/$file"
to_dir="$(dirname "${to_path}")"
mkdir -p $to_dir || abort "Couldn't create backup folder: $to_dir"
mv $from_path $to_path || abort "Couldn't move '$from_path' to '$to_path'."
fi
done
# Install new version
echo "Downloading: $zip_url"
curl -L -o $zip_file $zip_url || abort "Couldn't download the archive."
echo "Extracting: $zip_file"
unzip -od $data_dir $zip_file || abort "Couldn't extract the archive."
cleanup
echo "Downloading archive..."
curl -Ls -o $zip_file $zip_url || abort "Couldn't download: $zip_url"
echo "Extracting archive..."
unzip -qod $config_dir $zip_file || abort "Couldn't extract: $zip_file"
echo "Deleting archive..."
rm -f $zip_file || echo "Couldn't delete: $zip_file"
echo "Deleting backup..."
rm -rf $backup_dir || echo "Couldn't delete: $backup_dir"
# Download default config if one doesn't exist yet
scriptopts_dir="$data_dir/script-opts"
scriptopts_dir="$config_dir/script-opts"
conf_file="$scriptopts_dir/uosc.conf"
if [ ! -f "$conf_file" ]; then
echo "Config not found, downloading default one..."
mkdir -pv $scriptopts_dir
echo "Downloading: $conf_url"
curl -L -o $conf_file $conf_url || abort "Couldn't download the config file, but uosc should be installed correctly."
echo "Config not found, downloading default uosc.conf..."
mkdir -p $scriptopts_dir || echo "Couldn't create: $scriptopts_dir"
curl -Ls -o $conf_file $conf_url || echo "Couldn't download: $conf_url"
fi
echo "uosc has been installed."

View File

@@ -1,89 +1,129 @@
$ZipURL = "https://github.com/tomasklaen/uosc/releases/latest/download/uosc.zip"
$ConfURL = "https://github.com/tomasklaen/uosc/releases/latest/download/uosc.conf"
$Files = "scripts/uosc", "fonts/uosc_icons.otf", "fonts/uosc_textures.ttf", "scripts/uosc_shared", "scripts/uosc.lua"
# Portable vs AppData install
if (Test-Path "$PWD/portable_config") {
$DataDir = "$PWD/portable_config"
Write-Output "Portable mode: $DataDir"
# 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 ((Get-Item -Path $PWD).BaseName -eq "portable_config") {
$DataDir = "$PWD"
Write-Output "Portable mode: $DataDir"
Write-Output "Installing into (portable config):"
$ConfigDir = "$PWD"
}
else {
$DataDir = "$env:APPDATA/mpv"
Write-Output "AppData mode: $DataDir"
if (!(Test-Path $DataDir)) {
Write-Output "Creating folder: $DataDir"
New-Item -ItemType Directory -Force -Path $DataDir > $null
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
}
}
$ZipFile = "$DataDir/uosc_tmp.zip"
Write-Output "$ConfigDir"
Function Cleanup() {
try {
if (Test-Path $ZipFile) {
Write-Output "Deleting: $ZipFile"
Remove-Item -LiteralPath $ZipFile -Force
}
$BackupDir = "$ConfigDir/.uosc-backup"
$ZipFile = "$ConfigDir/uosc_tmp.zip"
function DeleteIfExists($Path) {
if (Test-Path $Path) {
Remove-Item -LiteralPath $Path -Force -Recurse > $null
}
catch {}
}
Function Abort($Message) {
Cleanup
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
}
# Remove old or deprecated folders & files
try {
$UoscDir = "$DataDir/scripts/uosc"
$UoscDeprecatedDir = "$DataDir/scripts/uosc_shared"
$UoscDeprecatedFile = "$DataDir/scripts/uosc.lua"
if (Test-Path $UoscDir) {
Write-Output "Deleting old: $UoscDir"
Remove-Item -LiteralPath $UoscDir -Force -Recurse
# Ensure install directory exists
if (!(Test-Path -Path $ConfigDir -PathType Container)) {
if (Test-Path -Path $ConfigDir -PathType Leaf) {
Abort("Config directory is a file.")
}
if (Test-Path $UoscDeprecatedDir) {
Write-Output "Deleting deprecated: $UoscDeprecatedDir"
Remove-Item -LiteralPath $UoscDeprecatedDir -Force -Recurse
try {
New-Item -ItemType Directory -Force -Path $ConfigDir > $null
}
if (Test-Path $UoscDeprecatedFile) {
Write-Output "Deleting deprecated: $UoscDeprecatedFile"
Remove-Item -LiteralPath $UoscDeprecatedFile -Force
catch {
Abort("Couldn't create config directory.")
}
}
catch {
Abort("Couldn't cleanup old files.")
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 {
Write-Output "Downloading: $ZipURL"
Invoke-WebRequest -OutFile $ZipFile -Uri $ZipURL > $null
}
catch {
Abort("Couldn't download the archive.")
Abort("Couldn't download: $ZipURL")
}
Write-Output "Extracting archive..."
try {
Write-Output "Extracting: $ZipFile"
Expand-Archive $ZipFile -DestinationPath $DataDir -Force > $null
Expand-Archive $ZipFile -DestinationPath $ConfigDir -Force > $null
}
catch {
Abort("Couldn't extract the archive.")
Abort("Couldn't extract: $ZipFile")
}
Cleanup
Write-Output "Deleting archive..."
DeleteIfExists($ZipFile)
Write-Output "Deleting backup..."
DeleteIfExists($BackupDir)
# Download default config if one doesn't exist yet
try {
$ScriptOptsDir = "$DataDir/script-opts"
$ScriptOptsDir = "$ConfigDir/script-opts"
$ConfFile = "$ScriptOptsDir/uosc.conf"
if (!(Test-Path $ConfFile)) {
Write-Output "Config not found, downloading default one..."
Write-Output "Config not found, downloading default uosc.conf..."
New-Item -ItemType Directory -Force -Path $ScriptOptsDir > $null
Write-Output "Downloading: $ConfURL"
Invoke-WebRequest -OutFile $ConfFile -Uri $ConfURL > $null
}
}