#!/bin/bash
#################################################################
# Application Setup Script v0.1.3   (c) 2002 Kadu Team          #
# This program is free software. You can redistribute it and/or #
# modify it under the terms of the GNU General Public License   #
#################################################################


#   Definicja domyslnych wartosci dla zmiennych

APP_NAME=KADU
APP_DEF_DIR=/usr
# $1 - nazwy katalogw, $2 - Wartosci domy?lne

#  Deklaracja funkcji

function AskForPath()
{
	echo -n "Enter new path to $1 : "
	read -e
	if [ "$REPLY" == "" ]; then
		REPLY="$2"
	fi;
}

# $1 - path, $2 - chek file, $3 - correctly  string, $4 - not correctly string
function CheckPath()
{
	if [ -e $1/$2 ]; then
		CORRECT="$3"
	else
		CORRECT="$4"
	fi;
}

function Konfiguruj()
{
	QTDIR="$QT_DIR"
	LD_LIBRARY_PATH="$QT_DIR/lib:$LD_LIBRARY_PATH"
	export QTDIR
	KDEDIR="$KDE_DIR"
	export KDEDIR
	OPER="Configure"
	./configure "--prefix=$APP_DIR"
	WYK=$?
 if test "$WYK" -ne 0; then
        Error
	exit
 fi;
}

function Konfiguruj_bez_libgadu()
{
	QTDIR="$QT_DIR"
	LD_LIBRARY_PATH="$QT_DIR/lib:$LD_LIBRARY_PATH"
	export QTDIR
	KDEDIR="$KDE_DIR"
	export KDEDIR
	OPER="Configure"
	./configure "--prefix=$APP_DIR" "--with-existing-libgadu" 
	WYK=$?
 if test "$WYK" -ne 0; then
        Error
	exit
 fi;
}

function Konfiguruj_bez_ssl()
{
	QTDIR="$QT_DIR"
	LD_LIBRARY_PATH="$QT_DIR/lib:$LD_LIBRARY_PATH"
	export QTDIR
	KDEDIR="$KDE_DIR"
	export KDEDIR
	OPER="Configure"
	./configure "--prefix=$APP_DIR" "--without-openssl" 
	WYK=$?
 if test "$WYK" -ne 0; then
        Error
	exit
 fi;
}

function Konfiguruj_z_debug()
{
	QTDIR="$QT_DIR"
	LD_LIBRARY_PATH="$QT_DIR/lib:$LD_LIBRARY_PATH"
	export QTDIR
	KDEDIR="$KDE_DIR"
	export KDEDIR
	OPER="Configure"
	./configure "--prefix=$APP_DIR" "--with-debug" 
	WYK=$?
 if test "$WYK" -ne 0; then
        Error
	exit
 fi;
}

function Kompiluj()
{
	OPER="Kompiluj"
	make
	WYK=$?
if test "$WYK" -ne 0; then
        Error
	exit
 fi;
}

function Zainstaluj()
{	OPER="Zainstaluj"
	make install
	WYK=$?
 if test "$WYK" ;then
        Gratulacje
        exit
        else
        Error
        exit
 fi;
}

function Odinstaluj()
{
	OPER="Odinstaluj"
	make uninstall
	WYK=$?
 if test "$WYK" ;then
        Deinstal
        exit
        else
        Error
        exit
 fi;
}

function Gratulacje()
{
	clear
	echo " ============================================================================="
	echo " 		Congratulatin $APP_NAME is correctly instaled in directory: $APP_DIR"
	echo " ============================================================================="
}

function Deinstal()
{
	clear
	echo " ============================================================================="
	echo " 		$APP_NAME is correctly removed of directory $APP_DIR"
	echo " ============================================================================="
}

function Aktualizuj_ldsoconf()
{
GREP_RESULT=`grep $LIB_DIR /etc/ld.so.conf`
if [ -z "$GREP_RESULT" ]; then
	echo >> /etc/ld.so.conf
	echo $LIB_DIR >> /etc/ld.so.conf
	/sbin/ldconfig
	echo "Plik ld.so.conf zosta? zaktualizowany"
fi
}

function Error()
{
	echo -e "\a"
	echo " ============================================================================="
	echo " 		  ERROR operacion $OPER not correctly finished "
	echo " ============================================================================="
}
function ShowInfo()
{
 	clear
        echo
	echo " =============================================================================="
	echo "                            PROGRAM USES THIS PATH:"
	CheckPath "$QT_DIR" include/qapplication.h FOUND. "NOT FOUND!"
	echo "     * Library QT version >=3.0.1, in directory: [$QT_DIR], $CORRECT"
	CheckPath "$KDE_DIR" include/kapp.h FOUND. "NOT FOUND!"
	echo "     * KDE Desktop version >=3.0.0, in directory: [$KDE_DIR], $CORRECT"
	echo "     KDE IS NOT REQUIRED  FOR INSTALLATION IN ODHER MENAGERS"
	CheckPath "$APP_DIR" bin/kadu "INSTALLED" "NOT INSTALLED"
	echo "     * Application $APP_NAME will be installed at directory: [$APP_DIR], $CORRECT"
	echo
	echo " =============================================================================="
	echo " |                                   MENU                                     |"
	echo " =============================================================================="
	echo " |                Press   L   to view GNU licence.                            |"
	echo " |                Press   Q   to change QT Library path.                      |"
	echo " |                Press   K   to change KDE Desktop path.                     |"
	echo " |                Press   D   to change destination path.                     |"
	echo " |                Press   I   to install Kadu.                                |"
	echo " |                Press   U   to uninstall Kadu.                              |"
	echo " |                Press   B   to install Kadu with existing libgadu           |"
	echo " |                Press   S   to install Kadu without SSL                     |"
	echo " |                Press   G   to install Kadu with Debug                      |"
	echo " |                Press   P   to Help in Links program.                       |"
	echo " |                Press   E   to exit.                                        |"
	echo " =============================================================================="
        echo "	                 WARNING!!! You have must root access."
}

function Szuk_kadu
{
APP_DIR=`find $HOME -path */bin/kadu -type f | sed -e 's/\/bin\/kadu$//' -e q`
if [ "$APP_DIR" == "" ]; then
APP_DIR=`find /usr -path */bin/kadu -type f | sed -e 's/\/bin\/kadu$//' -e q`
fi;
if [ "$APP_DIR" == "" ]; then
APP_DIR=`find /opt -path */bin/kadu -type f | sed -e 's/\/bin\/kadu$//' -e q`
fi;
if [ "$APP_DIR" == "" ]; then
APP_DIR="$APP_DEF_DIR"
fi;
}

function Szuk_QT
{
QT_DIR=`find /usr -path */include/qapplication.h -type f | sed -e 's/\/include\/qapplication.h$//' -e q`
if [ "$QT_DIR" == "" ]; then
QT_DIR=`find /opt -path */include/qapplication.h -type f | sed -e 's/\/include\/qapplication.h$//' -e q`
fi;
}

function Szuk_KDE
{
KDE_DIR=`find /usr -path */include/kapp.h -type f | sed -e 's/\/include\/kapp.h$//' -e q`
if [ "$KDE_DIR" == "" ]; then
KDE_DIR=`find /opt -path */include/kapp.h -type f | sed -e 's/\/include\/kapp.h$//' -e q`
fi;
}

#########################################################################################
#                                                       START PROGRAMU                                                                     #
#########################################################################################
clear
	echo
	echo "==============================================================================="
	echo "          Welcome To $APP_NAME  Application Setup Script v0.1.3"
	echo "==============================================================================="
	echo
	echo "  This program is free software; you can redistribute it and/or modify"
	echo "  it under the terms of the GNU General Public License as published by"
	echo "  the Free Software Foundation; either version 2 of the License, or   "
	echo "  (at your option) any later version.                                 "
	echo
	echo "  This script will help You with $APP_NAME configuration, compilation and"
	echo "  installation. $APP_NAME Application requires following units installed:"
	echo
        echo " Configuring setup program please wait .........."
	echo
	echo "Searching for  KADU..."
		Szuk_kadu
	echo "Searching for  QT..."
		Szuk_QT
		Szuk_KDE

########################################################################################
#					SEKCJA WYBORU MENU										#
########################################################################################
while [ 1 ]; do
	ShowInfo
	while [ 1 ]; do
		read -n 1 -s
		if [[ ("$REPLY" == "L") || ("$REPLY" == "l") ]]; then
			clear
			cat COPYING | more
			break
		fi;
		if [[ ("$REPLY" == "Q") || ("$REPLY" == "q") ]]; then
			AskForPath "biblioteki QT"
			QT_DIR="$REPLY"
			break
		fi;
		if [[ ("$REPLY" == "K") || ("$REPLY" == "k") ]]; then
			AskForPath "KDE Desktop"
			KDE_DIR="$REPLY"
			break
		fi;
		if [[ ("$REPLY" == "D") || ("$REPLY" == "d") ]]; then
			AskForPath "$APP_NAME"
			APP_DIR="$REPLY"
                        break
		fi;
		if [[ ("$REPLY" == "I") || ("$REPLY" == "i") ]]; then
			Konfiguruj
			Kompiluj
			Zainstaluj
			Aktualizuj_ldsoconf
		fi;
		if [[ ("$REPLY" == "B") || ("$REPLY" == "b") ]]; then
			Konfiguruj_bez_libgadu
			Kompiluj
			Zainstaluj
		fi;
		if [[ ("$REPLY" == "S") || ("$REPLY" == "s") ]]; then
			Konfiguruj_bez_ssl
			Kompiluj
			Zainstaluj
			Aktualizuj_ldsoconf
		fi;
		if [[ ("$REPLY" == "G") || ("$REPLY" == "g") ]]; then
			Konfiguruj_z_debug
			Kompiluj
			Zainstaluj
			Aktualizuj_ldsoconf
		fi;
		if [[ ("$REPLY" == "U") || ("$REPLY" == "u") ]]; then
			Odinstaluj
		fi;
		if [[ ("$REPLY" == "E") || ("$REPLY" == "e") ]]; then
			exit
		fi;
		if [[ ("$REPLY" == "P") || ("$REPLY" == "p") ]]; then
			links ./doc/index_doc.html
			break
		fi;
	done
done
########################################################################################
#					KONIEC													 #
########################################################################################
