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

case $LANG in 
	hu*) 
	notroot=$notroot_hu
	header=$header_hu
	chkdbstatus=$chkdbstatus_hu
	chkdbagain=$chkdbagain_hu
	chknetstatus=$chknetstatus_hu
	accessyes=$accessyes_hu
	databasetimeout=$databasetimeout_hu
	dbwaito=$dbwaito_hu
	;;
	*) 
	notroot=$notroot_en
	header=$header_hu
	chkdbstatus=$chkdbstatus_en
	chkdbagain=$chkdbagain_en
	chknetstatus=$chknetstatus_en
	accessyes=$accessyes_en
	databasetimeout=$databasetimeout_en
	dbwaito=$dbwaito_en
	;;
esac


echo -e " 
${MAG} $header ${DEF}
"
netchk() {
if [ "$1" = "1" ];then
    echo -n " * $chknetstatus "
fi

NET=`sh default-netchk`
if [ "$NET" = "OK" ];then
echo -en "[${GRN}OK${DEF}] "
else
echo -en "[${RED}FAIL${DEF}] "
    echo ""
    exit
fi

}

prochk() {
if [ "$1" = "1" ];then
    echo -n " * $chkdbstatus "
    COUNTER=0
fi
process=`ps --no-header -Ao comm | grep urpm`
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
 #else
 #   echo -en "[${GRN}OK${DEF}] "

fi
}

prockill() {
    echo notimplementedyet
}

waitol() {
    if [ $COUNTER = 30 ];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"
}


case $1 in 
	minden|all)
	    param=" --auto-update"
	    ;;
	csomagok|packages)
	    param=" --auto-select"
	    ;;
	Media[0-10])
	    param=".update $1"
	    ;;
	forrasok|repos)
	    param=".update -a"
	    ;;
	forras|repo)
	    param=".update $2"
	    ;;
	rendszer|system)
	    param=" --auto-select"
	    ;;
	segitseg|help)
    echo -e "${GRN}opciók/options: 
	$(echo `basename $0`) ([minden|all][csomagok|packages]
		[forrasok|repos][forras|repo] 
		[rendszer|system][segitseg|usage]
		[info|hasznalat|--help|usage])
			
	${YEL}example: $(echo $0 | sed 's|.*/bin/||') repo XYREPONAME
			"
	    exit
	    ;;
	info|-h|--help|--segits|hasznalat|usage)
	    /usr/sbin/urpmi |sed -e 's,Man.*,blackPanther Europe,' -e 's,Mage.*,blackPanther Europe,' -e 's,urpmi ,blackPanther OS Package Manager based on urpmi ,'
	    echo -e "${GRN}"
	    frissites help
	    echo -e "${DEF}"
	    exit
	    ;;
	--*)
	    param=" $@"
	    ;;
	*)
	    param=" --auto-select"
	    ;;
esac

netchk 1
prochk 1
echo ""
echo ""

if [ $UID != 0 ];then 
    echo -e "${RED} $notroot ${DEF}"
    echo -en "${YEL} * "
    su - -c "urpmi$param"
    exit
 else
    echo -en "${GRN} * "
    urpmi$param
    echo -e "${DEF}"
fi
echo ""