#!/bin/bash

# Get a list of all brew-installed executables in PATH
BREW_BIN_PATH="$(brew --prefix)/bin"

APP=$(zenity --entry \
  --title="Homebrew Launcher Creator" \
  --text="Enter the name of the Homebrew GUI app (binary):")

if [ -z "$APP" ]; then
  zenity --error --text="No app name entered. Exiting."
  exit 1
fi

if [ ! -x "$BREW_BIN_PATH/$APP" ]; then
  zenity --error --text="Executable not found at $BREW_BIN_PATH/$APP"
  exit 1
fi

ICON_PATH=$(zenity --file-selection \
  --title="Choose an icon for $APP" \
  --file-filter="*.png *.svg" \
  --filename="$HOME/")

DESKTOP_FILE="$HOME/.local/share/applications/${APP}-homebrew.desktop"

cat > "$DESKTOP_FILE" <<EOF
[Desktop Entry]
Name=${APP^} (Homebrew)
Exec=$BREW_BIN_PATH/$APP
Icon=${ICON_PATH:-utilities-terminal}
Type=Application
Categories=Utility;
Terminal=false
EOF

chmod +x "$DESKTOP_FILE"

zenity --info --title="🎉 Launcher Created" \
  --text="Desktop launcher for '$APP' created!\nIt should now appear in your app menu."
