#!/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
#
trap '/bin/rm -f /tmp/$$' 1 2 15

#
# Load in the device list
#
source ./devicesBuild.list

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

	DEVICEMAKEFILES=${DEVICEMAKEFILES}" "${NOSPACENAME}/Makefile
done

#
# Find any vendor supplied Makefiles
#
VENDORMAKEFILES=
if test `find VendorSupplied/ -type d -mindepth 1 -maxdepth 1 2>/dev/null | wc -l | tr -d \t` -ge 1; then
	for i in `find VendorSupplied/ -type d -mindepth 1 -maxdepth 1`; do
		if test "${i}" != "VendorSupplied/CVS"; then
			VENDORMAKEFILES=${VENDORMAKEFILES}" "${i}/Makefile
		fi
	done
fi

if test "x$1" = "xadd"; then

	#
	# Search for the exact device list in configure.ac
	#
	if awk -v compare1="${DEVICEMAKEFILES}" -v compare2="${VENDORMAKEFILES}" '
BEGIN                  { found  = 0;
                         found1 = 0;
                         found2 = 0;            }
/^[d]nl @BEGIN-HACK-1/ { found = 1;             }
/^[d]nl @BEGIN-HACK-2/ { found = 2;             }
/^[d]nl @END-HACK-1/   { found = 0              }
/^[d]nl @END-HACK-2/   { found = 0              }
                       { if (found == 1)
                         {
                            if (compare1 == $0)
                            {
                               found1 = 1
                            }
                         }
                         if (found == 2)
                         {
                            if (compare2 == $0)
                            {
                               found2 = 1
                            }
                         }
                       }
END                    { if ((found1 == 1) && (found2 == 1))
                         {
                            exit 1
                         }
                         else
                         {
                            exit 0
                         }
                       }

' < configure.ac; then

		echo "Updating configure.ac"

		#
		# Add device directories to configure.ac
		#
		if awk -v DEVICEMAKEFILES="${DEVICEMAKEFILES}" -v VENDORMAKEFILES="${VENDORMAKEFILES}" '
BEGIN                  { skip = 0              }
/^[d]nl @BEGIN-HACK-1/ { skip = 1
                         print $0
                         print DEVICEMAKEFILES }
/^[d]nl @BEGIN-HACK-2/ { skip = 1
                         print $0
                         print VENDORMAKEFILES }
/^[d]nl @END-HACK-1/   { skip = 0              }
/^[d]nl @END-HACK-2/   { skip = 0              }
                       { if (skip == 0)
                         {
                            print $0
                         }                     
                       }
' < configure.ac > /tmp/$$; then

			echo "Success!"

			cp /tmp/$$ configure.ac

			rm /tmp/$$

		else

			echo "Failure!"

		fi
	fi

	(eval . ./setit ${DEVICELIST}; export DEVICES; cd XMLParser/; ./runParser)

elif test "x$1" = "xremove"; then

	#
	# Remove device directories from configure.ac
	#
	awk -v DEVICEMAKEFILES="${DEVICEMAKEFILES}" '
BEGIN                  { skip = 0              }
/^[d]nl @BEGIN-HACK-1/ { skip = 1
                         print $0              }
/^[d]nl @END-HACK-1/   { skip = 0              }
/^[d]nl @BEGIN-HACK-2/ { skip = 1
                         print $0              }
/^[d]nl @END-HACK-2/   { skip = 0              }
                       { if (skip == 0)
                         {
                            print $0
                         }                     }
' < configure.ac > /tmp/$$

	cp /tmp/$$ configure.ac

	rm /tmp/$$
else
	echo "Usage: $0 (add | remove)"
fi
