#!/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

case $LANG in 
	hu*) 
	notroot=$notroot_hu
	installmsg=$installmsg_hu
	rpmimputerr=$rpmimputerr_hu
	enginerror=$enginerror_hu
	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=$notroot_en
	installmsg=$installmsg_en
	rpmimputerr=$rpmimputerr_en
	enginerror=$enginerror_en
	header=$header_en
	chkdbstatus=$chkdbstatus_en
	chkdbagain=$chkdbagain_en
	chknetstatus=$chknetstatus_en
	accessyes=$accessyes_en
	databasetimeout=$databasetimeout_en
	dbwaito=$dbwaito_en	
	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
	/The feature thaks to 10 years old my daugther/
	$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 " * $chkdbstatus "
    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 
    echo -e "${DEF}"
elif [ "$packbin" = smart ];then
    dbg "command: smart install $INPUT"
    echo -en "${YEL} * " 
        smart install $INPUT
    echo -en "${DEF}"
elif [ "$packbin" = yum ];then
    dbg "command: yum install $INPUT"
    echo -en "${YEL} * " 
         yum install $INPUT
    echo -en "${DEF}"
elif [ "$packbin" = apt-rpm ];then
    dbg "command: apt-rpm $INPUT"
    echo -en "${YEL} * " 
         apt-rpm $INPUT
    echo -en "${DEF}"
elif [ "$packbin" = rpm ];then
    dbg "command: rpm -Uvh $INPUT"
    echo -en "${YEL} * " 
         rpm -Uvh $INPUT
    echo -en "${DEF}"
elif [ "$packbin" = gurpmi ];then
    dbg "command: gurpmi $INPUT"
    echo -en "${YEL} * " 
         gurpmi $INPUT
    echo -en "${DEF}"
elif [ "$packbin" = gurpmi2 ];then
    dbg "command: gurpmi2 $INPUT"
    echo -en "${YEL} * " 
         gurpmi2 $INPUT
    echo -en "${DEF}"
elif [ "$aengine" = NOENGINE -o "$packbin" = NOENGINE -o "$packbin" = "" ];then
	echo -en "${RED} * $enginerror ${DEF}"
	echo ""
    
fi
    echo ""
dbg "Command end."
