#!/bin/bash

# Application remover
# Copyright 2020-2025 Paweł "pavroo" Pijanowski under GNU GPL 3
# Last update 2025/05/04 by pavroo

# get default's locale file
DEFLOCDIR="/usr/share/sparky/sparky-aptus-appcenter"
if [ "`cat /etc/default/locale | grep LANG= | grep de`" != "" ]; then
. $DEFLOCDIR/de
elif [ "`cat /etc/default/locale | grep LANG= | grep el`" != "" ]; then
. $DEFLOCDIR/el
elif [ "`cat /etc/default/locale | grep LANG= | grep es`" != "" ]; then
	if [ "`cat /etc/default/locale | grep LANG= | grep es_AR`" != "" ]; then
. $DEFLOCDIR/es_AR
	else
. $DEFLOCDIR/es_ES
	fi
elif [ "`cat /etc/default/locale | grep LANG= | grep fi`" != "" ]; then
. $DEFLOCDIR/fi
elif [ "`cat /etc/default/locale | grep LANG= | grep fr`" != "" ]; then
. $DEFLOCDIR/fr
elif [ "`cat /etc/default/locale | grep LANG= | grep hu`" != "" ]; then
. $DEFLOCDIR/hu
elif [ "`cat /etc/default/locale | grep LANG= | grep id_ID`" != "" ]; then
. $DEFLOCDIR/id_ID
elif [ "`cat /etc/default/locale | grep LANG= | grep it`" != "" ]; then
. $DEFLOCDIR/it
elif [ "`cat /etc/default/locale | grep LANG= | grep pl`" != "" ]; then
. $DEFLOCDIR/pl
elif [ "`cat /etc/default/locale | grep LANG= | grep pt_BR`" != "" ]; then
. $DEFLOCDIR/pt_BR
elif [ "`cat /etc/default/locale | grep LANG= | grep pt_PT`" != "" ]; then
. $DEFLOCDIR/pt_PT
elif [ "`cat /etc/default/locale | grep LANG= | grep ru`" != "" ]; then
. $DEFLOCDIR/ru
elif [ "`cat /etc/default/locale | grep LANG= | grep uk`" != "" ]; then
. $DEFLOCDIR/uk
elif [ "`cat /etc/default/locale | grep LANG= | grep zh_CN`" != "" ]; then
. $DEFLOCDIR/zh_CN
else
. $DEFLOCDIR/en
fi

CHECKNALA=`which nala`
if [ "$CHECKNALA" != "" ]; then
	UNINSTALL="nala purge"
else
	UNINSTALL="apt-get purge"
fi
AUROREMOVE="apt-get autoremove"
OOPACKAGES="openoffice-brand-en-us openoffice-brand-writer openoffice-brand-calc openoffice-brand-impress openoffice-brand-base openoffice-brand-draw openoffice-brand-math openoffice-pyuno openoffice-ooofonts openoffice-ooolinguistic openoffice-debian-menus"

if [ -f /usr/bin/spterm ]; then
	SPARKYXTERM="spterm -e"
elif [ -f /usr/bin/sparky-xterm ]; then
	SPARKYXTERM="sparky-xterm"
elif [ -f /usr/bin/x-terminal-emulator ]; then
	SPARKYXTERM="x-terminal-emulator -e"
else
	echo "x terminal emulator is missing... exiting..."
	exit 1
fi

# remove packge now
#kill -USR1 $YAD_PID
PACKAGE="$1"
#sleep 1

if [ "$PACKAGE" = "openoffice" ]; then
	#kill -USR1 $YAD_PID
	$SPARKYXTERM "$UNINSTALL $OOPACKAGES && echo 'Done, press <ENTER> to exit...' && read"

elif [ "$PACKAGE" = "protonvp" ]; then
	#kill -USR1 $YAD_PID
	$SPARKYXTERM "$UNINSTALL proton-vpn-gnome-desktop protonvpn-stable-release && $AUTOREMOVE && echo 'Done, press <ENTER> to exit...' && read"

elif [ "$PACKAGE" = "vmware" ]; then
	/usr/bin/sparky-vmplayer-installer
	exit 0

else
	#kill -USR1 $YAD_PID
	$SPARKYXTERM "$UNINSTALL $PACKAGE && echo 'Done, press <ENTER> to exit...' && read"
fi

if [ -f /usr/lib/sparky-aptus-appcenter/page-error.html ]; then
	rm -f /usr/lib/sparky-aptus-appcenter/page-error.html
fi

CHECKPACKAGE0=`apt-cache policy $PACKAGE | head -n2 | tail -n1 | grep [0-9]`
if [ "$CHECKPACKAGE0" = "" ]; then
	DIALOGTEXT="$LOCAL031"
else
	DIALOGTEXT="$LOCAL033"
fi

cat > /usr/lib/sparky-aptus-appcenter/page-error.html << FOO
<!DOCTYPE html>
<html>
<!-- 
APTus AppCenter by pavroo <pavroo@onet.eu> 2018
License: GNU GPL 3
Last update: October 26, 2020 by pavroo
-->
	<head>
		<title>Installation Status | APTus AppCenter</title>
		<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
		<script src="./js/lang.js" type="text/javascript"></script>
	</head>
<body>
<div id="wrapper">
	<div id="header">
		<div id="logo">
		</div>
	</div>
	<div id="menu-wrapper-back">
		<ul id="menu">
			<li><a href="index.html"><span>&laquo;-- </span></a></li>
			<li><a href="index.html"><span><script>dict.print('main', 'applications');</script></span></a></li>
			<li><a href="tools.html"><span><script>dict.print('main', 'aptus');</script></span></a></li>
			<li><a href="aptus://narz_search"><span><script>dict.print('main', 'search');</script></span></a></li>
			<li><a href="info.html"><span><script>dict.print('main', 'info');</script></span></a></li>
		</ul>
	</div>
	<div id="page">
		<div id="contentfull">
			<div class="contentbgfull">
				<div class="post">
				<div class="entry">
<table border="0" width="100%">
<div class="column0">
<tbody>
	<tr>
		<td><br />&nbsp;
		<br />&nbsp;
		$DIALOGTEXT<br />
		<br />&nbsp;
		<a href="index.html" class="buttonmenu">$LOCAL001</a><br />
		<br />
	</tr>
</tbody>
</div>
</table>
				</div>
				</div>
			</div>
		</div>
		<!-- end #content -->
	</div>
	<!-- end #page -->
</div>
</body>
</html>
FOO

#kill -USR1 $YAD_PID
#cd /usr/lib/sparky-aptus-appcenter
#./aptus-appcenter error01

exit 0
