#!/bin/bash

# Last update 2020/10/05 by pavroo

# get default's locale file
DEFLOCDIR="/usr/share/pearl/pearl-aptus"
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
. $DEFLOCDIR/es
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 ja`" != "" ]; then
. $DEFLOCDIR/ja
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 zh_CN`" != "" ]; then
. $DEFLOCDIR/zh_CN
else
. $DEFLOCDIR/en
fi

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

DIALOG700="yad --window-icon=application-x-deb --width=700 --height=600 --center"
DIALOG="yad --window-icon=application-x-deb --width=450 --height=200 --center"
TITLE="--always-print-result --dialog-sep --image=application-x-deb --title="
TEXT="--text="
FILESELECTION="--file "
FILTER="--file-filter"
MSGBOX="--button=Ok:0"
TITLETEXT="APTus"
if [ -f /usr/bin/pearl-xterm ];then
	PEARLXTERM="/usr/bin/pearl-xterm"
else
	echo "pearl-xterm is missing... Exiting..."
	exit 1
fi

# check connection
PINGTEST=$(ping -c 1 debian.org | grep [0-9])

if [ "$PINGTEST" = "" ]; then
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL16"
	exit 1
fi

DEBFILE=`$DIALOG700 $TITLE"$LOCAL11" $FILESELECTION $FILTER="deb file |*.deb"`

if [ "$?" != "0" ]; then
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"$LOCAL12"
	exit 0

else
	$PEARLXTERM "gdebi $DEBFILE"

	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"$LOCAL8"
	exit 0
fi

exit 0
