#! /usr/bin/bash

# Hacky script, but gets the job done

gui_title="Installer"
gui_width=350

file_content="[Desktop Entry]
Type=Application
Terminal=false
Name=TDP Manager
Comment=Unofficial TDP manager for GPD Win 3 2
Exec=bash -c 'export PATH=\"\$PATH:${HOME}/.local/gpd-win3/scripts\";${HOME}/.local/gpd-win3/scripts/tdp gui'
"

finish_message="A .desktop file has been installed in ~/.local/share/applications/
"

if [ -z $(which zenity 2>/dev/null) ] || [ -z $(which tdp 2>/dev/null) ]; then
  echo "Missing Zenity, or script collection is not properly installed."
  echo "Make sure the scripts are in ${HOME}/.local/gpd-win3/scripts and added to your PATH"
else
  if zenity --question --title=:"$gui_title" --width=$gui_width --text="This installer will add a desktop entry for the TDP manager's GUI. Meaning you will be able to launch it from your application menu like any other software.\n\nDo you wish to proceed?"; then
    if zenity --question --title="$gui_title" --width=$gui_width --text="This script collection does not include icons. Do you have your own icon (.png or .svg) that should be used?\nIf you have, answer yes, then select it.\n\nIf unsure, answer no."; then
      icon_path=$(zenity --file-selection)
    fi

    if ! [ -z $icon_path ]; then
      file_content="${file_content}Icon=${icon_path}"
      finish_message="${finish_message}And ${icon_path} was used as its icon."
      echo "Added icon to desktop entry"
    fi

    echo "$file_content" > ~/.local/share/applications/tdp-gui.desktop
    zenity --info --title="$gui_title" --width=$gui_width --text="$finish_message"

  fi
fi