#!/bin/bash

#Default stuff I don't want to have to change everywhere
PYXML="python-xml"
PYTREE="python-elementtree"
PYRPM="rpm-python"
SMART="smart"
KSTRAY="smart-ksmarttray"
INSPKG="/usr/bin/install-package"
IDIR=`kde-config --prefix`
SERVICEMENU="$IDIR/share/apps/konqueror/servicemenus/smart_konqi.desktop"

#Find out our architecture and set repository as required
if [ `uname -m` = "x86_64" ] ; then
	REPOSITORY="ftp://ftp-1.gwdg.de/pub/opensuse/distribution/10.2/repo/oss/suse/x86_64"
	REPOSMART="http://ftp.skynet.be/pub/suser-guru/smart/10.2/x86_64"
else
	REPOSITORY="ftp://ftp-1.gwdg.de/pub/opensuse/distribution/10.2/repo/oss/suse/i586"
	REPOSMART="http://ftp.skynet.be/pub/suser-guru/smart/10.2/i586"
fi

#Get some required packages
if rpm -q $PYRPM > /dev/null ; then
	echo -e '\E[32m' "*rpm-python is already installed"; tput sgr0
else
	echo -e '\E[33m' "*installing rpm-python"; tput sgr0
	rpm -ihv $REPOSITORY/rpm-python-*.rpm
fi

if rpm -q $PYXML > /dev/null ; then
	echo -e '\E[32m' "*python-xml is already installed"; tput sgr0
else
	echo -e '\E[33m' "*installing python-xml"; tput sgr0
	rpm -ihv $REPOSITORY/python-xml-*.rpm
fi

if rpm -q $PYTREE > /dev/null ; then
	echo -e '\E[32m' "*python-elementtree is already installed"; tput sgr0
else
	echo -e '\E[33m' "*installing python-elementtree"; tput sgr0
	rpm -ihv $REPOSITORY/python-elementtree-*.rpm
fi

#Alright.. now we get to the actual meat of the deal
#install smart... after checking to see if it is already there
if rpm -q $SMART > /dev/null ; then
	echo -e '\E[31m' "SMART SEEMS TO ALREADY BE INSTALLED"; tput sgr0
	echo -e '\033[1m' "Do you wish to continue with the configuration (y/N)?"; tput sgr0
	read ANSWER

	#Process the answer we got from the user
	if [ $ANSWER != "y" ] ; then
		if [ $ANSWER != "Y" ] ; then
			exit 0
		fi
	fi
	
else
	echo -e '\E[33m' "*installing smart"; tput sgr0
	rpm -ihv $REPOSMART/smart-latest.rpm
fi

echo -e '\E[33m' "*starting smart update"; tput sgr0
smart update

#Nice easy to sue python gui for installing programs.
echo -e '\E[33m' "*installing smart-gui"; tput sgr0
rpm --import http://linux01.gwdg.de/~pbleser/guru-rpm.asc
smart install smart-gui

#Package update monitor for the kde taskbar
if rpm -q $KSTRAY > /dev/null ; then
	echo -e '\E[32m' "*ksmarttray is already installed"; tput sgr0
else
	echo -e '\E[33m' "*installing smart-ksmarttray"; tput sgr0
	smart install smart-ksmarttray
fi

#Bash script to be used by the service menu
echo -e '\E[33m' "*installing install-package shortcut"; tput sgr0
#install-package is a script to install rpms via smart...
rm -f $INSPKG
echo '#!/bin/bash' > $INSPKG
echo 'kdesu "smart --gui $2 $3 $4 $5 $6 $7 $8 $9 install \"$1\""' >> $INSPKG

chmod 755 $INSPKG

#Right Click service menu in konqueror for installing RPMS
echo -e '\E[33m' "*installing desktop service menu"; tput sgr0
rm -f $SERVICEMENU
echo "[Desktop Entry]" > $SERVICEMENU
echo "ServiceTypes=application/x-rpm" >> $SERVICEMENU
echo "Actions=install" >> $SERVICEMENU
echo "" >> $SERVICEMENU
echo "[Desktop Action install]" >> $SERVICEMENU
echo "Name=Install Package" >> $SERVICEMENU
echo "Icon=smart" >> $SERVICEMENU
echo "Exec=install-package %U" >> $SERVICEMENU

echo -e '\E[32m' "!!! INSTALLATION COMPLETE !!!"; tput sgr0
echo -e '\E[32m' "Make sure you run ksmarttray for any users"; tput sgr0
echo -e '\E[32m' "who you want to manage package updates."; tput sgr0
echo -e '\E[32m' "In KDE simply click Alt-F2 and type ksmarttray"; tput sgr0

