#!/usr/bin/sh

####### 
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2023********
##   blackPanther Default Apps Configuration
#####


export TEXTDOMAIN="libdiscover"
export TEXTDOMAINDIR="/usr/share/locale"

xmsg=$(which kdialog 2>/dev/null || which pydialog 2>/dev/null)

if [ -x "$xmsg" ];then

dbusRef=$($xmsg --desktopfile org.kde.discover \
    --title "$(gettext 'Discover')" \
    --icon plasmadiscover \
    --progressbar "<h2>$(gettext 'Open Discover')</h2> \
    $(gettext 'Refreshing Cache…')")

kdialogPid=`echo $dbusRef |cut -d- -f2 |cut -d' ' -f1`
qdbus $dbusRef showCancelButton true
qdbus $dbusRef Set "" "maximum" 0;

#pkcon refresh cache -c 1 &
pkcon -y -p refresh force &
sleep 1
pkPid=$(pidof pkcon)

if [ -n "$pkPid" ] ;then
#while ps --no-headers $kdialogPid &>/dev/null && ps --no-headers $pkPid &>/dev/null && [ "`qdbus $dbusRef org.kde.kdialog.ProgressDialog.wasCancelled`" != "true" ]; do
    while [ "$(qdbus $dbusRef org.kde.kdialog.ProgressDialog.wasCancelled 2>/dev/null)" = "false" ] &&  ps --no-headers --format args $pkPid &>/dev/null; do
	Stat=$(qdbus $dbusRef org.kde.kdialog.ProgressDialog.wasCancelled 2>/dev/null)
	sleep 1
    done
fi

if [ -n "$dbusRef" ];then
 kill $pkPid 2>/dev/null
 qdbus $dbusRef close 2>/dev/null
fi

PIDCHK="$(ps aux --no-header | grep -v grep | grep discover | grep dialog | awk '{print $2}')"
if [ -n "$PIDCHK" ];then
    echo "Force stop dialog..:"
    kill -KILL $PIDCHK
fi

fi

exec /usr/bin/plasma-discover "$@"
