#!/usr/bin/env bash
# Create menu + Desktop shortcuts for Siraya Printer desktop manager.
set -euo pipefail

TARGET="${1:-}"
if [[ -z "$TARGET" || ! -e "$TARGET" ]]; then
  echo "Usage: $0 /path/to/SirayaPrinter.AppImage|siraya-printer-desktop"
  exit 1
fi

TARGET="$(readlink -f "$TARGET")"
NAME="Siraya Printer"
APPS_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/applications"

if command -v xdg-user-dir >/dev/null 2>&1; then
  USER_DESKTOP="$(xdg-user-dir DESKTOP 2>/dev/null || true)"
fi
USER_DESKTOP="${USER_DESKTOP:-$HOME/Desktop}"

write_entry() {
  local dest="$1"
  cat >"$dest" <<EOF
[Desktop Entry]
Type=Application
Name=${NAME}
Comment=Siraya thermal printer manager
Exec="${TARGET}" --no-sandbox
Icon=printer
Terminal=false
Categories=Office;
StartupWMClass=SirayaPrinter
EOF
  chmod +x "$dest"
}

mkdir -p "$APPS_DIR" "$USER_DESKTOP"
write_entry "$APPS_DIR/siraya-printer.desktop"
write_entry "$USER_DESKTOP/Siraya Printer.desktop"

if command -v update-desktop-database >/dev/null 2>&1; then
  update-desktop-database "$APPS_DIR" >/dev/null 2>&1 || true
fi

echo "Desktop shortcuts created:"
echo "  $APPS_DIR/siraya-printer.desktop"
echo "  $USER_DESKTOP/Siraya Printer.desktop"
