Medlar/installer.sh
2026-11-26 12:24:36 -06:00

142 lines
6.7 KiB
Shell
Executable file

#!/bin/sh
# Medlar installer. Installs from a release tar.gz or AppImage, or removes an install.
# installer.sh [--user|--system] [--prefix DIR] [--url URL] [FILE]
# installer.sh --uninstall [--purge] [--user|--system] [--prefix DIR]
# --user (default): ~/.local, no root. --system: /usr/local, sudo only for the copy.
# DESTDIR=dir puts everything under dir (for packaging and tests; sudo is not used then).
# Without FILE it takes Medlar-*-Linux-x86_64.tar.gz or Medlar-*-x86_64.AppImage found next to this script.
# SHA256SUMS-<version> beside the file is checked when present. Needs glibc 2.44 or newer.
set -eu
mode=install scope=user purge=0 prefix= url= file=
while [ $# -gt 0 ]; do
case $1 in
--user) scope=user ;;
--system) scope=system ;;
--prefix) [ $# -ge 2 ] || { echo "--prefix needs a folder." >&2; exit 2; }; prefix=$2; shift ;;
--url) [ $# -ge 2 ] || { echo "--url needs an address." >&2; exit 2; }; url=$2; shift ;;
--uninstall) mode=uninstall ;;
--purge) purge=1 ;;
-h|--help) sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
-*) echo "Unknown option: $1" >&2; exit 2 ;;
*) file=$1 ;;
esac
shift
done
if [ -z "$prefix" ]; then
if [ "$scope" = system ]; then prefix=/usr/local; else prefix=$HOME/.local; fi
fi
destdir=${DESTDIR:-}
root=$destdir$prefix
sudo=
if [ "$scope" = system ] && [ -z "$destdir" ] && [ "$(id -u)" != 0 ]; then sudo=sudo; fi
bin=$root/bin/medlar
desktop=$root/share/applications/medlar.desktop
icon=$root/share/icons/hicolor/scalable/apps/medlar.svg
meta=$root/share/metainfo/medlar.metainfo.xml
lic=$root/share/licenses/medlar/Rubik-OFL.txt
refresh() {
[ -z "$destdir" ] || return 0
command -v update-desktop-database >/dev/null 2>&1 && $sudo update-desktop-database "$root/share/applications" 2>/dev/null || true
command -v gtk-update-icon-cache >/dev/null 2>&1 && $sudo gtk-update-icon-cache -q -t "$root/share/icons/hicolor" 2>/dev/null || true
}
if [ "$mode" = uninstall ]; then
$sudo rm -f "$bin" "$desktop" "$icon" "$meta" "$lic"
for d in "$root/share/licenses/medlar" "$root/share/licenses" "$root/share/icons/hicolor/scalable/apps" \
"$root/share/icons/hicolor/scalable" "$root/share/icons/hicolor" "$root/share/icons" "$root/share/metainfo" \
"$root/share/applications" "$root/share" "$root/bin"; do
$sudo rmdir "$d" 2>/dev/null || true
done
mp=${XDG_CONFIG_HOME:-$HOME/.config}/mimeapps.list
if [ "$scope" = user ] && [ -z "$destdir" ] && [ -f "$mp" ]; then
sed -i '/^x-scheme-handler\/medlar=medlar\.desktop;\{0,1\}$/d' "$mp"
fi
refresh
if [ "$purge" = 1 ]; then
rm -rf "${XDG_CONFIG_HOME:-$HOME/.config}/medlar" "${XDG_DATA_HOME:-$HOME/.local/share}/medlar" \
"${XDG_STATE_HOME:-$HOME/.local/state}/medlar" "${XDG_CACHE_HOME:-$HOME/.cache}/medlar"
echo "Settings and data removed. Downloaded files were not touched."
fi
echo "Medlar removed from $root."
exit 0
fi
# glibc check (MEDLAR_GLIBC overrides the detected version, for tests)
need=2.44
have=${MEDLAR_GLIBC:-$(getconf GNU_LIBC_VERSION 2>/dev/null | sed 's/^glibc //')}
if [ -z "$have" ]; then
echo "Medlar needs glibc $need or newer, and this system does not report glibc. Nothing was installed." >&2; exit 1
fi
newer=$(printf '%s\n%s\n' "$need" "$have" | sort -V | head -n 1)
if [ "$newer" != "$need" ]; then
echo "Medlar needs glibc $need or newer, and this system has glibc $have. Nothing was installed." >&2; exit 1
fi
here=$(cd "$(dirname "$0")" && pwd)
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT INT TERM
if [ -n "$url" ]; then
file=$tmp/$(basename "${url%%\?*}")
if command -v curl >/dev/null 2>&1; then curl -fL -o "$file" -- "$url"
elif command -v wget >/dev/null 2>&1; then wget -O "$file" -- "$url"
else echo "Neither curl nor wget is here, so $url cannot be fetched." >&2; exit 1; fi
fi
if [ -z "$file" ]; then
for f in "$here"/Medlar-*-Linux-x86_64.tar.gz "$here"/Medlar-*-x86_64.AppImage; do
[ -f "$f" ] && { file=$f; break; }
done
fi
[ -n "$file" ] && [ -f "$file" ] || { echo "No Medlar tar.gz or AppImage given or found next to the installer." >&2; exit 1; }
# checksum
dir=$(dirname "$file"); name=$(basename "$file")
for s in "$dir"/SHA256SUMS-*; do
[ -f "$s" ] || continue
line=$(grep -F " $name" "$s" | head -n 1 || true)
[ -n "$line" ] || continue
want=${line%% *}; got=$(sha256sum "$file" | cut -d' ' -f1)
[ "$want" = "$got" ] || { echo "Checksum of $name does not match $(basename "$s"). Nothing was installed." >&2; exit 1; }
echo "Checksum ok."
done
st=$tmp/stage
mkdir -p "$st/bin" "$st/share/applications" "$st/share/icons/hicolor/scalable/apps" "$st/share/metainfo" "$st/share/licenses/medlar"
case $name in
*.tar.gz)
mkdir "$tmp/x"; tar -xzf "$file" -C "$tmp/x"
src=$(find "$tmp/x" -maxdepth 1 -mindepth 1 -type d | head -n 1)
[ -f "$src/bin/medlar" ] || { echo "$name has no bin/medlar; it is not a Medlar release." >&2; exit 1; }
cp "$src/bin/medlar" "$st/bin/medlar"
cp "$src/share/medlar.svg" "$st/share/icons/hicolor/scalable/apps/medlar.svg"
cp "$src/share/medlar.metainfo.xml" "$st/share/metainfo/medlar.metainfo.xml"
cp "$src/share/medlar.desktop" "$st/share/applications/medlar.desktop"
cp "$src/share/Rubik-OFL.txt" "$st/share/licenses/medlar/Rubik-OFL.txt" ;;
*.AppImage)
cp "$file" "$st/bin/medlar"; chmod 755 "$st/bin/medlar"
mkdir "$tmp/x"
(cd "$tmp/x" && "$st/bin/medlar" --appimage-extract >/dev/null 2>&1) || true
ex=$tmp/x/squashfs-root
[ -f "$ex/medlar.desktop" ] || { echo "Could not read the AppImage contents. Nothing was installed." >&2; exit 1; }
cp "$ex/medlar.svg" "$st/share/icons/hicolor/scalable/apps/medlar.svg"
cp "$ex/usr/share/metainfo/medlar.metainfo.xml" "$st/share/metainfo/medlar.metainfo.xml"
cp "$ex/medlar.desktop" "$st/share/applications/medlar.desktop"
rmdir "$st/share/licenses/medlar" "$st/share/licenses" ;;
*) echo "Unknown file type: $name (need .tar.gz or .AppImage)." >&2; exit 1 ;;
esac
chmod 755 "$st/bin/medlar"
sed "s|^Exec=medlar|Exec=$prefix/bin/medlar|" "$st/share/applications/medlar.desktop" > "$st/d" && mv "$st/d" "$st/share/applications/medlar.desktop"
$sudo mkdir -p "$root"
$sudo cp -R "$st/." "$root/"
$sudo chmod 755 "$bin"
refresh
# medlar: scheme handler: the desktop file lists it; a user install also sets it as the default.
if [ "$scope" = user ] && [ -z "$destdir" ] && command -v xdg-mime >/dev/null 2>&1; then
xdg-mime default medlar.desktop x-scheme-handler/medlar 2>/dev/null || true
fi
echo "Medlar installed in $bin"
case ":$PATH:" in *":$prefix/bin:"*) ;; *) echo "Note: $prefix/bin is not on your PATH." ;; esac