#!/usr/bin/env sh set -eu usage() { cat <&2; exit 2; } prefix=$1 ;; --system) prefix=/usr/local ;; --debug) build_type=Debug ;; --build-dir) shift [ "$#" -gt 0 ] || { echo "install.sh: missing value for --build-dir" >&2; exit 2; } build_dir=$1 ;; --destdir) shift [ "$#" -gt 0 ] || { echo "install.sh: missing value for --destdir" >&2; exit 2; } destdir=$1 ;; --help|-h) usage exit 0 ;; *) echo "install.sh: unknown option: $1" >&2 usage >&2 exit 2 ;; esac shift done if [ -z "$build_dir" ]; then if [ "$build_type" = Debug ]; then build_dir=$script_dir/build-debug else build_dir=$script_dir/build fi fi run_install() { command -v cmake >/dev/null 2>&1 || { echo "install.sh: cmake is required" >&2; exit 1; } command -v pkg-config >/dev/null 2>&1 || { echo "install.sh: pkg-config is required" >&2; exit 1; } cmake -S "$script_dir" -B "$build_dir" \ -DCMAKE_BUILD_TYPE="$build_type" \ -DCMAKE_INSTALL_PREFIX="$prefix" \ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON cmake --build "$build_dir" if [ -n "$destdir" ]; then DESTDIR=$destdir cmake --install "$build_dir" elif mkdir -p "$prefix" 2>/dev/null && [ -w "$prefix" ]; then cmake --install "$build_dir" elif command -v sudo >/dev/null 2>&1; then sudo cmake --install "$build_dir" else echo "install.sh: $prefix is not writable and sudo was not found" >&2 exit 1 fi if command -v update-desktop-database >/dev/null 2>&1; then applications_dir=${destdir}${prefix}/share/applications if [ -d "$applications_dir" ]; then update-desktop-database "$applications_dir" >/dev/null 2>&1 || true fi fi } run_uninstall() { manifest=$build_dir/install_manifest.txt if [ ! -f "$manifest" ]; then echo "install.sh: no install manifest found at $manifest" >&2 echo "install.sh: run install first or pass the matching --build-dir" >&2 exit 1 fi while IFS= read -r path || [ -n "$path" ]; do target=${destdir}${path} if [ -e "$target" ] || [ -L "$target" ]; then if [ -w "$(dirname "$target")" ]; then rm -f "$target" elif command -v sudo >/dev/null 2>&1; then sudo rm -f "$target" else echo "install.sh: cannot remove $target and sudo was not found" >&2 exit 1 fi echo "Removed $target" fi done < "$manifest" if command -v update-desktop-database >/dev/null 2>&1; then applications_dir=${destdir}${prefix}/share/applications if [ -d "$applications_dir" ]; then update-desktop-database "$applications_dir" >/dev/null 2>&1 || true fi fi } case "$action" in install) run_install ;; uninstall) run_uninstall ;; esac