#!/bin/bash

declare -r in_Get=${*};

source "build.bash";

#
# make shure the libraies from the distribution is used and not the one
# installed in /opt/ada
#
unset -v ADA_INCLUDE_PATH
unset -v ADA_OBJECTS_PATH
unset -v ADA_PROJECT_PATH

#
# setup cvs
#
declare -x CVS_RSH=ssh;
declare -x CVSROOT=":ext:anoncvs@savannah.gnu.org:/cvsroot/gcc";

function GNAT_Clean ()
	{
	local -r in_Version=${1};
	local -r in_Config=${2};
	local -r in_Type=${2};

	Make_Directory ../gnu_${in_Version}/${in_Config};
	
	pushd ../gnu_${in_Version}/${in_Config};
		make  ${in_Type}	2>&1 |	tee ../clean.gcc.${in_Config}.log;
		find	-iname "*.ali"			-exec rm -vf  '{}' ';';
		find	-iname "*.o"			-exec rm -vf  '{}' ';';
		find	-iname "config.cache"	-exec rm -vf  '{}' ';';
		rm -v gcc/gnat*
	popd;

	return;
	}

function Configure ()
    {
	local -r in_Version=${1};
	local -r in_Config=${2};

	Make_Directory ../gnu_${in_Version}/${in_Config};

	pushd ../gnu_${in_Version}/${in_Config};
		if [[ ${in_Version} = 3.4 ]] ; then
			Languages=c,ada,c++,f77,java,objc;
		else
			Languages=c,ada,c++,f95,java,objc;
		fi;

		../gcc/configure														\
					--prefix=/opt/ada_${in_Version}								\
					--with-gcc													\
					--with-gnu-ld												\
					--with-gnu-as												\
					--enable-alloca=yes											\
					--enable-mpfr												\
					--enable-libada												\
					--enable-libgcj												\
					--enable-multilib											\
					--enable-java-gc=boehm										\
					--enable-c99												\
					--enable-threads=yes										\
					--enable-interpreter										\
					--enable-hash-synchronization								\
					--enable-shared												\
					--enable-languages=${Languages}								\
		2>&1 |	tee	../configure.${in_Config}.log;

#					--enable-objc-gc=boehm										\
#					--enable-checking											\
#					--enable-bootstrap											\

	popd;

	return ${?};
	}

function Compile ()
    {
	local -r in_Version=${1};
	local -r in_Config=${2};

	pushd ../gnu_${in_Version}/${in_Config};
		make bootstrap	2>&1 |	tee "../compile.bootstrap.${in_Config}.log";

		if (( ${?} == 0 )) ; then
			make	2>&1 |	tee "../compile.gcc.${in_Config}.log";
		fi;
	
		if (( ${?} == 0 )) ; then
			make -C gcc gnatlib_and_tools	2>&1 |	tee "../compile.gnat.${in_Config}.log";
		fi;

		if (( ${?} == 0 )) ; then
			make -C gcc gnatlib-shared	2>&1 |	tee "../compile.gnat.${in_Config}.log";
		fi;

		if (( ${?} == 0 )) ; then
			make info	2>&1 |	tee "../compile.info.${in_Config}.log";
		fi;
	popd;

	return ${?};
	}
	
function Check ()
    {
	local -r in_Version=${1};
	local -r in_Config=${2};

	pushd ../gnu_${in_Version}/${in_Config};
		make -C gcc check-ada	2>&1 |	tee "../check.${in_Config}.log";
	popd;

	return ${?};
	}


function Install ()
    {
	local -r in_Version=${1};
	local -r in_Config=${2};

	pushd ../gnu_${in_Version}/${in_Config};
		make install	2>&1 |	tee "../install.${in_Config}.log";

		local -r New_Version=$(/opt/ada/bin/gcc -dumpversion);
		local -r New_Target=$(/opt/ada/bin/gcc -dumpmachine);

		pushd /opt;
			ln --symbolic --verbose --force --no-dereference "ada_${in_Version}" "ada";
		popd;

		pushd "/opt/ada/lib/gcc/${New_Target}/${New_Version}/adalib";
			ln --symbolic --verbose "libgccprefix.a"	"libgccprefix-${in_Version}.a";
			ln --symbolic --verbose "libgmem.a"			"libgmem-${in_Version}.a";
			ln --symbolic --verbose "libgnarl.a"		"libgnarl-${in_Version}.a";
			ln --symbolic --verbose "libgnat.a"			"libgnat-${in_Version}.a";
		popd;
		
	popd;

	return;
	}

case "$(uname -s -m)" in
	"Linux i686")
		#
		# un-set compiler options.
		#
		unset -v CFLAGS;
		unset -v CPPFLAGS;
		unset -v ADA_CFLAGS;
		unset -v ADAFLAGS;
		Target=$(gcc -dumpmachine);
	;;
	"Linux x86_64")
		#
		# set compiler options. For x86_64 -fPIC is needed.
		#
		declare -x CFLAGS="-fPIC -DPIC";
		declare -x CPPFLAGS="-fPIC -DPIC";
		declare -x ADA_CFLAGS="-fPIC -DPIC";
		declare -x ADAFLAGS="-fPIC -DPIC";
		Target="x86_64-suse-linux";
	;;
	"CYGWIN_NT-5.1 i686")
		#
		# un-set compiler options.
		#
		unset -v CFLAGS;
		unset -v CPPFLAGS;
		unset -v ADA_CFLAGS;
		unset -v ADAFLAGS;
		Target=$(gcc -dumpmachine);
	;;
	"MINGW32_NT-5.1 i686")
		#
		# un-set compiler options.
		#
		unset -v CFLAGS;
		unset -v CPPFLAGS;
		unset -v ADA_CFLAGS;
		unset -v ADAFLAGS;
		Target="i686-pc-mingw32";
	;;
	*)
		#
		# un-set compiler options.
		#
		unset -v CFLAGS;
		unset -v CPPFLAGS;
		unset -v ADA_CFLAGS;
		unset -v ADAFLAGS;
		Target=$(gcc -dumpmachine);
	;;
esac;

if [[ ${in_Get} == *usr* ]] ; then
	#
	# make shure the compiler from the distribution is used and not the one
	# installed in /opt/ada. Usefull fallback:
	#   when 4.0.x is installed and 3.4.x is to be compiled.
	#   when the gcc installations has failed.
	#   when .ali file format changes frequently.
	#
	declare -x CC=/usr/bin/gcc
	declare -x ADA=/usr/bin/gcc
elif [[ ${in_Get} == *opt_3* ]] ; then
	declare -x CC=/opt/ada_3.4/bin/gcc
	declare -x ADA=/opt/ada_3.4/bin/gcc
elif [[ ${in_Get} == *opt_4* ]] ; then
	declare -x CC=/opt/ada_4.0/bin/gcc
	declare -x ADA=/opt/ada_4.0/bin/gcc
fi;

if [[ ${in_Get} == *get* ]] ; then
	if [[ ${in_Get} == *3.4.0* ]] ; then
		Update_From_CVS "../gnu_3.4" "gcc" "-r gcc_3_4_0_release";
	elif [[ ${in_Get} == *3.4.1* ]] ; then
		Update_From_CVS "../gnu_3.4" "gcc" "-r gcc_3_4_1_release";
	elif [[ ${in_Get} == *3.4.2* ]] ; then
		Update_From_CVS "../gnu_3.4" "gcc" "-r gcc_3_4_2_release";
	elif [[ ${in_Get} == *3.4.3* ]] ; then
		Update_From_CVS "../gnu_3.4" "gcc" "-r gcc_3_4_3_release";
	elif [[ ${in_Get} == *3.4* ]] ; then
		Update_From_CVS "../gnu_3.4" "gcc" "-r gcc-3_4-branch";
	fi;
	if [[ ${in_Get} == *4.0.0* ]] ; then
		Update_From_CVS "../gnu_4.0" "gcc" "-r gcc_4_0_0_release";
	elif [[ ${in_Get} == *4.0* ]] ; then
		Update_From_CVS "../gnu_4.0" "gcc" "-r gcc-4_0-branch";
	fi;
	if [[ ${in_Get} == *4.1* ]] ; then
		Update_From_CVS "../gnu_4.1" "gcc" "-A";
	fi;
fi;

if [[ ${in_Get} == *clean* ]] ; then
	if [[ ${in_Get} == *3.4* ]] ; then
		GNAT_Clean "3.4" ${Target} clean;
	fi;
	if [[ ${in_Get} == *4.0* ]] ; then
		GNAT_Clean "4.0" ${Target} clean;
	fi;
	if [[ ${in_Get} == *4.1* ]] ; then
		GNAT_Clean "4.1" ${Target} clean;
	fi;
fi;

if [[ ${in_Get} == *dist* ]] ; then
	if [[ ${in_Get} == *3.4* ]] ; then
		GNAT_Clean "3.4" ${Target} distclean;
	fi;
	if [[ ${in_Get} == *4.0* ]] ; then
		GNAT_Clean "4.0" ${Target} distclean;
	fi;
	if [[ ${in_Get} == *4.1* ]] ; then
		GNAT_Clean "4.1" ${Target} distclean;
	fi;
fi;

if [[ ${in_Get} == *conf* ]] ; then
	if [[ ${in_Get} == *3.4* ]] ; then
		Configure "3.4" ${Target};
	fi;
	if [[ ${in_Get} == *4.0* ]] ; then
		Configure "4.0" ${Target};
	fi;
	if [[ ${in_Get} == *4.1* ]] ; then
		Configure "4.1" ${Target};
	fi;
fi;

if [[ ${in_Get} == *comp* ]] ; then
	if [[ ${in_Get} == *3.4* ]] ; then
		Compile "3.4" ${Target};
	fi;
	if [[ ${in_Get} == *4.0* ]] ; then
		Compile "4.0" ${Target};
	fi;
	if [[ ${in_Get} == *4.1* ]] ; then
		Compile "4.1" ${Target};
	fi;
fi;

if [[ ${in_Get} == *check* ]] ; then
	if [[ ${in_Get} == *3.4* ]] ; then
		Check "3.4" ${Target};
	fi;
	if [[ ${in_Get} == *4.0* ]] ; then
		Check "4.0" ${Target};
	fi;
	if [[ ${in_Get} == *4.1* ]] ; then
		Check "4.1" ${Target};
	fi;
fi;

if [[ ${in_Get} == *inst* ]] ; then
	if [[ ${in_Get} == *3.4* ]] ; then
		Install "3.4" ${Target};
	fi;
	if [[ ${in_Get} == *4.0* ]] ; then
		Install "4.0" ${Target};
	fi;
	if [[ ${in_Get} == *4.1* ]] ; then
		Install "4.1" ${Target};
	fi;
fi;

if [[ -z ${in_Get} ]] ; then
	echo -e '
Build GNU Compiler Collection

build-gcc {\e[32mCompiler List\e[30m} {\e[32mVersion List\e[30m} {\e[32mOption List\e[30m}

    \e[32mVersion List\e[30m:

	New compiler version to be created.

	gnu_\e[31m3.4.\e[34mX\e[30m	Compile Relese Version 3.4.\e[34mX\e[30m
	gnu_\e[31m3.4\e[30m		Compile Latest Version from 3.4 Branch
	gnu_\e[31m4.0\e[30m		Compile Latest Version from HEAD

    \e[32mOption List\e[30m:

	Creation steps to be executed.

	\e[31mget\e[30m		Get new sources from CVS
	\e[31mdist\e[30mclean	Clean old compile for re-configuration
	\e[31mclean\e[30m		Clean old compile
	\e[31mconf\e[30migure	Configure the current target
	\e[31mcomp\e[30mile		Compile	the current target
	\e[31minst\e[30mall		Install

    \e[32mCompiler List\e[30m:

	Old compiler version to be used in creation of the new compiler.

	\e[31musr\e[30m		Use compiler from distribution (/usr/bin/gcc).
	\e[31mopt_3\e[30m.4		Use installed 3.4 compiler (/opt/ada_3.4/gcc).
	\e[31mopt_4\e[30m.0		Use installed 4.0 compiler (/opt/ada_4.0/gcc).

Destination directory is /opt/ada_\e[34mX.X\e[30m with a sybolic link created
to /opt/ada.
Configuration id for current OS and CPU.

'

fi;
