#!/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 old daughter :)
export PATH=$PATH:/usr/sbin:/sbin

SETCOLOR_FAIL="echo -en \\033[1;31m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
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=`

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


case $LANG in 
	hu*) 
	notroot=$notroot_hu
	removemsg=$removemsg_hu
	rpmimputerr=$rpmimputerr_hu
	enginerror=$enginerror_hu
	header=$header_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
	"
	;;
	*) 
	notroot=$notroot_en
	removemsg=$removemsg_en
	rpmimputerr=$rpmimputerr_en
	enginerror=$enginerror_en
	header=$header_eh
	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  : $0 packagename (or packagename-version-release)
	
	${GRN}or (for advanced users)
	${CYN}Advanced install example: $0 packagename --engine=rpm
	
	${GRN}or (for developers)
	${CYN}Extended install example: $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

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

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

echo -e " 
${MAG} $header ${DEF}
"
aengine=/usr/sbin/urpme
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=`echo "$FORCEENGINE" | sed -e 's|.*--engine=||' -e 's|.*engine=||'`
    packbin="$engine"
    dbg "Forced packager binary: $packbin"
fi

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

[ -n "$local" ]&& dbg "Local Remove Format Not Correct, fixating....\n"
[ -n "$local" ]&& INPUT=`echo $INPUT| sed -e 's|.noarch.rpm||' -e 's|.i586.rpm||'`

dbg "$INPUT"
echo -e "${GRN} * $removemsg ${DEF}"

if [ "$packbin" = urpme ];then
    dbg "command: urpme $INPUT"
    echo -en "${YEL} * " 
        /usr/sbin/urpme "$INPUT"
elif [ "$packbin" = smart ];then
    dbg "command: smart remove $INPUT"
        smart remove "$INPUT"
elif [ "$packbin" = yum ];then
    dbg "command: yum remove $INPUT"
         yum remove "$INPUT"
elif [ "$packbin" = apt-rpm ];then
    dbg "command: apt-rpm $INPUT"
         apt-get "$INPUT"
elif [ "$packbin" = rpm ];then
    dbg "command: rpm -Uvh $INPUT"
         rpm -e "$INPUT"
elif [ "$aengine" = NOENGINE ];then
	echo "Error 1000! Installer engine not found. Please install one and apply command again!\n
	Available engines for blackPanther OS: rpm, urpmi, smart, yum, apt-rpm"
        exit
fi

echo ""

