#!/bin/bash

# Yad based front-end to dpkg & apt as an alternative for gdebi-gtk/kde tools
# Copyright Paweł "pavroo" Pijanowski 2018-2022 under GNU GPL
# Last update 2022/02/18

testroot="`whoami`"
if [ "$testroot" != "root" ]; then
	echo "must be root... exiting..."
	exit 1
fi

TEMPDIR="/tmp/debitool"
INSTALL="apt-get install"
#REINSTALL="apt-get install --reinstall"
REINSTALL="dpkg -i"
UNINSTALL="apt-get purge"
CLEAN="apt-get autoremove"
if [ -f /usr/bin/sparky-xterm ]; then
	XTERM="/usr/bin/sparky-xterm"
elif [ -f /usr/bin/x-terminal-emulator ]; then
	XTERM="/usr/bin/x-terminal-emulator"
else
	echo "sparky-xterm or x-terminal-emulator is missing... Exiting..."
	exit 1
fi

DEBFILE=`cat $TEMPDIR/debname`
cd $TEMPDIR
DEBPACK=`ls | grep *.deb`

if [ "$1" = "remove" ]; then
	# remove deb
	$XTERM "$UNINSTALL $DEBFILE"
	$XTERM "$CLEAN"

elif [ "$1" = "reinstall" ]; then
	# reinstall deb
	$XTERM "$REINSTALL $DEBPACK"

elif [ "$1" = "inst" ]; then
	# install local deb
	$XTERM "$INSTALL ./$DEBPACK"
	#$XTERM "$UPDATE"
	#$XTERM "$GDEBI $DEBPACK"

#elif [ "$1" = "repoinst" ]; then
#	# install repo's deb
#	$XTERM "$INSTALL $DEBFILE"
fi

rm -rf $TEMPDIR

exit 0
