#!/bin/sh
# egnomebuild uses SmallEiffel to compile a system that depends on the eGnome
# class library.
#
# To customise the build for your personal preferences, you may set certain
# variables in $HOME/.egtkbuild.rc.  To customise for a particular
# project, you can use ./.egtkbuild.rc; if this exists it is used instead
# of $HOME/.egtkbuild.rc.
#
# .egnomebuild.rc must be Bourne shell script with execute permissions set
# for the current user.
#
# .egnomebuild.rc should set the following variables, if necessary (the
# default value is shown), which will override the default settings:
#   COMPILE=compile_to_c
#   SE_OPTS="-no_strip -no_style_warning -case_insensitive"
#   DEBUG=-g
#   OPTIMISE=-O
#   EXTERNAL=
#
# It may also set these variables, which will be prepended to the default
# options (therefore do not repeat the default options):
#   CECIL     (default: "-cecil ${EGNOME}/C/se/cecil.se")
#   INCFLAGS  (default: "-I. `gnome-config --cflags gnome gnomeui gnorba gtk`")
#   LIBS      (default: "`gnome-config --libs gnome gnomeui gnorba gtk`")

syntax() {
	echo "Usage: egnomebuild -verbose <root-class-name>"
	exit 1
}

CFLAGS=`gnome-config --cflags glib gnome gnomeui gnorba gtk`
#LIBS=`gnome-config --libs gnome gnomeui gnorba gtk`

found=TRUE
while [ $found = TRUE ]
do
	if [ "$1" = -verbose ]
	then
		shift
		verbose=TRUE
	else
		found=FALSE
	fi
done

root_class=$1

if [ -z "$root_class" -o -n "$2" ]
then
	syntax
fi

if [ "$verbose" = TRUE ]
then
	echo "Current settings:
	SmallEiffel = $SmallEiffel
	EGTK  = $EGTK
"
fi

COMPILE=compile_to_c
DEBUG=-g
OPTIMISE=-O
SE_OPTS="-no_strip -no_style_warning -case_insensitive"
EXTERNAL=

STATICLIB=${EGNOME}/C/se/eif_gnome.a
GNOMELIBS=`gnome-config --libs gnome gnomeui gnorba gtk`


if [ -r ./.egnomebuild.rc ]
then
	if [ "$verbose" = TRUE ]
	then
		echo Reading ./.egnomebuild.rc
	fi
	. ./.egnomebuild.rc
elif [ -r $HOME/.egnomebuild.rc ]
then
	if [ "$verbose" = TRUE ]
	then
		echo Reading $HOME/.egnomebuild.rc
	fi
	. $HOME/.egnomebuild.rc
fi

if [ "$DEBIAN_GTK_DEBUG" = true ]
then
	if [ -r /usr/lib/libgtk_g.a ]
	then
		GNOMELIBS=`echo $GNOMELIBS | sed -e 's/-lgtk /-lgtk_g /' -e 's/-lgdk /-lgdk_g /' -e 's/-lglib /-lglib_g /'`
	fi
fi

INCFLAGS="$INCFLAGS -I. -I${EGTK}/C/se `gtk-config --cflags`"
LIBS="$LIBS ${STATICLIB} ${GNOMELIBS}"
CECIL="$CECIL -cecil ${EGTK}/C/se/cecil.se"
EXTERNAL="${EGTK}/C/se/eif_gtk_se.c ${EGTK}/C/se/eif_gtk.a $EXTERNAL"

if [ ! -r ${STATICLIB} ]
then
	echo "
${STATICLIB} does not exist;
cd to `dirname ${STATICLIB}`
and build it with the command 'make -f Makefile.se'"
	exit 1
fi

if [ "$verbose" = TRUE ]
then
	echo $COMPILE $SE_OPTS $CECIL ${root_class}.e -o ${root_class} $DEBUG \
	   $OPTIMISE $INCFLAGS $EXTERNAL $LIBS
fi

$COMPILE $SE_OPTS $CECIL ${root_class}.e -o ${root_class} $DEBUG $OPTIMISE \
	$INCFLAGS $EXTERNAL $LIBS && (
		echo -n "Generated C files to recompile : "
		grep "gcc -pipe .* -c ${root_class}" ${root_class}.make | wc -l
		sh -v ${root_class}.make
	)
