#!/bin/sh

# Here I will define the needed functions to get all the wanted data

fkt_get_c_compiler()
{
	LGUESS=$1

	which $LGUESS >/dev/null
	if [ $? = 0 ]
	then
		echo $LGUESS
	else
		which  gcc > /dev/null
		if [ $? = 0 ]
		then
			echo gcc
		else
			which cc > /dev/null
			if [ $? = 0 ]
			then
				echo cc
			else
				which xlc > /dev/null
				if [ $? = 0 ]
				then
					echo xlc
				else
					echo unknown
				fi
			fi
		fi
	fi
}

fkt_get_cpp_compiler()
{
	LGUESS=$1

	which $LGUESS > /dev/null
	if [ $? = 0 ]
	then
		echo $LGUESS
	else
		which g++ > /dev/null
		if [ $? = 0 ]
		then
			echo g++
		else
			which xlC > /dev/null
			if [ $? = 0 ]
			then
				echo xlC
			else
				echo unknown
			fi
		fi
	fi
}

fkt_get_c_flags_for_cc()
{
	LFLAGS=$@

	if [ "$C_COMPILER" = "gcc" ]
	then
		echo $LFLAGS
	else
		echo no_gcc
	fi
}

fkt_get_cpp_flags_for_cpp()
{
	LFLAGS=$@

	if [ "$CPP_COMPILER" = "g++" ]
	then
		echo $LFLAGS
	else
		echo no_g++
	fi
}

fkt_get_qt_dir()
{
	if [ "$QTDIR" = "" ]
	then
		if [ -d /usr/lib/qt ]
		then
			echo /usr/lib/qt
		else
			if [ -d /usr/local/qt ]
			then
				echo /usr/local/qt
			else
				echo unknown
			fi
		fi
	else
		echo $QTDIR
	fi
}

fkt_get_kde_dir()
{
	if [ "$KDEDIR" = "" ]
	then
		if [ -d /opt/kde ]
		then
			echo /opt/kde
		else
			if [ -d /usr/local/kde ]
			then
				echo /usr/local/kde
			else
				echo unknown
			fi
		fi
	else
		echo $KDEDIR
	fi
}

fkt_get_x11_dir()
{
	LGUESS=$1

	if [ -d "$LGUESS" ]
	then
		echo $LGUESS
	else
		if [ -d /usr/X11R6 ]
		then
			echo /usr/X11R6
		else
			if [ -d /usr/lpp/X11/lib/R6 ]
			then
				echo /usr/lpp/X11/lib/R6
			else
				echo unknown
			fi
		fi
	fi
}

fkt_get_extra_libs()
{
	LGUESS=$@

	if [ -f /usr/lib/libdl.a ]
	then
		LGUESS=$LGUESS" -ldl"
	fi

	echo $LGUESS
}

fkt_get_os()
{
	SYSTEM=`uname -s`

	if [ "$SYSTEM" = "Linux" ]
	then
		echo Linux
	else
		if [ "$SYSTEM" = "AIX" ]
		then
			echo AIX
		else
			echo unknown
		fi
	fi
}

fkt_get_ld_type()
{
	LDTYPE=`ld -v 2>/dev/null|awk '{ print $1 }'`
	
	if [ "$LDTYPE" = "GNU" ]
	then
		echo GNU
	else
		echo unknown
	fi
}

fkt_is_msgfmt_usable()
{
	which msgfmt >/dev/null
	if [ $? = 0 ]
	then
		OOPTION=`msgfmt --help|awk '/-o/ { gsub(",","",$1); print $1 }'`
		if [ "$OOPTION" = "-o" ]
		then
			echo yes
		else
			echo no
		fi
	else
		echo not_found
	fi
}


echo ----------------------------------------------------------------------------
echo                             Configuration of KOra
echo ----------------------------------------------------------------------------

echo

echo Now I will try to find out something about your system. If I can\'t find out the
echo needed data I will ask you.
echo
echo If I present a guess you can change it or accept the guess with the RETURN key
echo If I ask for yes or no answer with this words. Not with shortcuts.

echo

#########################################################
# Find the system name on which you are running configure
#########################################################
OS=`fkt_get_os`
echo Your operating system is $OS

#########################################################
# Specify special define for the compiler depending on OS
#########################################################
if [ "$OS" = "Linux" ]
then
	echo OS_DEFINE = -DOS_LINUX > config.choice
else
	if [ "$OS" = "AIX" ]
	then
		echo OS_DEFINE = -DOS_AIX > config.choice
	else
		echo OS_DEFINE = -DOS_GENERIC > config.choice
	fi
fi

#################################
# Find a C compiler in the system
#################################
GUESS=`awk -F. '/^CC.=/ { print $3 }' config.guess`
CHOICE=`fkt_get_c_compiler $GUESS`
if [ "$CHOICE" = "unknown" ]
then
	echo I cannot find a C compiler for your system. Tell me your compiler
	echo Type in the name of your compiler
	read ANSWER
	if [ "$ANSWER" = "" ]
	then
		echo Without compiler I cannot do anything. Good bye
		exit
	fi
	echo
else
	ANSWER=$CHOICE
fi
echo CC = $ANSWER >> config.choice
echo The C Compiler is set to $ANSWER
C_COMPILER=$ANSWER
export C_COMPILER


####################################
# Find a C++ compiler in your system
####################################
GUESS=`awk -F. '/^CXX.=/ { print $3 }' config.guess`
CHOICE=`fkt_get_cpp_compiler $GUESS`
if [ "$CHOICE" = "unknown" ]
then
	echo I cannot find a C++ compiler for your system. Tell me your compiler
	echo Type in the name of your compiler
	read ANSWER
	if [ "$ANSWER" = "" ]
	then
		echo Without compiler I cannot do anything. Good bye
		exit
	fi
	echo
else
	ANSWER=$CHOICE
fi
echo CXX = $ANSWER >> config.choice
echo The C++ Compiler is set to $ANSWER
CPP_COMPILER=$ANSWER
export CPP_COMPILER


#######################################
# Determine the C flags that are to set
#######################################
GUESS=`awk -F. '/^CFLAGS.=/ { print $3 }' config.guess`
FLAGS=`fkt_get_c_flags_for_cc $GUESS`
if [ "$FLAGS" = "no_gcc" ]
then
	echo You are not using the gcc C compiler. So tell me your C flags
	echo For the gcc I wanted to use the following flags
	echo $GUESS
	echo
	echo Which flags do you want to use ?
	read ANSWER
	echo
else
	ANSWER=$FLAGS
fi
echo CFLAGS = $ANSWER >> config.choice
echo C Compilerflags are set to $ANSWER


#########################################
# Determine the C++ flags that are to set
#########################################
GUESS=`awk -F. '/^CXXFLAGS.=/ { print $3 }' config.guess`
FLAGS=`fkt_get_cpp_flags_for_cpp $GUESS`
if [ "$FLAGS" = "no_g++" ]
then
	echo You are not using the g++ C++ compiler. So tell me your C++ flags
	echo For the g++ I wanted to use the following flags
	echo $GUESS
	echo
	echo Which flags do you want to use ?
	read ANSWER
	echo
else
	ANSWER=$FLAGS
fi
echo CXXFLAGS = $ANSWER >> config.choice
echo The C++ Compilerflags are set to $ANSWER


#############################################
# Is the directory for the Qt library known ?
#############################################
ANSWER=`fkt_get_qt_dir`
if [ "$ANSWER" = "unknown" ]
then
	echo I cannot find your Qt directory variable QTDIR
	echo My guess is
	CHOICE=`awk -F. '/^QTDIR.=/ { print $3 }' config.guess`
	echo $CHOICE
	echo What is your choice [$CHOICE]?
	read ANSWER
	echo
	if [ "$ANSWER" = "" ]
	then
		ANSWER=$CHOICE
	fi
fi
echo QTDIR = $ANSWER >> config.choice
echo The Qt directory is $ANSWER



################################################
# Is the directory for the KDE libraries known ?
################################################
ANSWER=`fkt_get_kde_dir`
if [ "$ANSWER" = "unknown" ]
then
	echo I cannot find your KDE directory variable KDEDIR
	echo My guess for the variable is:
	CHOICE=`awk -F. '/^KDEDIR.=/ { print $3 }' config.guess`
	echo $CHOICE
	echo What is your choice [$CHOICE]?
	read ANSWER
	echo
	if [ "$ANSWER" = "" ]
	then
		ANSWER=$CHOICE
	fi
fi
echo KDEDIR = $ANSWER >> config.choice
echo The KDE directory is $ANSWER


#################################################################################
# Set silently the include path for the compiler. That should be allways the same
#################################################################################
ANSWER=`awk -F. '/^INCPATH.=/ { print $3 }' config.guess`
echo INCPATH = $ANSWER >> config.choice
echo The compiler include path is set to $ANSWER


################################################
# Set silently the C++ compiler to use as linker
################################################
ANSWER=$CPP_COMPILER
echo LINK = $ANSWER >> config.choice
echo The program will be linked with $ANSWER


####################
# Set the link flags
####################
LD_TYPE=`fkt_get_ld_type`
if [ ! "$LD_TYPE" = "GNU" ]
then
	echo You are not running GNU ld
	if [ "$OS" = "AIX" -a "$CPP_COMPILER" = "g++" ]
	then
		echo You are running a native ld with the GNU C++ compiler
		ANSWER=-Wl,-bbigtoc
	else
		echo You are running a native ld with a native C++ Compiler
		echo Some systems need linker flags now. On Linux no link flag is needed. But AIX
		echo for example needs the flag -bbigtoc because the TOC is very big.
		CHOICE=`awk -F. '/^LFLAGS.=/ {print $3 }' config.guess`
		echo What are the linker flags you want to set ?
		if [ "$CHOICE" = "---" ]
		then
			echo I think you will leave the flags unset. But you can enter some flags if you want.
			CHOICE=
		else
			echo My guess is:
			echo $CHOICE
		fi
		echo What is your choice [$CHOICE]?
		read ANSWER
		echo
		if [ "$ANSWER" = "" ]
		then
			ANSWER=$CHOICE
		fi
	fi
else
	echo You are running GNU ld
	ANSWER=
fi
echo LFLAGS = $ANSWER >> config.choice
echo Here are the used linker flags [if any]: $ANSWER


#############################################
# find the directory for the X11 installation
#############################################
GUESS=`awk -F. '/^X11DIR.=/ { print $3 }' config.guess`
CHOICE=`fkt_get_x11_dir $GUESS`
if [ "$CHOICE" = "unknown" ]
then
	echo I cannot find the path of your X11 installation.
	echo Please tell me the location:
	read ANSWER
	echo
else
	ANSWER=$CHOICE
fi
echo X11DIR = $ANSWER >> config.choice
echo This is the X11 directory: $ANSWER


##############################################################################################
# because we allready know about all the needed pathes we will set the library pathes silently
##############################################################################################
ANSWER=`awk -F. '/^LIBPATH.=/ { print $3 }' config.guess`
echo LIBPATH = $ANSWER >> config.choice
echo This is the Library path: $ANSWER


#########################################################################
# because we know about all the libs we want to link we set them silently
#########################################################################
GUESS=`awk -F. '/^LIBS.=/ { print $3 }' config.guess`
ANSWER=`fkt_get_extra_libs $GUESS`
echo LIBS = $ANSWER >> config.choice
echo This are the libraries that will be linked: $ANSWER


###################################################
# moc is moc. They won't rename it. Set it silently
###################################################
ANSWER=`awk -F. '/^MOC.=/ { print $3 }' config.guess`
echo MOC = $ANSWER >> config.choice
echo The Qt Meta Object Compiler is: $ANSWER


#################################################################
# Ok, now I will get the installation directory from config.guess
#################################################################
CHOICE=`awk -F. '/^INSTALLDIR.=/ { print $3 }' config.guess`
ANSWER=$CHOICE
echo INSTALLDIR = $ANSWER >> config.choice
echo The installation directory prefix for KOra is $ANSWER

#############################################################
# Now we want to find out if we have a msgfmt that we can use
#############################################################
ANSWER=`fkt_is_msgfmt_usable`
if [ "$ANSWER" = "yes" ]
then
	echo Multilanguage support can be generated
	echo GMSGFMT = msgfmt >> config.choice
else
	if [ "$ANSWER" = "not_found" ]
	then
		echo
		echo -------------------
		echo MULTILANGUAGE ERROR
		echo -------------------
		echo For multilanguage support we need the tool
		echo msgfmt. This tool is not found on your system.
		echo Get at least version 0.10.32 of the GNU gettext
		echo package and install it on your system.
		echo We will continue without multilanguage support.
		echo GMSGFMT = @echo msgfmt not installed. Wanted flags: >> config.choice
	else
		echo
		echo -------------------
		echo MULTILANGUAGE ERROR
		echo -------------------
		echo For multilanguage support we need the tool
		echo msgfmt. You have it installed. But your msgfmt
		echo does not support the flag -o.
		echo Get at least version 0.10.32 of the GNU gettext
		echo package and install it on your system.
		echo We will continue without multilanguage support.
		echo GMSGFMT = @echo msgfmt does not support this flags: >> config.choice
	fi
fi



echo We made it !!!
echo The file config.choice is now created.
echo Now you can execute the following commands:
echo make
echo make install
echo
echo If you have problems with KOra you can mail me.
echo My address is Ullrich.Wagner@gmx.de

echo

echo Have fun.
exit
