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

#
# Initialize our variables
#
CONFIGURE_ARGS=
MAKE_ARGS=
NEW_DEVICES=
BUILD_COMPILED_DEVICE=0
BUILD_XML_DEVICE=1

#
# Loop through the arguments and search for configure arguments and make arguments
#
until [ -z "$1" ]
do
	case "$1" in
        --prefix* | --exec-prefix* | --bindir* | --sbindir* | --libexecdir*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--datadir* | --sysconfdir* | --sharedstatedir* | --localstatedir*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--libdir* | --includedir* | --oldincludedir* | --infodir* | --mandir*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--program-prefix* | --program-suffix* | --program-transform-name*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--build* | --host* | --with-PACKAGE* | --without-PACKAGE*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--with-gnu-ld* | --with-pic*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--disable-devicexml*)
		BUILD_XML_DEVICE=0
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--enable-devicexml*)
		ARG=${1:18}
		if test ! -z "${ARG}"; then
			if test ${ARG:0:1} = "="; then
				ARG=${ARG:1}
			fi
		fi
		if test -z "${ARG}" -o "${ARG}" = "true" -o "${ARG}" = "yes"; then
			BUILD_XML_DEVICE=1
		elif test "${ARG}" = "false" -o "${ARG}" = "no"; then
			BUILD_XML_DEVICE=0
		fi
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--disable-devicecompile*)
		BUILD_COMPILED_DEVICE=0
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--enable-devicecompile*)
		ARG=${1:22}
		if test ! -z "${ARG}"; then
			if test ${ARG:0:1} = "="; then
				ARG=${ARG:1}
			fi
		fi
		if test -z "${ARG}" -o "${ARG}" = "true" -o "${ARG}" = "yes"; then
			BUILD_COMPILED_DEVICE=1
		elif test "${ARG}" = "false" -o "${ARG}" = "no"; then
			BUILD_COMPILED_DEVICE=0
		fi
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	--disable-* | --enable-*)
		CONFIGURE_ARGS="${CONFIGURE_ARGS} ${1}";
	;;
	DEVICES=*)
		MA=${1:8}
		NEW_DEVICES="${NEW_DEVICES} ${MA}"
#		MAKE_ARGS="${MAKE_ARGS} DEVICES=\"${MA}\"";
	;;
	--help*)
		echo "arguments that can be passed to $0 are as follows:"
		echo "	DEVICES=xxx"
		echo
		echo "arguments that can be passed to configure are as follows:"
		echo
		./configure --help
		exit
	;;
	*)
		echo "Warning: Ignoring $1";
	;;
	esac

	shift
done

#
# Print out what the variables are set to
#
echo "CONFIGURE_ARGS=\"${CONFIGURE_ARGS}\""
echo "MAKE_ARGS=\"${MAKE_ARGS}\""
echo "NEW_DEVICES=\"${NEW_DEVICES}\""
echo "BUILD_COMPILED_DEVICE=${BUILD_COMPILED_DEVICE}"
echo "BUILD_XML_DEVICE=${BUILD_XML_DEVICE}"

#
# Setup the list of devices
#
if test -z "${NEW_DEVICES}"; then
	#
	# Load in the list of every possible device
	#
	source ./devices.list
else
	#
	# Set the list to the supplied devices
	#
	DEVICELIST=${NEW_DEVICES}
fi

#
# Generate devices.list
#
echo "# NOTE: This is a generated file!  Do not edit!"                             > devices.list
echo "#"                                                                          >> devices.list
echo "# IBM Omni driver"                                                          >> devices.list
echo "# Copyright (c) International Business Machines Corp., 2000"                >> devices.list
echo "#"                                                                          >> devices.list
echo "# This library is free software; you can redistribute it and/or modify"     >> devices.list
echo "# it under the terms of the GNU Lesser General Public License as published" >> devices.list
echo "# by the Free Software Foundation; either version 2.1 of the License, or"   >> devices.list
echo "# (at your option) any later version."                                      >> devices.list
echo "#"                                                                          >> devices.list
echo "# This library is distributed in the hope that it will be useful,"          >> devices.list
echo "# but WITHOUT ANY WARRANTY; without even the implied warranty of"           >> devices.list
echo "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See"                >> devices.list
echo "# the GNU Lesser General Public License for more details."                  >> devices.list
echo "#"                                                                          >> devices.list
echo "# You should have received a copy of the GNU Lesser General Public License" >> devices.list
echo "# along with this library; if not, write to the Free Software"              >> devices.list
echo "# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"  >> devices.list
echo "#"                                                                          >> devices.list
echo ""                                                                           >> devices.list
echo "# NOTE: underscores will be replaced with spaces"                           >> devices.list
echo "#"                                                                          >> devices.list
echo "# NOTE: we are not including Epson.PDC~ because we do not want to build"    >> devices.list
echo "#       these devices for the Omni package."                                >> devices.list
echo "#"                                                                          >> devices.list
echo ""                                                                           >> devices.list

# initialize the command line for $1 $2 ...
set -- ${DEVICELIST}

START=true
HEAD1="DEVICELIST=\""
TAIL1=" \\"
HEAD2="            "
TAIL2="\""

if test -z "$1"; then
	echo -n "${HEAD1}" >> devices.list
	echo "${TAIL2}"    >> devices.list
fi

until [ -z "$1" ]
do
	if ${START}; then
		echo -n "${HEAD1}" >> devices.list
		START=false
	else
		echo -n "${HEAD2}" >> devices.list
	fi

	echo -n $1                 >> devices.list

	shift

	if test -z "$1"; then
		echo "${TAIL2}"    >> devices.list
	else
		echo "${TAIL1}"    >> devices.list
	fi
done

#
# Generate devices.mak
#
echo "# NOTE: This is a generated file!  Do not edit!"                             > devices.mak
echo "#"                                                                          >> devices.mak
echo "# IBM Omni driver"                                                          >> devices.mak
echo "# Copyright (c) International Business Machines Corp., 2000"                >> devices.mak
echo "#"                                                                          >> devices.mak
echo "# This library is free software; you can redistribute it and/or modify"     >> devices.mak
echo "# it under the terms of the GNU Lesser General Public License as published" >> devices.mak
echo "# by the Free Software Foundation; either version 2.1 of the License, or"   >> devices.mak
echo "# (at your option) any later version."                                      >> devices.mak
echo "#"                                                                          >> devices.mak
echo "# This library is distributed in the hope that it will be useful,"          >> devices.mak
echo "# but WITHOUT ANY WARRANTY; without even the implied warranty of"           >> devices.mak
echo "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See"                >> devices.mak
echo "# the GNU Lesser General Public License for more details."                  >> devices.mak
echo "#"                                                                          >> devices.mak
echo "# You should have received a copy of the GNU Lesser General Public License" >> devices.mak
echo "# along with this library; if not, write to the Free Software"              >> devices.mak
echo "# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"  >> devices.mak
echo "#"                                                                          >> devices.mak
echo ""                                                                           >> devices.mak
echo "# NOTE: underscores will be replaced with spaces"                           >> devices.mak
echo "#"                                                                          >> devices.mak
echo "# NOTE: we are not including Epson.PDC~ because we do not want to build"    >> devices.mak
echo "#       these devices for the Omni package."                                >> devices.mak
echo "#"                                                                          >> devices.mak
echo ""                                                                           >> devices.mak

if   test ${BUILD_COMPILED_DEVICE} = 0 -a ${BUILD_XML_DEVICE} = 0; then
	DEVICES="test~"
elif test ${BUILD_COMPILED_DEVICE} = 0 -a ${BUILD_XML_DEVICE} = 1; then
	DEVICES="XMLDevice~ test~"
elif test ${BUILD_COMPILED_DEVICE} = 1 -a ${BUILD_XML_DEVICE} = 0; then
	DEVICES="${DEVICELIST} test~"
elif test ${BUILD_COMPILED_DEVICE} = 1 -a ${BUILD_XML_DEVICE} = 1; then
	DEVICES="${DEVICELIST} XMLDevice~ test~"
fi

# initialize the command line for $1 $2 ...
set -- ${DEVICES}

START=true
HEAD1="DEVICELIST=\""
TAIL1=" \\"
HEAD2="            "
TAIL2="\""

until [ -z "$1" ]
do
	if ${START}; then
		echo -n "${HEAD1}" >> devices.mak
		START=false
	else
		echo -n "${HEAD2}" >> devices.mak
	fi

	echo -n $1                 >> devices.mak

	shift

	if test -z "$1"; then
		echo "${TAIL2}"    >> devices.mak
	else
		echo "${TAIL1}"    >> devices.mak
	fi
done

#
# Load in the list of all of the devices
#
source ./devices.list

#
# Loop through the devices, set up some variables, and make symbolic links
#
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 the XMLParser directory
# 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 ..

#
# Copy the correct version of XML in the DeviceFontSandBox directory
# 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 UPDFFontParser.hpp UPDFFontParser.cpp XmlFile.hpp XmlFile.cpp"
filelist_xerces1_3="Makefile.am UPDFFontParser.hpp UPDFFontParser.cpp XmlFile.hpp XmlFile.cpp MyDOMErrorHandler.hpp MyDOMErrorHandler.cpp"
filelist_xml4c3_1_0="Makefile.am UPDFFontParser.hpp UPDFFontParser.cpp XmlFile.hpp XmlFile.cpp MyDOMErrorHandler.hpp MyDOMErrorHandler.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 DeviceFontSandBox;
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}" "CFLAGS=\"${CFLAGS}\"" ..."
./configure ${CONFIGURE_ARGS} CFLAGS="${CFLAGS}" || exit

if test ${BUILD_XML_DEVICE} = 1; then
	echo "Configuring XMLDevices..."
	(cd XMLDevice; ./cleanStart)
fi

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!"
