Tunal/scripts/install.sh

112 lines
3.6 KiB
Shell

#!/usr/bin/env bash
# Builds Tunal in release mode and installs it.
# bash scripts/install.sh for the current user, under ~/.local
# bash scripts/install.sh --system under /usr with the polkit policy (asks sudo once)
# bash scripts/install.sh [--system] --uninstall removes what that install put there
# PREFIX=/some/dir bash scripts/install.sh another place for the user install
# --no-build installs the release binary that is already built; DESTDIR stages the files for
# packagers (packaging/arch/PKGBUILD uses both).
set -euo pipefail
usage() {
sed -n '2,8s/^# \?//p' "${BASH_SOURCE[0]}"
}
system=false
uninstall=false
build=true
for arg in "$@"; do
case "$arg" in
--system) system=true ;;
--uninstall) uninstall=true ;;
--no-build) build=false ;;
-h | --help) usage; exit 0 ;;
*) echo "Unknown option: $arg" >&2; usage >&2; exit 2 ;;
esac
done
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$root"
dest="${DESTDIR:-}"
target="${CARGO_TARGET_DIR:-target}"
if $system; then
# The policy names /usr/bin/tunal, so the system install has one place.
prefix=/usr
else
prefix="${PREFIX:-$HOME/.local}"
fi
policy=org.tunal.Tunal.elevated-helper.policy
# mode, file in the repository (or the build), place it goes
files=(
755 "$target/release/tunal" "$prefix/bin/tunal"
644 assets/tunal.desktop "$prefix/share/applications/tunal.desktop"
644 assets/tunal.svg "$prefix/share/icons/hicolor/scalable/apps/tunal.svg"
644 assets/tunal.png "$prefix/share/icons/hicolor/256x256/apps/tunal.png"
)
if $system; then
files+=(644 "assets/polkit/$policy" "/usr/share/polkit-1/actions/$policy")
fi
# Only a real system install needs root; a staged one belongs to whoever stages it.
as_root=()
if $system && [[ -z "$dest" ]]; then
if command -v pacman >/dev/null && owner="$(pacman -Qoq /usr/bin/tunal 2>/dev/null)"; then
echo "/usr/bin/tunal belongs to the pacman package '$owner'; use pacman for it." >&2
exit 1
fi
if [[ $EUID -ne 0 ]]; then
command -v sudo >/dev/null || { echo "The system install needs sudo (or run it as root)." >&2; exit 1; }
as_root=(sudo)
fi
fi
if ! $uninstall; then
if $build; then
cargo build --release --locked -p tunal
fi
[[ -x "$target/release/tunal" ]] || { echo "No release binary; run without --no-build." >&2; exit 1; }
fi
if [[ ${#as_root[@]} -gt 0 ]]; then
echo "Root rights are needed for $prefix:"
sudo -v
fi
for ((i = 0; i < ${#files[@]}; i += 3)); do
to="$dest${files[i + 2]}"
if $uninstall; then
"${as_root[@]}" rm -f -- "$to"
else
"${as_root[@]}" install -Dm"${files[i]}" -- "${files[i + 1]}" "$to"
fi
done
if [[ -z "$dest" ]]; then
if command -v update-desktop-database >/dev/null; then
"${as_root[@]}" update-desktop-database "$prefix/share/applications" || true
fi
if command -v gtk-update-icon-cache >/dev/null; then
"${as_root[@]}" gtk-update-icon-cache -qtf "$prefix/share/icons/hicolor" || true
fi
fi
if $uninstall; then
echo "Removed Tunal from $prefix"
exit 0
fi
[[ -z "$dest" ]] || exit 0
echo "Installed Tunal under $prefix"
if $system; then
if [[ -e "$HOME/.local/bin/tunal" ]]; then
echo "An older copy in ~/.local may start instead. To remove it:"
echo " bash scripts/install.sh --uninstall"
fi
else
case ":$PATH:" in
*":$prefix/bin:"*) ;;
*) echo "Note: $prefix/bin is not in PATH." ;;
esac
echo "For the full administrator password prompt, install with --system instead."
fi