#!/bin/sh

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

fkt_is_oracle_home_set()
{
	if [ "$ORACLE_HOME" = "" ]
	then
		echo no
	else
		echo yes
	fi
}

fkt_is_oracle_installed()
{
	if [ -f $ORACLE/home/bin/svrmgrl ]
	then
		echo yes
	else
		echo unknown
	fi
}

fkt_is_proc_in_path()
{
	which proc >/dev/null
	if [ $? = 0 ]
	then
		echo yes
	else
		echo no
	fi
}

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_oracle_proc()
{
	LGUESS=$1

	which $LGUESS > /dev/null
	if [ $? = 0 ]
	then
		echo $LGUESS
	else
		which proc > /dev/null
		if [ $? = 0 ]
		then
			echo proc
		else
			echo unknown
		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_oracle_libs()
{
	LGUESS=$@

	if [ -f $ORACLE_HOME/lib/libcommon.* ]
	then
		if [ -f $ORACLE_HOME/lib/libnlsrtl3.* ]
		then
			if [ -f $ORACLE_HOME/lib/libcore4.* ]
			then
				echo $LGUESS" -lcommon -lcore4 -lnlsrtl3"
			else
				if [ -f $ORACLE_HOME/lib/libcore3.* ]
				then
					echo $LGUESS" -lcommon -lcore3 -lnlsrtl3"
				else
					echo unknown
				fi
			fi
		else
			echo unknown
		fi
	else
		echo unknown
	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
}



echo ----------------------------------------------------------------------------
echo                             Configuration of KOra SERVER
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 out if the environment variable ORACLE_HOME is set correctly
###################################################################
ANSWER=`fkt_is_oracle_home_set`
if [ ! "$ANSWER" = "yes" ]
then
	echo The Environment variable ORACLE_HOME isn't set
	echo Please set it correctly first. Then try again.
	exit
fi


###############################################
# Find out if ORACLE is installed on the system
###############################################
ANSWER=`fkt_is_oracle_installed`

if [ "$ANSWER" = "unknown" ]
then
	echo Is ORACLE accessable to your system ?
	echo yes or no ?
	read ANSWER
	echo
fi

if [ ! "$ANSWER" = "yes" ]
then
	echo You don\'t have an ORACLE database installed
	echo Without ORACLE you can\'t use this product. Sorry.
	exit
fi



############################################################
# Find out if there is the Pro*C Precompiler within the PATH
############################################################
ANSWER=`fkt_is_proc_in_path`
if [ "$ANSWER" = "unknown" ]
then
	echo Is the ORACLE Pro*C Precompiler accessable to your system ?
	echo yes or no ?
	read ANSWER
	echo
fi
if [ ! "$ANSWER" = "yes" ]
then
	echo The ORACLE Precompiler is not found in your path
	echo Without the Pro*C precompiler you can\'t compile this program. Sorry.
	exit
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


###########################################
# Find the Pro*C precompiler in your system
###########################################
GUESS=`awk -F. '/^PROC.=/ { print $3 }' config.guess`
CHOICE=`fkt_get_oracle_proc $GUESS`
if [ "$CHOICE" = "unknown" ]
then
	echo I cannot find your ORACLE Pro*C precompiler. Tell me the name of the precompiler
	echo Type in the name of your precompiler
	read ANSWER
	if [ "$ANSWER" = "" ]
	then
		echo Without the Oracle Precompiler I cannot do anything. Good bye
		exit
	fi
	echo
else
	ANSWER=$CHOICE
fi
echo PROC = $ANSWER >> config.choice
echo The ORACLE Pro*C precompiler is $ANSWER


#######################################
# 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


#################################################################################
# Set silently the include path for the compiler. That should be allways the same
#################################################################################
ANSWER=`awk -F. '/^INCPATH.=/ { print $3 }' config.guess`
if [ "$ANSWER" = "---" ]
then
	echo INCPATH = >> config.choice
else
	echo INCPATH = $ANSWER >> config.choice
fi
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


##########################
# set the ORACLE libraries
##########################
GUESS=`awk -F. '/^ORALIBS.=/ { print $3 }' config.guess`
CHOICE=`fkt_get_oracle_libs $GUESS`
if [ "$CHOICE" = "unknown" ]
then
	echo I couldn\'t find the expected Oracle libraries
	echo You can use my guess if you created the combined ORACLE library for your system.
	echo Otherwise you have to use all the ORACLE libraries. This may be a litte difficult.
	echo Consult the machine dependend documentation of ORACLE in case of problems.Also
	echo you may read my HOWTO file
	echo What is your choice [$GUESS]?
	read ANSWER
	if [ "$ANSWER" = "" ]
	then
		ANSWER=$GUESS
	fi
else
	ANSWER=$CHOICE
fi
echo ORALIBS = $ANSWER >> config.choice
echo This libraries are now used for ORACLE: $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


#################################################################
# 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




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
