#!/bin/bash

# omarchy:summary=Create a desktop launcher for a terminal UI app
# omarchy:args=[name command window-style icon-url-or-name]

set -e

ICON_DIR="$HOME/.local/share/icons/hicolor/256x256/apps"

safe_icon_name() {
  local name
  name=$(printf '%s\n' "$1" \
    | tr '[:upper:]' '[:lower:]' \
    | sed 's/[^[:alnum:]]\+/-/g; s/^-//; s/-$//')
  if [[ -n $name ]]; then
    printf '%s\n' "$name"
  else
    printf 'tui-%s\n' "$(printf '%s' "$1" | sha256sum | cut -c1-12)"
  fi
}

require_plain_name() {
  if [[ $1 == */* || $1 == "." || $1 == ".." ]]; then
    echo "App name cannot contain '/' or be '.'/'..': $1" >&2
    exit 1
  fi
}

desktop_string_escape() {
  local value="$1"

  value=${value//\\/\\\\}
  value=${value//$'\t'/\\t}
  value=${value//$'\r'/\\r}
  value=${value//$'\n'/\\n}
  [[ $value == " "* ]] && value="\\s${value# }"

  printf '%s' "$value"
}

icon_name_from_ref() {
  local ref="$1"
  local name
  name=$(basename -- "$ref")

  if [[ $name == *.* ]]; then
    safe_icon_name "${name%.*}"
  else
    printf '%s\n' "$name"
  fi
}

install_user_icon() {
  local source="$1"
  local name="$2"
  local ext="${source##*.}"

  [[ $ext == "$source" ]] && ext="png"
  mkdir -p "$ICON_DIR"
  cp -- "$source" "$ICON_DIR/$name.$ext"
  gtk-update-icon-cache "$HOME/.local/share/icons/hicolor" &>/dev/null || true
  printf '%s\n' "$name"
}

download_icon() {
  local tmp
  tmp=$(mktemp "$(dirname "$2")/.icon.XXXXXX") || return 1
  if curl -fsSL --max-time 10 -o "$tmp" -- "$1" 2>/dev/null &&
    [[ -s $tmp && $(file -b --mime-type "$tmp") == image/* ]]; then
    chmod 644 "$tmp"
    mv -f "$tmp" "$2"
    return 0
  else
    rm -f "$tmp"
    return 1
  fi
}

if (( $# != 4 )); then
  echo -e "\e[32m$(omarchy-i18n tui.intro)\n\e[0m"
  APP_NAME=$(gum input --prompt "$(omarchy-i18n tui.namePrompt)" --placeholder "$(omarchy-i18n tui.namePlaceholder)")
  require_plain_name "$APP_NAME"
  APP_EXEC=$(gum input --prompt "$(omarchy-i18n tui.commandPrompt)" --placeholder "$(omarchy-i18n tui.commandPlaceholder "lazydocker" "bash -c 'dust; read -n 1 -s'")")
  WINDOW_STYLE=$(gum choose --header "$(omarchy-i18n tui.windowStyle)" float tile)
  ICON_REF=$(gum input --prompt "$(omarchy-i18n tui.iconPrompt)" --placeholder "$(omarchy-i18n tui.iconPlaceholder "https://dashboardicons.com")")
else
  APP_NAME="$1"
  APP_EXEC="$2"
  WINDOW_STYLE="$3"
  ICON_REF="$4"
fi

if [[ -z $APP_NAME || -z $APP_EXEC || -z $ICON_REF ]]; then
  echo "You must set app name, app command, and icon URL/name!"
  exit 1
fi

require_plain_name "$APP_NAME"

DESKTOP_FILE="$HOME/.local/share/applications/$APP_NAME.desktop"
mkdir -p "$(dirname "$DESKTOP_FILE")"

if [[ $ICON_REF =~ ^https?:// ]]; then
  ICON_VALUE=$(safe_icon_name "$APP_NAME")
  mkdir -p "$ICON_DIR"
  if ! download_icon "$ICON_REF" "$ICON_DIR/$ICON_VALUE.png"; then
    echo "Error: Failed to download icon."
    exit 1
  fi
  gtk-update-icon-cache "$HOME/.local/share/icons/hicolor" &>/dev/null || true
elif [[ -f $ICON_REF ]]; then
  ICON_VALUE=$(install_user_icon "$ICON_REF" "$(safe_icon_name "$APP_NAME")")
else
  # Bundled Omarchy icons are package-owned under /usr/share/icons/hicolor.
  ICON_VALUE=$(icon_name_from_ref "$ICON_REF")
fi

if [[ $WINDOW_STYLE == "float" ]]; then
  APP_CLASS="TUI.float"
else
  APP_CLASS="TUI.tile"
fi

name_field=$(desktop_string_escape "$APP_NAME")
exec_field=$(desktop_string_escape "xdg-terminal-exec --app-id=$APP_CLASS -e $APP_EXEC")
icon_field=$(desktop_string_escape "$ICON_VALUE")

cat >"$DESKTOP_FILE" <<EOF
[Desktop Entry]
Version=1.0
Name=$name_field
Comment=$name_field
Exec=$exec_field
Terminal=false
Type=Application
Icon=$icon_field
StartupNotify=true
EOF

chmod +x "$DESKTOP_FILE"

if (( $# != 4 )); then
  echo -e "You can now find $APP_NAME using the app launcher (SUPER + SPACE)\n"
fi
