#!/bin/sh

version=0.2.96
prefix=/usr
exec_prefix=${prefix}
CC=gcc
CXX=c++
GLIB_CONFIG=/usr/bin/glib-config

with_ui="yes"
language="c"

usage()
{
	cat <<EOF
Usage: gda-buildclient [OPTIONS] -o target -f input_files [-l libraries]
Options:
	[--no-ui]     Do not link against GDA widget library
	[--version]   Display script version
	[--cpp]       Compile a C++ client
EOF

	exit $1
}

if test $# -lt 1
then
	usage 1 1>&2
fi

# Make sure prefix is set
if test "$prefix" = ""
then
	echo "\$prefix not set...exiting"
	exit
fi

if test $# -lt 1
then
	usage 1 1>&2
fi

# Read command-line options
while test $# -gt 0
do
	case "$1" in
	-o)
		current_arg="ARG_TARGET"
		;;
	-f)
		current_arg="ARG_FILES"
		;;
	--no-ui)
		with_ui="no"
		;;
	--version)
		echo "gda-buildclient version ${version}"
		echo ""
		echo "Copyright (C) The Free Software Foundation, 1998-2000"
		echo "Software developped by Rodrigo Moya <rmoya@chez.com>"
		exit 0
		;;
	--cpp)
		language="cpp"
		;;
	*)
		if test "$current_arg" = "ARG_TARGET"
		then
			target="$1"
		elif test "$current_arg" = "ARG_FILES"
		then
			obj_files="${obj_files} $1"
		fi
		;;
	esac
	shift
done

# Check compilers
if test "${language}" = "c"
then
    # Make sure CC is set
    if test "$CC" = ""
    then
	echo "\$CC not set...exiting"
	exit
    fi
    INCLUDE_FLAGS="-I${prefix}/include/gda @GNOME_INCLUDEDIR@"
    LIBS_FLAGS="@GNOME_LIBDIR@ @GNOMEGNORBA_LIBS@  -L${prefix}/lib -lgda-common -lgda-client -lxml"
elif test "${language}" = "cpp"
then
    # Make sure CXX is set
    if test "$CXX" = ""
    then
	echo "\$CXX not set...exiting"
	exit
    fi
    INCLUDE_FLAGS="-I${prefix}/include/gda -I${prefix}/include/gda++ @GNOME_INCLUDEDIR@"
    LIBS_FLAGS="@GNOME_LIBDIR@ @GNOMEGNORBA_LIBS@  -L${prefix}/lib -lgda-common -lgda-client -lgda-clientcpp -lxml"
fi

# Check parameters specified by user
if test "${target}" = ""
then
	usage 1 1>&2
fi

# Set variables
if test "$with_ui" = "yes"
then
	LIBS_FLAGS="${LIBS_FLAGS} -lgda-client-ui -lgnomeprint"
fi

# Build the client
if `$CC ${INCLUDE_FLAGS} -o ${target} ${obj_files} ${LIBS_FLAGS}`
then
	echo "$0: ${target} was built without errors"
else
	echo "$0: error running $_"
	exit 1
fi
