#!/bin/bash

Info () {
local title
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2021"
app="$(basename $0)"
msgid="The Starbuntu application
<i>\$app</i>
was written by"
info="$(gettext "$msgid" | (export PATH `envsubst --variables "$msgid"`; envsubst "$msgid"))"

yad --center --fixed --title="$title" --window-icon="info" --image="info" --borders=20 --text="$info"$'\n'$'\n'"$author"$'\n'"$mail"$'\n'$'\n'"$year"$'\n' --text-align=center --selectable-labels --button="OK" &
wunwa "$title" > /dev/null; xdotool key Home key Down
}

Contents () {
    local pre=$"Contents Of " pkg="$1"
    ! $empty && list=($(dpkg -L "$pkg" 2> /dev/null)) || list=($(tar -tf /tmp/hsperfdata_root/pkgs/$pkg/data.tar.*))
    list=(${list[@]//.\//\/})
    if [ ${#list[@]} -eq 0 ]; then
	wget https://packages.ubuntu.com/$DISTRIB_CODENAME/amd64/$pkg/filelist -O /tmp/hsperfdata_$USER/$pkg-filelist.html
	list="$(cat /tmp/hsperfdata_$USER/$pkg-filelist.html)"
	list="${list#*<pre>}"; list="${list%$'\n'</pre>*}"
	list=($list)
	rm /tmp/hsperfdata_$USER/$pkg-filelist.html
    fi
    yad --center --title="$pre$pkg" --width=600 --height=500 --borders=20 --window-icon="$HOME/.icons/lucky_bag.png" --list --column="Contents" --no-headers --search-column=1 --regex-search ${list[@]} --button="OK":0 &
}

details () {
    local pre="$1" pkg="$2"
    if [ "$pre" = "Install" ]; then
        pre="$(gettext "Install")"
    else
        pre="$(gettext "Remove")"
    fi
    ! $empty && show="$(apt-cache show "$pkg" | sed "s!Package!$(gettext "Package")!g")" || show="$(cat /tmp/hsperfdata_root/pkgs/$pkg/control)"
    titel="${show%%$'\n'*}"
    text="$(echo "$show" | sed "s!Architecture:!$(gettext "Architecture:")!g; s!Depends:!$(gettext "Depends:")!g; s!Recommends:!$(gettext "Recommends:")!g; s!Suggests:!$(gettext "Suggests:")!g; s!Filename:!$(gettext "Filename:")!g; s!Replaces:!$(gettext "Replaces:")!g; s!Description!$(gettext "Description")!g")"
    yad --center --title="$pre $titel" --window-icon="$HOME/.icons/lucky_bag.png" --borders=20 --width=1280 --height=768 --wrap --text-info <<< "$text" --editable --button=$(gettext "Cancel"):1 --button="$(gettext "Contents")":"$0 contents $pkg" --button="Info":"$0 info" --button="OK":0
}

do_install () {
    local pkg="$1"
    if ! $empty; then
	apt install $pkg
    else
	apt -s install /tmp/hsperfdata_root/pkgs/$pkg/*.deb > /dev/null
	if [ $? -gt 0 ]; then
	    apt update
	    apt install $pkg
	else
	    apt install /tmp/hsperfdata_root/pkgs/$pkg/*.deb
	fi
    fi
    [ -n "$(dpkg -l $pkg 2>/dev/null | grep ii)" ] && echo "$pkg" >> /var/lib/apt/lists/auxfiles/pilist && sed -Ei "/^$pkg$/d" /var/lib/apt/lists/auxfiles/pnilist
    appmenu-refresh 2>/dev/null; obamenu-refresh &
    echo
    echo $end_msg
    read
} 

param="$1"
[ -z "$(find /var/lib/apt/lists -maxdepth 1 -type f -not -name "lock")" ] && empty=true || empty=false
arch="$(dpkg --print-architecture)"
end_msg="$(gettext "[ End, please press Return! ]")"

[ "$param" = "info" ] && Info && exit 0
[ "$param" = "contents" ] && Contents "$2" && exit 0
[ "$param" = "do_install" ] && do_install "$2" && exit 0

IFS=$'\n'

if [[ "$USER" != "root" ]]; then
    gsu --no-error-msg $0 &
    exit 0
fi

[[ ! -d /tmp/hsperfdata_root ]] && mkdir /tmp/hsperfdata_root
. /etc/lsb-release

title=$"Package Manager"
spalte1=$"Package(s) to be installed:"
spalte2=$"Package(s) to be removed:"
button1=$"Cancel"
button2=$"Help"
lang_dir=$"C"
warning=$"&#10;    Please wait!&#10;    Package lists are being created ...&#10;"

yad --center --fixed --title="$title" --borders=20 --window-icon="$HOME/.icons/lucky_bag.png" --image="dialog-warning" --text="$warning" --no-buttons --close-on-unfocus &

pilist=($(cat /var/lib/apt/lists/auxfiles/pilist | sort))
pnilist=($(cat /var/lib/apt/lists/auxfiles/pnilist | sort))

n=$RANDOM
yad --plug=$n --tabnum=1 --list --multiple --editable --column="$spalte1" --no-headers --search-column=1 --regex-search ${pnilist[@]} 1>/tmp/hsperfdata_root/install &
yad --plug=$n --tabnum=2 --list --multiple --editable --column="$spalte2" --no-headers --search-column=1 --regex-search ${pilist[@]} 1>/tmp/hsperfdata_root/remove &
yad --center --notebook --key=$n --title=$title --width=420 --height=618 --borders=10 --window-icon="$HOME/.icons/lucky_bag.png" --no-escape --tab="$spalte1" --tab="$spalte2" --button="$button1":1 --button="$button2":"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#pkg-manager" --button="Info":"$0 info" --button="OK":0

exitstatus=$?

if [ $exitstatus -eq 0 ] ; then
    inst_pkg=$(cat /tmp/hsperfdata_root/install); rm /tmp/hsperfdata_root/install
    rm_pkg=$(cat /tmp/hsperfdata_root/remove); rm /tmp/hsperfdata_root/remove
    inst_pkg=(${inst_pkg//\|/}); rm_pkg=(${rm_pkg//\|/})
    if $empty; then
        all_pkg=(${inst_pkg[@]} ${rm_pkg[@]})
	mkdir /tmp/hsperfdata_root/pkgs
	for ((k=0; k<${#all_pkg[@]}; k++)); do
	    mkdir /tmp/hsperfdata_root/pkgs/${all_pkg[$k]}
	    cd /tmp/hsperfdata_root/pkgs/${all_pkg[$k]}
	    wget https://packages.ubuntu.com/$DISTRIB_CODENAME/$arch/${all_pkg[$k]}/download -O download.html
	    wget $(grep "kernel" /tmp/hsperfdata_root/pkgs/${all_pkg[$k]}/download.html | cut -d'"' -f2)
	    deb_file=$(grep "kernel" /tmp/hsperfdata_root/pkgs/${all_pkg[$k]}/download.html | cut -d'"' -f2 | cut -d'/' -f9)
	    ar vx $deb_file; tar xvf control.tar.*
	done
    fi
    title=$"Package Manager"
    text=$"Display Package Details?&#10;"
    list=(                  $"No"                   $"Yes")
    reply=`yad --center --title="$title" --borders=20 --window-icon="$HOME/.icons/lucky_bag.png" --text="$text" --list --column="Details y/n" --no-headers ${list[@]} --close-on-unfocus`
    if [ $? -gt 0 ]; then
	exit 255
    elif [[ $reply =~ $"Yes" ]]; then
	button=2
    else
	button=1
    fi
    if [ $button -eq 2 ]; then
	for ((k=0; k<${#inst_pkg[@]}; k++)); do
	    details Install ${inst_pkg[$k]}
	    [ $? -eq 1 ] && unset inst_pkg[$k] && inst_pkg=(${inst_pkg[@]})
	done
	for ((k=0; k<${#rm_pkg[@]}; k++)); do
	    details Remove ${rm_pkg[$k]}
	    [ $? -eq 1 ] && unset rm_pkg[$k] && rm_pkg=(${rm_pkg[@]})
	done
    fi
else
    exit 2
fi

for i in ${inst_pkg[@]}; do xterm -T "$(gettext "Install")"" $i" -e "$0 do_install $i"; done
for r in ${rm_pkg[@]}; do
    xterm -T "$(gettext "Remove") $r" -e "apt purge $r; apt --purge autoremove; echo; echo $end_msg; read"
    [ -z "$(dpkg -l $r 2>/dev/null | grep ii)" ] && appmenu-refresh 2>/dev/null && obamenu-refresh & echo "$r" >> /var/lib/apt/lists/auxfiles/pnilist && sed -Ei "/^$r$/d" /var/lib/apt/lists/auxfiles/pilist
done

