#!/usr/bin/env bash

THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename "$THIS_FILE")

usage() {
  cat <<EOF

Uninstalls '.desktop' files, including their icons. Most commonly
used for web applications, if an application was installed by a
package manager, then it should be used to uninstall the application.

Usage:

  $ $THIS [OPTIONS] [FILE...]

OPTIONS:

  --dry-run:    Perform but don't actually remove anything.
  -h | --help:  Show the help page.

If no files are supplied, then an interactive session will be
started that allows you to choose the applications to remove.

EOF
}

declare -a files
interactive_mode="0"
dry_run="0"
help_flag="0"
XDG_DATA_HOME=${XDG_DATA_HOME}
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}

while [[ $# -gt 0 ]]; do
  if [[ $1 =~ ^--dry ]]; then
    dry_run="1"
  elif [[ $1 =~ ^-h ]] || [[ $1 =~ ^--h ]]; then
    usage && exit 0
  else
    files+=("$1")
  fi
  shift
done

log() {
  logging log --source "$THIS_FILE" "$@"
}

############################## MAIN ##############################

source "$SCRIPTS/hypr/logging"
setup-logging "$THIS.log" "$THIS"

if [[ -z $XDG_DATA_HOME ]]; then
  log "xdg data home is not set"
  log "using: ~/.local/share"
  XDG_DATA_HOME=$HOME/.local/share
fi

if [[ ${#files} == 0 ]]; then
  interactive_mode="1"

  files+=(
    $(find $XDG_DATA_HOME/applications -mindepth 1 -maxdepth 1 -type f -name "*.desktop" -printf "%f\n" |
      gum choose --no-limit --padding "2 4" --header "Choose desktop apps to remove:" --selected-prefix="✗ ")
  )
fi

for f in ${files[@]}; do

  icon=""
  log "ARG: $f"

  # Handle a passed in webapp spec file.
  if [[ $f =~ \.json$ ]]; then
    name=$(jq -r ".name" $f)
    f="$name.desktop"
  fi

  fname=${f##*/}
  log "Uninstalling Desktop: $fname"
  file="$XDG_DATA_HOME/applications/$fname"

  if [[ ! -f $file ]]; then
    log "[WARNING]: File didn't exist, skipping!"
  else
    # get the line in file that has the icon path.
    icon_line=$(cat $file | grep "Icon")
    # get just the file path.
    icon=${icon_line/#Icon=/}

    log "   removing icon: rm -rf $icon"
    log "   removing desktop: rm -rf $file"

    if [[ $dry_run == "0" ]]; then
      rm -rf $icon
      rm -rf $file
    fi
  fi
done

if [[ ${#files} -gt 0 ]]; then
  # Refresh the database so that applcation luanchers, etc
  # don't show the app still exists.
  update-desktop-database $XDG_DATA_HOME/applications

  if [[ $interactive_mode == "1" ]]; then
    log "Done!"
  fi
fi
