#!/usr/bin/env bash
# Installs Mel for the current user: binary on PATH, desktop entry, and the
# hicolor icons — so the app menu, dock, and taskbar all show the Mel mark.
# Run from the unpacked download directory:  ./install.sh
set -euo pipefail

HERE="$(cd "$(dirname "$0")" && pwd)"
BIN_DIR="${HOME}/.local/bin"
APPS_DIR="${HOME}/.local/share/applications"
ICONS_DIR="${HOME}/.local/share/icons/hicolor"

mkdir -p "$BIN_DIR" "$APPS_DIR"
install -m 755 "$HERE/mel" "$BIN_DIR/mel"

for s in 16 32 48 64 128 256 512; do
  mkdir -p "$ICONS_DIR/${s}x${s}/apps"
  install -m 644 "$HERE/icons/hicolor/${s}x${s}/apps/mel.png" \
    "$ICONS_DIR/${s}x${s}/apps/mel.png"
done

# Point the desktop entry at the installed binary (absolute path so it works
# even when ~/.local/bin isn't on the session PATH), and give the launcher a
# home working dir (freedesktop Path= must be absolute, so it's injected at
# install time; the in-app startup normalization covers every other context).
sed -e "s|^Exec=.*|Exec=${BIN_DIR}/mel|" -e "/^Path=/d" "$HERE/mel.desktop" \
    | sed "/^Exec=/a Path=${HOME}" > "$APPS_DIR/mel.desktop"
chmod 644 "$APPS_DIR/mel.desktop"

update-desktop-database "$APPS_DIR" 2>/dev/null || true
gtk-update-icon-cache "$ICONS_DIR" 2>/dev/null || true

echo "Mel installed — find it in your app menu (re-log if it doesn't appear yet)."
