#!/bin/bash
#
# IBM Omni driver
# Copyright (c) International Business Machines Corp., 2000
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
# the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

#
# Loop through the arguments and search for configure arguments and make arguments
#
CONFIGURE_ARGS=
MAKE_ARGS=

until [ -z "$1" ]
do
	case "$1" in
	--enable-*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	DEVICES=*)
		MA=${1:8}
		MAKE_ARGS="${MAKE_ARGS} DEVICES=\"${MA}\"";
	;;
	*)
		echo "Warning: Ignoring $1";
	;;
	esac

	shift
done

echo "CONFIGURE_ARGS=${CONFIGURE_ARGS}"
echo "MAKE_ARGS=${MAKE_ARGS}"

#
# Load in the device list
#
. ./devices.mak

#
# Loop through the devices and set up some variables
#
for i in ${DEVICELIST}; do
	#
	# Set up the names
	#
	NAME="`echo ${i} | sed -e 's/~//g' -e 's/\..*//'`"
	DIR="`echo ${i} | sed -e 's/~//g' -e 's/_/ /g'`"
	NOSPACENAME="`echo ${i} | sed -e 's/~//g' -e 's/_//g'`"

	#
	# Bug: Autotools cannot handle spaces in filenames!
	#      So create a spaceless directory name equivalent.
	#
	if test "${DIR}" != "${NOSPACENAME}"; then
		if test ! -L "${NOSPACENAME}"; then
			echo "Linking \"${DIR}\" to \"${NOSPACENAME}\""
			ln -s "${DIR}" "${NOSPACENAME}" 2>/dev/null
		fi
	fi
done

#
# Detect XML version
#
xml_version=
if which xml2-config >/dev/null; then
	xml_version=libxml2
elif which xml-config >/dev/null; then
	xml_version=libxml
#elif which xerces @TBD
#elif which xml4c  @TBD
fi

if test -z "${xml_version}"; then
	echo "Error: XML parser is undetected!"
	exit
fi

#
# Copy the correct version of XML in
# NOTE: Do not copy Makefile.in unless automake successfully converts it into the
#       proper format for the current aclocal/automake/autoconf setup.
#
filelist_libxml="Makefile.am Main.cpp MyErrorHandler.hpp MyErrorHandler.cpp DeviceInfo.hpp OmniDomParser.hpp OmniDomParser.cpp"
filelist_xerces1_3="Makefile.am DebugDocumentHandler.hpp DebugDocumentHandler.cpp DeviceInfo.hpp MyErrorHandler.hpp MyErrorHandler.cpp Main.cpp OmniDomParser.hpp OmniDomParser.cpp DebugParser.hpp DebugParser.cpp"
filelist_xml4c3_1_0="Makefile.am DebugDocumentHandler.hpp DebugDocumentHandler.cpp DeviceInfo.hpp MyErrorHandler.hpp MyErrorHandler.cpp Main.cpp OmniDomParser.hpp OmniDomParser.cpp DebugParser.hpp DebugParser.cpp"
case ${xml_version} in
libxml*)	filelist=$filelist_libxml;	dir=libxml		;;
xerces1_3)	filelist=$filelist_xerces1_3;	dir=xerces-c1_3_0-linux	;;
xml4c3_1_0)	filelist=$filelist_xml4c3_1_0;	dir=xml4c3_1_0-linux	;;
*)		echo "Error: should not be here!"; exit			;;
esac
cd XMLParser;
success=1
for i in $filelist; do
	if test ! -f $i; then
		success=0
		echo "Missing: $i"
		break
	fi
	cmp -s $i $dir/$i
	if test $? != 0; then
		success=0
		echo "Different: $i"
		break
	fi
done
if test $success != 1; then
	for i in $filelist; do
		echo "Copying: $dir/$i"
		cp $dir/$i .
	done		
fi
cd ..

if test -f aclocal.m4; then
	rm aclocal.m4*
fi

echo "Running aclocal..."
aclocal || exit

echo "Running autoheader..."
autoheader || exit

echo "Running automake..."
automake --gnu --add-missing || exit

echo "Running autoreconf..."
autoreconf || exit

echo "Running configure "${CONFIGURE_ARGS}" ..."
# @BUG - running "./configure ${CONFIGURE_ARGS}" vs "eval ./configure ${CONFIGURE_ARGS}" produces different results!
eval ./configure ${CONFIGURE_ARGS} || exit

echo "Running make "${MAKE_ARGS}" ..."
# @BUG - running "make ${MAKE_ARGS}" vs "eval make ${MAKE_ARGS}" produces different results!
eval make ${MAKE_ARGS} || exit

echo "Success!"
