#!/bin/sh
. /etc/blackPanther-default-apps.conf
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2011********
# installer engine selector for blackPanther OS  thanks for the idea of my a 10-years daughter :)
export PATH=$PATH:/usr/sbin:/sbin
export TEXTDOMAIN="default-apps"
export TEXTDOMAINDIR="/usr/share/locale"


case $LANG in 
	XY*) 
	installmsg=$installmsg_hu
	rpmimputerr=$rpmimputerr_hu
	enginerror="Hiba 1000! A telepítőmotor nem található! Kérem telepítsen egyet és futtassa újra a programot!\n Az elérhető csomagkezelők blackPanther OS alatt: rpm, urpmi, smart, yum, apt-rpm"
	enginefail="Bizony valami hiba történt a csomagkezelő motornál! (kód: 1010)\nA hiba kikerüléséhez futtassa újra a programot az ${YEL}'--engine='${RED} kapcsoló használtával,\na telepítés befejezéséhez vagy a hiba kijavításához. További elérhető csomagkezelő-motorok:\n\n${MAG} * rpm, urpmi, smart, yum, apt-rpm \n\n${GRN}Használat ha pl a smart csomagkezlő-motort válaszjuk:\n${CYN} $(basename $0) $@ --engine=smart${DEF} \n\nTovábbi részletekért futtassa a ${YEL}$(basename $0) --segits${DEF} parancsot."
	header=$header_hu
	chkdbstatus=$chkdbstatus_hu
	chkdbagain=$chkdbagain_hu
	chknetstatus=$chknetstatus_hu
	accessyes=$accessyes_hu
	databasetimeout=$databasetimeout_hu
	dbwaito=$dbwaito_hu
	helptxt="${GRN}
	Ez egy ügyes program ${BLU}blackPanther OS-hez.${GRN} Ha futtatod
	ezt a programot akkor automatikusan kiválasztja a telepítőmotort a csomagtelepítéshez.
	Jelenleg a következő motorokat támogatja : ${YEL}rpm|urpmi|yum|smart|apt-rpm${GRN}

	Ilyen könnyű használni:
	${CYN}Hagyományos telepítési minta : $(echo `basename $0`) csomagneve (vagy csomagneve-verzió-kiadás)
	
	${GRN}vagy (haladó felhasználóknak)
	${CYN}További telepítési minta: $(echo `basename $0`) csomagneve --engine=rpm
	
	${GRN}vagy (fejlesztőknek)
	${CYN}Bővített telepítési minta: $(echo `basename $0`) DEBUG csomagneve --engine=rpm

	${BLU}Üdvözlettel: Barcza Károly
	kbarcza@blackpanther.hu - www.blackpantheros.eu
	/Ezt a feature ötletet köszönöm a 10 éves lányomnak!/
	$DEF"
	;;
	*) 
	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!')
	installmsg=$(gettext "The following package[s] install have in progress:")
	rpmimputerr=$(gettext "Input error! The input only an any \*.rpm file ..")
	enginerror=$(gettext "[Error 1000] - The installer engine not found! Please install anyone and run command again!\nAvailable engines for blackPanther OS: rpm, urpmi, smart, yum, apt-rpm")
	enginefail=_$"So so an error occured during istallaton! [code: 1010]\\nPlease run command again with ${YEL}'--engine='${RED} parameter to finish package installing or fixing error.\\nAvailable additional packager-engines:\\n\\n${MAG} * rpm, urpmi, smart, yum, apt-rpm \\n\\n${GRN}Usage if you selected smart engine:\n${CYN} `basename $0` $@ --engine=smart${DEF} \\n\\nPlease run command again:  ${YEL}`basename $0` --help${DEF} to more information."
	header=$(gettext "Packages and repository management of blackPanther OS based on command promt")
	chkdbstate=$(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 "Wait to access to database..")
	helptxt=_$"${GRN}
	This is smart program for ${BLU}blackPanther OS.${GRN} If you running 
	the script then auto select an installer engine for package install. 
	Working with follow engines : ${YEL}rpm|urpmi|yum|smart|apt-rpm${GRN}
	
	Easy Usage:
	${CYN}Simple install example  : $(echo `basename $0`) packagename (or packagename-version-release)
	
	${GRN}or (for advanced users)
	${CYN}Advanced install example: $(echo `basename $0`) packagename --engine=rpm
	
	${GRN}or (for developers)
	${CYN}Extended install example: $(echo `basename $0`) DEBUG packagename --engine=rpm
	
	${BLU}Best Regards: Charles Barcza from Hungary
	kbarcza@blackpanther.hu - www.blackpantheros.eu
	/This feature thanks to 10 years old my daughter/
	$DEF"
	;;
esac

DEBUG=`echo "${@}" |grep DEBUG`
INPUT=`echo "${@}" | sed -e 's|DEBUG ||' -e 's|--engine=.*||' -e 's|-engine=.*||' -e 's|engine=.*||'`
FORCEENGINE=`echo "${@}" | sed -e 's|DEBUG ||' | grep engine= | sed 's|.*engine=||'`

dbg()
{
    	  if [ -n "$DEBUG" ]; then
       	echo
       	$SETCOLOR_FAIL
       	echo -e "DBG >> ${@}"
       	$SETCOLOR_NORMAL
    	  fi
}

if [ "$1" == "--usage" -o "$1" == "--segits" -o "$1" == "--help" ];then
    echo -e "$helptxt"
        exit
fi

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 " * $chkdbstate "
    COUNTER=0
fi
process=`ps --no-header -Ao comm | sed -e 's|rpmbuild||g' -e 's|rrpm||g' -e 's|.rpm||g' | 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 = 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"
}


aengine=/usr/sbin/urpmi
test -x $aengine || aengine=`which smart 2>/dev/null || which apt-get 2>/dev/null || which rpm 2>/dev/null || echo "NOENGINE"`

dbg "Auto selected engine: $aengine"
if [ ! -n "$FORCEENGINE" ];then
    packbin=`echo $aengine | awk -Fbin/  '{ print $2 }'`
    dbg "Packager binary: $packbin"
 else
    #engine="$FORCEENGINE"
    engine=`echo "$FORCEENGINE" | sed -e 's|.*--engine=||' -e 's|.*engine=||'`
    dbg "packbin=which '$engine' 2>/dev/null || echo NOENGINE"
    packbin=`which $engine 2>/dev/null`
    if [ -n "$packbin" ];then
	packbin="$engine"
     else
        packbin=NOENGINE
    fi
    dbg "Forced packager binary: $packbin"
    
fi


echo -e " 
${MAG} $header ${DEF}
"

ONLINE=`echo $INPUT | grep "://"`
if [ -n "$ONLINE" ];then
    netchk 1
fi

prochk 1 #&& echo ""

dbg "AppInput: [$INPUT]"

#filesall=""
#testrpm=`file -b "$1" | grep 'RPM'`
#while [ $# -gt 0 ]
#	do
#		files=`echo "$1" | sed 's/ /\?/g'`
#		filesall="$files $filesall"
#		shift
#	done
#admin=`whoami`
echo $testrpm
echo $files
            
if [[ -z `echo $INPUT | grep ".rpm$"` ]]&&[[ ! -z `echo "$INPUT" | grep /` ]];then
    echo -en "${RED} * $rpmimputerr ${DEF}"
    echo "
    "
    
    exit
fi

# local or no
local=`echo $INPUT | grep ".rpm"`

[ -n "$local" ]&& dbg "Local install: Yes"

if [ $UID != 0 ];then 
    echo -e "${RED} $notroot ${DEF}"
    echo -en "${YEL}  * "
    path=$([ ! -n "`echo $*| grep /`" ] && echo "`pwd`/")
    dbg "INPUTS: 1$1 2$2 3$3 4$4 5$5"
    for f in $1 $2 $3 $4 $5 ;do
         fajl=$(echo "$f" | grep .rpm)
         #fajl=$(echo "$f" | sed 's|-.*||g')
         #opt=$(echo "$f" | grep "\--[a-z]")
    done
    #opt=$(echo "$*"| perl -pi -e "s|$fajl||")
    dbg "STATUS: $(echo "$INPUT" | grep /)"
    if [ ! -n "$(echo "$INPUT" | grep /)" ];then
	p=$@
	dbg "SU - -c $(basename $0) $p"
	su - -c "$(basename $0) $p"
    else
        dbg "SU $(basename $0) ${opt}${path}${fajl}"
	su - -c "$(basename $0) ${opt}${path}${fajl}"
    fi
    exit
fi

pack=`echo $sel | sed -e 's|.noarch|@noarch|' -e 's|.i586|@i586|'`
echo -e "${GRN} * $installmsg ${DEF}"

if [ "$packbin" = urpmi ];then
    dbg "command: urpmi $INPUT"
    echo -en "${YEL} * " 
    /usr/sbin/urpmi $INPUT
    [ "$?" = "255" ]&&echo -en "${RED} * $enginefail ${DEF}"
    echo -e "${DEF}"
elif [ "$packbin" = smart ];then
    dbg "command: smart install $INPUT"
    echo -en "${YEL} * " 
    echo "SMART install $(echo $INPUT|sed 's|--auto|--quiet|')"
	smart install $(echo $INPUT|sed 's|--auto|--quiet|')
    ret=$?
    echo -en "${DEF}"
elif [ "$packbin" = yum ];then
    dbg "command: yum install $INPUT"
    echo -en "${YEL} * " 
         yum install $INPUT
    ret=$?
    echo -en "${DEF}"
elif [ "$packbin" = apt-rpm ];then
    dbg "command: apt-rpm $INPUT"
    echo -en "${YEL} * " 
         apt-rpm $INPUT
    ret=$?
    echo -en "${DEF}"
elif [ "$packbin" = rpm ];then
    dbg "command: rpm -Uvh $INPUT"
    echo -en "${YEL} * " 
         rpm -Uvh $INPUT
    ret=$?
    echo -en "${DEF}"
elif [ "$packbin" = gurpmi ];then
    dbg "command: gurpmi $INPUT"
    echo -en "${YEL} * " 
         gurpmi $INPUT
    ret=$?
    echo -en "${DEF}"
elif [ "$packbin" = gurpmi2 ];then
    dbg "command: gurpmi2 $INPUT"
    echo -en "${YEL} * " 
         gurpmi2 $INPUT
    ret=$?
    echo -en "${DEF}"
elif [ "$aengine" = NOENGINE -o "$packbin" = NOENGINE -o "$packbin" = "" ];then
	echo -en "${RED} * $enginerror ${DEF}"
	echo ""
    
fi
    #echo "$ret"
dbg "Command end."
