#!/bin/sh                                                                   
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2016********
#alias GETTEXT='gettext "default-apps"'
export PATH=$PATH:/usr/sbin:/sbin
export TEXTDOMAIN="default-apps"
export TEXTDOMAINDIR="/usr/share/locale"
. /etc/blackPanther-default-apps.conf
#. gettext.sh
engapp=$(which urpmi 2>/dev/null||which smart 2>/dev/null||which rpm 2>/dev/null )
engapp=$(echo $engapp | sed 's|.*bin/||')

case $LANG in 
	*) 
	notroot=$(gettext "* You are not system admin [root]. Please usage [su] command to login and run this application again or please enter root password now!")
	header=$(gettext "* Packages and repository management of blackPanther OS based on command promt")
	chkdbstatus=$(gettext "Checking Database Status:")
	chkdbagain=$(gettext "Checking Database Status Again:")
	chknetstatus=$(gettext "Checking Internet Status:")
	accessyes=$(gettext "Access Possible")
	databasetimeout=$(gettext "The database access timeout. Please try again later!") 
	dbwaito=$(gettext "The Database Busy Now. I Wait to Database Access")
	helping=$(gettext "   [all | packages]  and/or  [repos | repo]  - to packages and/or repository update\\n\\t\\t[system] - to system upgrade\\n\\t\\t[help] - to short help display\\n\\t\\t[--info|--help|usage] - to long help to usage\\n\\n\\t\\t[db|database] - to fixating broken RPM Database\\n\\t\\t[fix|repair] - Repair the RPM packages connections Run: updating repo first!\\n\\t\\t[clean] - Clean downloaded RPM cache or broken packages\\n")
	ok=$(gettext "OK")
	fail=$(gettext "FAIL")
	;;
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 $(gettext "DATABASE LOCKED! ")
	exit
 #else
 #   echo -en "[${GRN}$ok${DEF}] "

fi
}

smartchk() {
    smart=$(which smart)
    if [ -x "$smart" ];then
	echo -n " * "
	echo -e ${BLU}$(gettext "Smart Found on Your System!")${DEF}
	smart=OK
    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}$(gettext "$dbwaito (max $((${WAITIME} * 30)) sec")):${RED}"
    fi
    COUNTER=$(expr ${COUNTER} + 1)
    echo -n "."
    sleep $WAITIME
    prochk 2
}

error() {
    echo $(gettext "ERROR") $1
}                             


case $1 in 
	adatbazis*|db*|database)
	    engapp='rpm'
	    param=" --rebuilddb"
	    ;;
	fix*|repa*|javit*)
	    smartchk
	    if [ "$smart" = "OK" ];then
	    engapp='smart'
	    param=" fix"
	    else
		echo $(gettext "Missing SMART engine for Fixating System Packages")
	    fi
	    ;;
	minden|all)
	    netchk 1
	    param=" --auto-update"
	    ;;
	csomagok|packages)
	    netchk 1
	    param=" --auto-select"
	    ;;
	Media[0-10])
	    netchk 1
	    param=".update $1"
	    ;;
	forrasok|repos)
	    if [ "$engapp" = "smart" ];then
		param=" update"
	    elif [ "$engapp" = "urpmi" ];then
		param=".update -a"
	    else
		engapp="echo"
		param=" ERROR IN THE PACKAGE ENGINE! PLEASE REPORT..."
	    fi
	    smartchk
	    netchk 1
	    ;;
	forras|repo)
	    netchk 1
	    param=".update $2"
	    ;;
	rendszer|system)
	    netchk 1
	    param=" --auto-select"
	    ;;
	urit*|tisztit*|clean*)
	    smartchk
	    if [ "$smart" = "OK" ];then
	    engapp='smart'
	    param=" clean"
	    elif [ "$smart" != "OK" ];then 
		rm -f /var/cache/urpmi/rpms/*
	    fi
	    ;;
	urpm|urpmi|rpm)
	    engapp='echo'
	    param=" -e \n[-] $(gettext ' Unknown') ${RED}'"$1"'${GRN}$(gettext ' option! Try with '"--help"' option. This is not a Mandriva/Mandrake!\n')"
	    ;;
	segits*|help*|info*)
    echo -e ${GRN} ' ' $(gettext "Options of Updating Application:")
    echo ""
    echo -e "   $(echo `basename $0`) $helping"
    echo -e 	${YEL} $(gettext "\\tExample:") `basename $0` $(gettext 'repo XYREPONAME')
    echo ""
	    exit
	    ;;
	--info|-h|--help*|--segits*|hasznalat|usage)
	    $engapp |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=" $@"
	    ;;
	*)
	    netchk 1
	    param=" --auto-select"
	    ;;
esac

prochk 1
echo ""
echo ""

if [ $UID != 0 ];then 
    echo -e "${RED} $notroot ${DEF}"
    echo -e "${YEL}"$(gettext "__Started_____________________________________ ")${GRN}
    su - -c "$engapp$param" && echo -e ${BLU}$(gettext "[+] This Command Successfully Completed")
    $runsmart
    echo -e "${YEL}"$(gettext "__End_________________________________________ ")
    
    exit
 else
    #echo -en "${GRN} * "
    echo -e "${YEL}"$(gettext "__Started_____________________________________ ")${GRN}
    if [ "$smart" = "OK" ]&&[ -n $(echo "$*" | grep update) ];then
	echo
	echo -en ${BLU} $(gettext " - the SMART database update in background.. ")${DEF}
	smart --quiet update && echo -e " - $(gettext 'the SMART Database Update :')    [${GRN} $(gettext Done)${DEF} ]"
	echo
    fi
    #echo "$engapp$param && echo -e ${BLU}$(gettext '[+] This Command Successfully Completed')"
    $engapp$param && echo && echo -e ${BLU}$(gettext '[+] This Command Successfully Completed')
    $runsmart
    echo -e "${YEL}"$(gettext "__End_________________________________________ ")${DEF}
    #echo -e "${DEF}"
fi
echo ""
