Damson/install.sh
astar76 9cad187d56 Proper install script, and a clean Discord disconnect
install.sh now works like a usual source install: PREFIX and DESTDIR,
/usr/local by default with sudo for the copy only, --user, --uninstall
and --purge. It builds in a temporary folder that is removed however the
script ends, so target/ is left alone. Installs PNG icons up to 512,
AppStream data, the licence and the README, and removes an older copy in
~/.local that would hide the new one on PATH.

Quitting or switching presence off now clears the activity and closes
the connection, with a short limit so Discord can't delay quitting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 18:50:17 -06:00

209 lines
8 KiB
Shell
Executable file

#!/usr/bin/env bash
# Builds Damson from source and installs it, like any Linux app built by hand.
# The build happens in a temporary folder that is deleted afterwards. Run with --help for options.
set -euo pipefail
usage() {
cat <<'EOF'
Usage: ./install.sh [options]
(no options) Build, then install system-wide to /usr/local (asks for sudo to copy)
--user Install for this user only, to ~/.local (no sudo)
--prefix DIR Install under DIR
--keep-build Build in target/ and keep it, which is faster when rebuilding
--uninstall Remove what this script installed (combine with --user or --prefix)
--purge With --uninstall, also delete settings and recovery copies
-h, --help Show this
Packagers: DESTDIR=/path/to/pkg ./install.sh --prefix /usr
EOF
}
die() { echo "error: $*" >&2; exit 1; }
say() { printf '\033[1m==>\033[0m %s\n' "$*"; }
cd "$(dirname "$(readlink -f "$0")")"
[ -f Cargo.toml ] && [ -f packaging/damson.desktop ] || die "run this from a Damson source folder"
prefix=/usr/local
user_install=0 uninstall=0 purge=0 keep_build=0
while [ $# -gt 0 ]; do
case "$1" in
--user) user_install=1 ;;
--prefix) [ $# -ge 2 ] || die "--prefix needs a folder"; prefix=$2; shift ;;
--prefix=*) prefix=${1#--prefix=} ;;
--keep-build) keep_build=1 ;;
--uninstall) uninstall=1 ;;
--purge) purge=1 ;;
-h|--help) usage; exit 0 ;;
*) usage >&2; die "unknown option: $1" ;;
esac
shift
done
[ "$purge" = 0 ] || [ "$uninstall" = 1 ] || die "--purge only goes with --uninstall"
# Under sudo, the person is SUDO_USER: their toolchain builds it, and root only copies.
root=0 real_user=$(id -un) real_home=$HOME
if [ "$(id -u)" = 0 ]; then
root=1
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != root ]; then
real_user=$SUDO_USER
real_home=$(getent passwd "$SUDO_USER" | cut -d: -f6)
fi
fi
if [ "$user_install" = 1 ]; then
[ "$root" = 0 ] || die "--user installs for one person, so run it without sudo"
prefix=$HOME/.local
fi
case "$prefix" in /*) ;; *) die "--prefix must be an absolute path" ;; esac
prefix=${prefix%/}
destdir=${DESTDIR:-}
destdir=${destdir%/}
target=$destdir$prefix
bindir=$prefix/bin
sizes="16 24 32 48 64 128 256 512"
# Every file this script installs, relative to the prefix.
installed_files() {
echo bin/damson
echo share/applications/damson.desktop
echo share/metainfo/damson.metainfo.xml
echo share/icons/hicolor/scalable/apps/damson.svg
for s in $sizes; do echo "share/icons/hicolor/${s}x$s/apps/damson.png"; done
echo share/licenses/damson/LICENSE
echo share/doc/damson/README.md
}
# sudo is used only for the copy, and only when the destination needs it.
writable_dir() {
local d=$1
while [ ! -e "$d" ]; do d=$(dirname "$d"); done
[ -w "$d" ]
}
as_owner=()
if [ "$root" = 0 ] && ! writable_dir "$target"; then
command -v sudo >/dev/null || die "$target needs root and sudo isn't installed. Try --user."
as_owner=(sudo)
fi
refresh_caches() {
[ -z "$destdir" ] || return 0
local share=$prefix/share
command -v update-desktop-database >/dev/null && "${as_owner[@]}" update-desktop-database -q "$share/applications" || true
if command -v gtk-update-icon-cache >/dev/null && [ -f "$share/icons/hicolor/index.theme" ]; then
"${as_owner[@]}" gtk-update-icon-cache -q -t -f "$share/icons/hicolor" || true
fi
}
remove_install() { # prefix on disk, sudo-or-empty
local base=$1; shift
local f
while read -r f; do "$@" rm -f "$base/$f"; done < <(installed_files)
"$@" rmdir --ignore-fail-on-non-empty "$base/share/licenses/damson" "$base/share/doc/damson" 2>/dev/null || true
}
if [ "$uninstall" = 1 ]; then
if [ ! -e "$target/bin/damson" ]; then
[ "$prefix" = "$real_home/.local" ] || [ ! -e "$real_home/.local/bin/damson" ] || say "There's a copy in ~/.local too: remove it with --user --uninstall"
die "Damson isn't installed in $prefix"
fi
say "Removing Damson from $prefix"
remove_install "$target" "${as_owner[@]}"
refresh_caches
if [ "$purge" = 1 ]; then
say "Deleting settings and recovery copies"
rm -rf "${XDG_CONFIG_HOME:-$real_home/.config}/damson"
else
echo "Settings are kept in ${XDG_CONFIG_HOME:-$real_home/.config}/damson (--purge deletes them)."
fi
say "Uninstalled"
exit 0
fi
# ---------------------------------------------------------------- build
as_builder() {
if [ "$root" = 1 ] && [ "$real_user" != root ]; then
sudo -u "$real_user" -H env PATH="$real_home/.cargo/bin:$PATH" "$@"
else
"$@"
fi
}
as_builder sh -c 'command -v cargo' >/dev/null || die "cargo not found. Install Rust (rustup or your distro's rust package) first."
as_builder sh -c 'command -v cc' >/dev/null || die "no C compiler found. Install gcc or clang first."
version=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)
# anything left behind is removed however the script ends
work=
cleanup() { [ -z "$work" ] || rm -rf "$work"; }
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
cache=${XDG_CACHE_HOME:-$real_home/.cache}
as_builder mkdir -p "$cache"
work=$(as_builder mktemp -d "$cache/damson-build.XXXXXX")
if [ "$keep_build" = 1 ]; then
say "Building Damson $version (release) in target/"
as_builder cargo build --release --locked
binary=target/release/damson
else
say "Building Damson $version (release)"
as_builder env CARGO_TARGET_DIR="$work/target" cargo build --release --locked
binary=$work/target/release/damson
fi
[ -x "$binary" ] || die "the build didn't produce a binary"
# ---------------------------------------------------------------- stage
stage=$work/stage
as_builder mkdir -p "$stage"
for s in $sizes; do as_builder "$binary" --export-icon "$s" "$stage/$s.png"; done
if [ "$prefix" = /usr ] || [ "$prefix" = /usr/local ]; then
as_builder cp packaging/damson.desktop "$stage/damson.desktop"
else
# outside the standard folders, the launcher can't count on PATH finding it
as_builder sh -c "sed -e 's|^Exec=damson|Exec=$bindir/damson|' -e 's|^TryExec=damson|TryExec=$bindir/damson|' packaging/damson.desktop > '$stage/damson.desktop'"
fi
# ---------------------------------------------------------------- install
say "Installing to $target"
[ -z "${as_owner[*]:-}" ] || echo "Copying to $target needs root:"
put() { "${as_owner[@]}" install -Dm"$1" "$2" "$target/$3"; }
put 755 "$binary" bin/damson
put 644 "$stage/damson.desktop" share/applications/damson.desktop
put 644 packaging/damson.metainfo.xml share/metainfo/damson.metainfo.xml
put 644 packaging/damson.svg share/icons/hicolor/scalable/apps/damson.svg
for s in $sizes; do put 644 "$stage/$s.png" "share/icons/hicolor/${s}x$s/apps/damson.png"; done
put 644 LICENSE share/licenses/damson/LICENSE
put 644 README.md share/doc/damson/README.md
refresh_caches
# An older copy in ~/.local comes first on PATH and would hide this one.
old=$real_home/.local
if [ -z "$destdir" ] && [ "$prefix" != "$old" ] && [ -e "$old/bin/damson" ]; then
say "Removing the older copy in $old, which would hide this one"
remove_install "$old"
command -v update-desktop-database >/dev/null && update-desktop-database -q "$old/share/applications" || true
[ "$root" = 0 ] || chown -R "$real_user:" "$old/share/applications" 2>/dev/null || true
fi
say "Cleaning up"
cleanup
work=
# ---------------------------------------------------------------- notes
if [ -z "$destdir" ]; then
case ":$PATH:" in
*":$bindir:"*) ;;
*) [ "$root" = 1 ] || echo "Note: $bindir isn't on your PATH, so add it to run damson by name." ;;
esac
fi
if ! ls /usr/share/org.kde.syntax-highlighting/syntax*/rust.xml >/dev/null 2>&1; then
echo "Note: code colours need Kate's syntax definitions: install the syntax-highlighting package"
echo " (Arch: syntax-highlighting, Debian/Ubuntu: libkf6syntaxhighlighting6-data, Fedora: kf6-syntax-highlighting)."
fi
say "Installed Damson $version. Run: damson [file[:line]] [folder]"