#!/bin/sh
. /etc/blackPanther-default-apps.conf
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2011********

case $LANG in 
	hu*)
	welcomepackfind=$welcomepackfind_hu
	nopackages=$nopackages_hu
	discoverend=$discoverend_hu
	findto=$findto_hu
	reporesults=$reporesults_hu
	wantinstall=$wantinstall_hu
	pcresults=$pcresults_hu
	other=$other_hu
	findaction=$findaction_hu
	endaction=$endaction_hu
	detialed=$detialed_hu
	header=$header_hu
	localfind=$localfind_hu
	requirename=$requirename_hu
	chkdbstatus=$chkdbstatus_hu
	chkdbagain=$chkdbagain_hu
	chknetstatus=$chknetstatus_hu
	accessyes=$accessyes_hu
	databasetimeout=$databasetimeout_hu
	dbwaito=$dbwaito_hu
	helptxt="${GRN}	Használat:
	--segits     Ez a felhasználói segítség
	--helyi      Keresés a telepített csomagok között
	--reszletes  Részletezett csomagkeresés

	${CYN}Például:
	${YEL}csomagkereses amarok${DEF}
	"

	
	;;
	*)
	welcomepackfind=$welcomepackfind_en
	nopackages=$nopackages_en
	discoverend=$discoverend_en
	findto=$findto_en
	reporesults=$reporesults_en
	wantinstall=$wantinstall_en
	pcresults=$pcresults_en
	other=$other_en
	findaction=$findaction_en
	endaction=$endaction_en
	detialed=$detialed_en
	header=$header_en
	localfind=$localfind_en
	requirename=$requirename_en
	chkdbstatus=$chkdbstatus_en
	chkdbagain=$chkdbagain_en
	chknetstatus=$chknetstatus_en
	accessyes=$accessyes_en
	databasetimeout=$databasetimeout_en
	dbwaito=$dbwaito_en	
	helptxt="Usage:
	--help      This messages show
	--local     Find for installed packages
	--detailed  Detailed package search
	
	${CYN}Example:
	${YEL}packagefinder amarok$DEF
	"

	;;
esac


netchk() {
if [ "$1" = "1" ];then
    echo -n " * $chknetstatus "
fi

iNET=`sh default-netchk`
echo -en "[${GRN}$iNET${DEF}] "
[ $iNET != OK ]&& exit


}

prochk() {
if [ "$1" = "1" ];then
    echo -n " * $chkdbstatus "
    COUNTER=0
fi
process=`ps --no-header -Ao comm | grep rpm`
if [ -n "$process" ];then
    if [ "$1" = "1" ];then
	echo -en "[${RED}FAIL${DEF}] "
	echo ""
    fi
    waitol
 else
    if [ "$1" = "2" ];then
	echo -ne "${MAG} $accessyes ${DEF}"
	echo ""
	echo -ne "${DEF} * $chkdbagain ${DEF}"
    fi
	    echo -en "[${GRN}OK${DEF}] "
fi
if [ -f /var/lib/rpm/.RPMLOC ];then
	echo "DATABASE LOCKED! "
	exit
fi
}

prockill() {
    echo notimplementedyet
}

waitol() {
    if [ $COUNTER = 1 ];then
	echo ""
	echo -e "${RED} * $databasetimeout ${DEF}"
	echo ""
	exit
    fi
    if [ $COUNTER = 0 ];then
	echo -en "${YEL} * $dbwaito (max $((${WAITIME} * 30)) sec): "
    fi
    COUNTER=`expr ${COUNTER} + 1`
    echo -n "."
    sleep $WAITIME
    prochk 2
}

error() {
    echo "ERROR $1"
}

header() {
echo -e "
$MAG $welcomepackfind
"
}
#echo -e " 
#${MAG} $header ${DEF}
#"
header
locfind() {
 echo -e "$DEF __${other}${space}"
 echo ""
 echo -en "$GRN * $pcresults $YEL"
 echo -e "" 
 rpmquery -a | grep $(echo $@|sed -e 's|--detailed ||' -e 's|--reszletes ||' -e 's|--helyi||' -e 's|--local||')
if [ $? = 1 ];then
     echo -e "$MAG ** $nopackages $RED$@"
fi
footer
exit
}

footer() {
echo ""
echo -e "$DEF __${endaction}_______________________________________________________________"
echo -e "$DEF ** $discoverend
"
}

OPT="-f"
if [ "$1" = "--segits" -o "$1" = "--help" ];then
    echo -e " $helptxt "
    #echo ""
    exit
elif [ "$1" = "--reszletes" -o "$1" = "--detailed" ];then
	if [ "$2" = "" ];then
	    echo -e "${RED} ** $requirename ${DEF}"
	    echo ""
	    exit
	fi
    OPT='--sources --fuzzy'
elif [ $1 = "--helyi" -o "$1" = "--local" ];then
	if [ "$2" = "" ];then
	    echo -e "${RED} ** $requirename ${DEF}"
	    echo ""
	    exit
	fi
	other=$localfind
	space="_________________________________________"
	item=$(echo $@ | sed -e 's|--helyi||' -e 's|--local||')
	locfind $item
	exit
	
fi

SAMPLE=$(echo $@|sed -e 's|--[a-z]\+ ||')

echo -e "$DEF __${findaction}______________________________________________________________________"
echo ""
echo -e "$GRN * $findto ${YEL}$SAMPLE${DEF}"
echo -en "$GRN ** $reporesults \n$RED"
echo -en " ** "
#echo "urpmq $OPT $(echo $@|sed -e 's|--detailed ||' -e 's|--reszletes ||' -e 's|--helyi||' -e 's|--local||')"
#echo "urpmq $OPT $SAMPLE"
urpmq $OPT "$SAMPLE"
if [ $? = 0 ];then
    echo -e "$BLU
 ** $wantinstall $SAMPLE"
 else
    space="_______________________________________________________________________"
    locfind "$SAMPLE"
 
fi

footer
