#!/bin/bash
########################################################################
#                                                                      #
#  Small script that lists all installed packages. Double click on     #
#  a package shows package info and a link to the package homepage.    #
#  "Save to file" saves the list to text file without the lib packages #
#  Only tested on Mint 19 & 20.                                        #
#  Depends on: apt, dpkg, yad and an internet browser                  #
#                                                                      #
#  Made by Koentje  (remon@cobrasoft.nl)                               #
#                                                        version 1.4   #
########################################################################

arch=`dpkg --print-architecture`


# Open homepage url of package in browser
function open_file {
   xdg-open "$url"
}
export -f open_file


# Uninstall package
function uninstall_package {
 # open dialog with box to type "yes" for uninstall.
 # If yes is typed, then dialog with uninstall info
 # After uninstall then reload main
 $HOME/bin/mousepointer hourglass
 dialog_width="1150"
 dialog_height="570"
 dialog_font="Ubuntu Mono 13"
 $HOME/bin/mousepointer default

yad --text="Type 'yes' and click 'OK' to uninstall or leave blank to exit."\
 --entry\
 --width="500" --height="80"\
 --buttons-layout=spread\
 --button="OK"


exit


 echo $package | yad --text-info --margins=6 --back=#181818\
 --title=" Uninstall:  $package "\
 --center --fixed --wrap\
 --tail --always-print-result\
 --fontname="$dialog_font"\
 --text-align=left\
 --width="$dialog_width" --height="$dialog_height"\
 --buttons-layout=spread\
 --button="OK"
}
export -f uninstall_package


# Open package informationn dialog
function info_dialog () {
 $HOME/bin/mousepointer hourglass
 tmpfile="/tmp/yad-show-package.tmp"
 dialog_width="1150"
 dialog_height="570"
 dialog_font="Ubuntu Mono 13"
 apt-cache show "$2" > "$tmpfile"
 export url=$(cat "$tmpfile" | grep "Homepage:" | head -n1 | awk '{print $2}')
 export package=$(cat "$tmpfile" | grep "Package: " | awk '{print $2}')
  if ! [ "$url" = "" ]; then buttonhomepage="--button=Website:bash -c open_file"; fi
 $HOME/bin/mousepointer default

 cat $tmpfile | yad --text-info --margins=6 --back=#181818\
 --title=" Package info:  $package "\
 --center --fixed --wrap\
 --tail --always-print-result\
 --fontname="$dialog_font"\
 --text-align=left\
 --width="$dialog_width" --height="$dialog_height"\
 --buttons-layout=spread\
 --button="OK"\
 "$buttonhomepage"\
 --button="Uninstall:bash -c uninstall_package"
}
export -f info_dialog


# Open main form with all installed packages
function main () {
 main_width="1900"
 main_height="800"
 export input
 input=$(dpkg -l | sort -fbn | tail -n +4 | grep -F "ii" | awk -F" "  '{ printf $1"\n" $2"\n" $3"\n" $4"\n"; $1=$2=$3=$4=""; printf substr($0,5)"\n" }' | tr "'" " " | yad --list\
 --width="$main_width" --height="$main_height"\
 --title=" Show installed packages - ($arch)  "\
 --separator=' '\
 --window-icon=utilities-system-monitor\
 --center --vscoll-policy=always\
 --window-icon=application\
 --text-align=left\
 --button=" Save to file  "!gtk-save-as!" Save list to file (without lib packages) ":200\
 --button=" Close"!gtk-close!" Bye.. ":202\
 --buttons-layout=spread\
 --dclick-action="bash -c 'info_dialog %s'"\
 --add-preview\
 --column-width=10 --grid-lines=both --expand-column=100 --no-markup\
 --column="Inst:TEXT"\
 --column="Package name:TEXT"\
 --column="Version number:TEXT"\
 --column="Platform:TEXT"\
 --column="Short description:TEXT") #"

 case $? in
   200)		# Cancel
    echo "Save package list to  $HOME/installed-software.txt"
    comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u) | grep -Ev ^lib > "$HOME/installed-software.txt"
    xdg-open "$HOME/installed-software.txt"
    main
   ;;
   202)		# Cancel
    echo "Program halted."
    exit
   ;;
   252)		# Escape exit
    echo "Escaped."
    exit
   ;;
   *)
    echo "Exit nr: $1"
   ;;
 esac
}

main
