#!/bin/sh # Damson 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. # Without FILE it uses the unpacked release folder it sits in, or Damson-*-Linux-x86_64.tar.gz or # Damson-*-x86_64.AppImage next to it. SHA256SUMS- beside the file is checked when present. # --purge also deletes settings and data (~/.config/damson: settings and recovery copies). DESTDIR=dir stages under dir (no sudo). # Needs glibc 2.35 or newer. set -eu app=damson main=damson need=2.35 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,9p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; -*) echo "Unknown option: $1 (see --help)." >&2; exit 2 ;; *) file=$1 ;; esac shift done [ "$purge" = 0 ] || [ "$mode" = uninstall ] || { echo "--purge only goes with --uninstall." >&2; exit 2; } if [ -z "$prefix" ]; then if [ "$scope" = system ]; then prefix=/usr/local; else prefix=$HOME/.local; fi fi case $prefix in /*) ;; *) echo "--prefix must be an absolute path." >&2; exit 2 ;; esac prefix=${prefix%/} destdir=${DESTDIR:-} root=$destdir$prefix list=$root/lib/$app/files sudo= if [ -z "$destdir" ] && [ "$(id -u)" != 0 ]; then d=$root; while [ ! -e "$d" ]; do d=$(dirname "$d"); done [ -w "$d" ] || sudo=sudo fi refresh() { [ -z "$destdir" ] || return 0 command -v update-desktop-database >/dev/null 2>&1 && $sudo update-desktop-database -q "$root/share/applications" 2>/dev/null || true [ -f "$root/share/icons/hicolor/index.theme" ] && command -v gtk-update-icon-cache >/dev/null 2>&1 \ && $sudo gtk-update-icon-cache -q -t -f "$root/share/icons/hicolor" 2>/dev/null || true } # Removes the files the last install listed, then the folders that became empty. remove_listed() { [ -f "$list" ] || return 0 while IFS= read -r f; do [ -n "$f" ] || continue $sudo rm -f "$root/$f" d=$(dirname "$f") while [ "$d" != . ] && $sudo rmdir "$root/$d" 2>/dev/null; do d=$(dirname "$d"); done done < "$list" $sudo rm -f "$list" $sudo rmdir "$root/lib/$app" "$root/lib" 2>/dev/null || true } if [ "$mode" = uninstall ]; then if [ ! -f "$list" ]; then echo "Damson is not installed in $prefix (no $list)." >&2 [ "$purge" = 1 ] || exit 1 else remove_listed refresh echo "Damson removed from $prefix." fi if [ "$purge" = 1 ]; then rm -rf "${XDG_CONFIG_HOME:-$HOME/.config}/damson" echo "Settings and data removed (~/.config/damson: settings and recovery copies)." else echo "Settings and data are kept (--purge deletes them)." fi exit 0 fi # glibc check (DAMSON_GLIBC overrides the detected version, for tests) have=${DAMSON_GLIBC:-$(getconf GNU_LIBC_VERSION 2>/dev/null | sed 's/^glibc //')} if [ -z "$have" ]; then echo "Damson needs glibc $need or newer, and this system does not report a glibc (musl?). Nothing was installed." >&2; exit 1 fi if [ "$(printf '%s\n%s\n' "$need" "$have" | sort -V | head -n 1)" != "$need" ]; then echo "Damson 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 installed, so $url cannot be fetched." >&2; exit 1; fi fi src= if [ -z "$file" ]; then if [ -f "$here/bin/$main" ]; then src=$here else for f in "$here"/Damson-*-Linux-x86_64.tar.gz "$here"/Damson-*-x86_64.AppImage; do [ -f "$f" ] && { file=$f; break; } done fi fi if [ -z "$src" ]; then [ -n "$file" ] && [ -f "$file" ] || { echo "No Damson tar.gz or AppImage was given or found next to the installer." >&2; exit 1; } 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 [ "${line%% *}" = "$(sha256sum "$file" | cut -d' ' -f1)" ] \ || { echo "The checksum of $name does not match $(basename "$s"). Nothing was installed." >&2; exit 1; } echo "Checksum ok." done mkdir "$tmp/x" case $name in *.tar.gz) tar -xzf "$file" -C "$tmp/x" src=$(find "$tmp/x" -maxdepth 1 -mindepth 1 -type d | head -n 1) [ -n "$src" ] && [ -f "$src/bin/$main" ] || { echo "$name has no bin/$main, so it is not a Damson release." >&2; exit 1; } ;; *.AppImage) cp "$file" "$tmp/app"; chmod 755 "$tmp/app" (cd "$tmp/x" && "$tmp/app" --appimage-extract >/dev/null 2>&1) || true [ -d "$tmp/x/squashfs-root/usr/share" ] || { echo "Could not read the contents of $name. Nothing was installed." >&2; exit 1; } src=$tmp/src; mkdir -p "$src/bin" mv "$tmp/app" "$src/bin/$main" mv "$tmp/x/squashfs-root/usr/share" "$src/share" [ ! -d "$tmp/x/squashfs-root/usr/lib" ] || mv "$tmp/x/squashfs-root/usr/lib" "$src/lib" ;; *) echo "Unknown file type: $name (need .tar.gz or .AppImage)." >&2; exit 1 ;; esac fi # Stage with launchers pointing at this prefix, then copy and list every file for --uninstall. st=$tmp/stage mkdir -p "$st" for d in bin lib share; do [ ! -d "$src/$d" ] || cp -R "$src/$d" "$st/$d"; done for d in "$st"/share/applications/*.desktop; do [ -f "$d" ] || continue for b in "$st"/bin/*; do b=$(basename "$b") sed -i -e "s#^Exec=$b\( \|\$\)#Exec=$prefix/bin/$b\1#" -e "s#^TryExec=$b\$#TryExec=$prefix/bin/$b#" "$d" done done chmod 755 "$st"/bin/* (cd "$st" && find . \( -type f -o -type l \) | sed 's|^\./||' | sort) > "$tmp/files" mkdir -p "$st/lib/$app"; echo "lib/$app/files" >> "$tmp/files"; cp "$tmp/files" "$st/lib/$app/files" remove_listed $sudo mkdir -p "$root" $sudo cp -R "$st/." "$root/" refresh echo "Damson installed in $prefix/bin/$main" case ":$PATH:" in *":$prefix/bin:"*) ;; *) echo "Note: $prefix/bin is not on your PATH." ;; esac