#!/bin/sh

INSTALL_XICQ=0
INSTALL_KXICQ=0
XICQ_DIR=/usr/local/bin

clear
echo " Xtrophy ICQ"

dialog --backtitle "Xtrophy ICQ" \
	--title "Readme" \
	--textbox "README" 20 76

dialog --backtitle "Xtrophy ICQ" \
	--title "License" \
	--textbox "LICENSE" 20 76

dialog --backtitle "Xtrophy ICQ" \
	--title "Install Xicq?" \
	--yesno "NOTICE: By selecting <YES> you agree to the license you have just been shown." 6 45
if [ $? = 0 ]
then
	dialog --backtitle "Xtrophy ICQ" \
		--checklist "Which client(s) would you like to install" 10 50 2 Xicq "text interface" off KXicq "Graphic interface" off 2>/tmp/$$
	if [ $? = 0 ]
	then
		read WHICH < /tmp/$$

		if [ "$WHICH" = "\"Xicq\"" ]
		then
			INSTALL_XICQ=1

			dialog --backtitle "Xtrophy ICQ" \
				--title "Where do you want Xicq?" \
				--inputbox "default is $XICQ_DIR" 8 60 $XICQ_DIR 2>/tmp/$$
			if [ $? = 0 ]
			then
				read XICQ_DIR < /tmp/$$
			fi
		fi

		if [ "$WHICH" = "\"KXicq\"" ]
		then
			INSTALL_KXICQ=1
		fi

		if [ "$WHICH" = "\"Xicq\" \"KXicq\"" ]
		then
			INSTALL_XICQ=1
			INSTALL_KXICQ=1

			INSTALL_XICQ=1

			XICQ_DIR=/usr/local/bin
			dialog --backtitle "Xtrophy ICQ" \
				--title "Where do you want Xicq?" \
				--inputbox "default is $XICQ_DIR" 8 60 $XICQ_DIR 2>/tmp/$$
			if [ $? = 0 ]
			then
				read XICQ_DIR < /tmp/$$
			fi
		fi

		if [ "$WHICH" = "" ]
		then
			dialog --backtitle "Xtrophy ICQ" \
				--msgbox "You didn't select any client! Installation aborted!" 6 37
			exit 1
		fi

		LIBDONE=0

		if [ $INSTALL_XICQ = 1 ]
		then

			LIBDONE=1
			make lib
		        if [ $? != 0 ]
			then
				dialog --backtitle "Xtrophy ICQ" \
					--title "ERROR" \
					--msgbox "An error occured while compiling libXicq." 5 45
				clear
				exit 1;
			fi

			make xicq
		        if [ $? != 0 ]
			then
				dialog --backtitle "Xtrophy ICQ" \
					--title "ERROR" \
					--msgbox "An error occured while compiling Xicq." 5 42
				clear
				exit 1;
			fi

			cp -f src/xicq/main $XICQ_DIR/xicq
		        if [ $? != 0 ]
			then
				dialog --backtitle "Xtrophy ICQ" \
					--title "ERROR" \
					--msgbox "Could not copy Xicq!" 5 24
				clear
				exit 1;
			fi
		fi

		if [ $INSTALL_KXICQ = 1 ]
		then
			if [ $LIBDONE = 0 ]
			then
				make lib
			        if [ $? != 0 ]
				then
					dialog --backtitle "Xtrophy ICQ" \
						--title "ERROR" \
						--msgbox "An error occured while compiling libXicq." 5 45
					clear
					exit 1;
				fi
			fi

			cd src/kxicq
			./configure
		        if [ $? != 0 ]
			then
				dialog --backtitle "Xtrophy ICQ" \
					--title "ERROR" \
					--msgbox "An error occured while configuring KXicq." 5 45
				clear
				exit 1;
			fi
			
			make
		        if [ $? != 0 ]
			then
				dialog --backtitle "Xtrophy ICQ" \
					--title "ERROR" \
					--msgbox "An error occured while compiling KXicq." 5 43
				clear
				exit 1;
			fi

			make install
		        if [ $? != 0 ]
			then
				dialog --backtitle "Xtrophy ICQ" \
					--title "ERROR" \
					--msgbox "Could not copy KXicq!" 5 25
				clear
				exit 1;
			fi
			kwmcom kpanel:restart
			cd ../..
		fi

		mkdir $HOME/.xicq
		cp -i contact $HOME/.xicq/
		cp -i config $HOME/.xicq/
		dialog --backtitle "Xtrophy ICQ" \
			--title "Configuration" \
			--msgbox "Please edit ~/.xicq/config and ~/.xicq/contact to fit your needs" 6 42

		dialog --backtitle "Xtrophy ICQ" \
			--msgbox "Installation complete." 5 26
		exit 0

		
	else
		dialog --backtitle "Xtrophy ICQ" \
			--msgbox "Installation aborted!" 5 25
		exit 1
	fi
else
	dialog --backtitle "Xtrophy ICQ" \
		--msgbox "Installation aborted!" 5 25
fi
exit 1
