#!/bin/bash

if [ "$#" != "1" ]; then
  echo 'Error: missing non-optional parameter'
  exit 1
fi

ask() { zenity --question --text="$1"; }
ask_rm_launcher() { ask "Not intending to ever use $*, do you wish to <b>remove this launcher from the menu</b>?"; }
rm_desktop_file() { gksu -D "debwin-download-package" -- rm "/usr/share/applications/download_$1.desktop"; }
terminal() {
  for term in xterm x-terminal-emulator xfce4-terminal gnome-terminal lxterminal konsole kterm mrxvt rxvt; do
    which $term >/dev/null && echo $term && break
  done
}
spawn_downloader() { gksu -D "debwin-download-package $1" -- $(terminal) -e sh -c "debwin-download-package $1 && $(rm_desktop_file $1)" ; }

download_dropbox() {
  if ask 'Dropbox is not currently installed.\n\nDo you wish to <b>install Dropbox now</b>?'; then
    spawn_downloader dropbox
  elif ask_rm_launcher 'Dropbox'; then
    rm_desktop_file dropbox
  fi
}

download_skype() {
  if ask 'Skype is not currently installed. Unlike most of what you see here, <b>Skype is non-free software</b> that puts strict <b><a href="http://stallman.org/skype.html">restrictions on your liberty</a></b>.\n\nGood free and open-source Skype <b>alternatives are <a href="https://jitsi.org">Jitsi</a>, <a href="http://www.ekiga.org">Ekiga</a>, <a href="http://www.linphone.org">Linphone</a>, or <a href="http://tox.im">Tox</a></b>.\n\nDo you wish to install Skype anyway?'; then
    spawn_downloader skype
  elif ask_rm_launcher 'Skype'; then 
    rm_desktop_file skype
  fi
}

download_steam() {
  if ask 'Steam for Linux is not currently installed.\n\nDo you wish to <b>install Steam client now</b>?'; then
    spawn_downloader steam
  elif ask_rm_launcher 'Valve Steam'; then
    rm_desktop_file steam
  fi
}

download_teamviewer() {
  if ask 'A superior <b>alternative to Teamviewer</b> is Chromium web browser (already installed) with <b><a href="https://chrome.google.com/webstore/detail/chrome-remote-desktop/gbchcmhmhahfdphkhkmpfmihenigjmpp">Chrome Remote Desktop</a> plugin</b> (by Google). Thus, Teamviewer is not currently installed or recommended.\n\nDo you wish to install Teamviewer anyway?'; then
    spawn_downloader teamviewer
  elif ask_rm_launcher 'Teamviewer'; then
    rm_desktop_file teamviewer
  fi
}

case "$1" in
  dropbox)    download_dropbox ;;
  skype)      download_skype   ;;
  steam)      download_steam   ;;
  teamviewer) download_teamviewer ;;
  *) echo "no download rules for package $1"; exit 2 ;;
esac

exit 0
