#!/bin/sh
#
# Install Ensemble from the tarball, into your own home.
#
# No root: the app runs as an ordinary user and asks the four daemons to do
# the privileged work. So this writes nothing outside ~/.local, and
# `./install.sh --uninstall` takes exactly those files back out again.

set -eu

here="$(dirname "$(readlink -f "$0")")"
prefix="${PREFIX:-$HOME/.local}"

bin="$prefix/bin/ensemble"
desktop="$prefix/share/applications/dev.wyns.ensemble.desktop"
icon="$prefix/share/icons/hicolor/scalable/apps/dev.wyns.ensemble.svg"

refresh() {
    # Without these the desktop keeps serving what it read before, and the
    # taskbar stays empty until the next login.
    if command -v update-desktop-database >/dev/null 2>&1; then
        update-desktop-database "$prefix/share/applications" 2>/dev/null || true
    fi
    touch "$prefix/share/icons/hicolor" 2>/dev/null || true
}

if [ "${1:-}" = "--uninstall" ]; then
    rm -f "$bin" "$desktop" "$icon"
    refresh
    echo "Removed Ensemble from $prefix."
    exit 0
fi

install -Dm755 "$here/ensemble" "$bin"
install -Dm644 "$here/dev.wyns.ensemble.desktop" "$desktop"
install -Dm644 "$here/dev.wyns.ensemble.svg" "$icon"
refresh

echo "Ensemble is in $bin"

case ":$PATH:" in
*":$prefix/bin:"*) ;;
*) echo "Note: $prefix/bin is not on your PATH; the menu entry works either way." ;;
esac
