#! /bin/sh -e

# DP: biarch-include.dpatch
# DP:
# DP: Adds biarch include directories
# DP:     /usr/include/c++/<ver>/<arch>-linux-gnu
# DP:     /usr/local/include/<arch>-linux-gnu
# DP:     /usr/include/<arch>-linux-gnu
# DP: to the system include paths, depending on 32/64 bit mode.

dir=
if [ $# -eq 3 -a "$2" = '-d' ]; then
    pdir="-d $3"
    dir="$3/"
elif [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch)
        patch $pdir -f --no-backup-if-mismatch -p0 < $0
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p0 < $0
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

--- gcc/cppinit.c.orig	2004-03-02 21:09:15.000000000 +0100
+++ gcc/cppinit.c	2006-10-10 16:32:26.620906000 +0200
@@ -822,6 +822,14 @@
 		  && *(CPP_OPTION (pfile, sysroot)))
 		continue;
 
+	      if (p->biarch)
+		{
+		  if (p->biarch == 64 && !(target_flags & MASK_64BIT))
+		    continue;
+		  if (p->biarch == 32 && (target_flags & MASK_64BIT))
+		    continue;
+		}
+
 	      /* Does this dir start with the prefix?  */
 	      if (!strncmp (p->fname, default_prefix, default_len))
 		{
@@ -843,6 +851,14 @@
 
   for (p = cpp_include_defaults; p->fname; p++)
     {
+      if (p->biarch)
+	{
+	  if (p->biarch == 64 && !(target_flags & MASK_64BIT))
+	    continue;
+	  if (p->biarch == 32 && (target_flags & MASK_64BIT))
+	    continue;
+	}
+
       /* Some standard dirs are only for C++.  */
       if (!p->cplusplus
 	  || (CPP_OPTION (pfile, cplusplus)
--- gcc/cppdefault.h.orig	2003-11-07 00:13:31.000000000 +0100
+++ gcc/cppdefault.h	2006-10-10 15:36:55.728738250 +0200
@@ -30,6 +30,22 @@
 #define STANDARD_INCLUDE_DIR "/usr/include"
 #endif
 
+#ifndef STANDARD32_INCLUDE_DIR
+#define STANDARD32_INCLUDE_DIR STANDARD_INCLUDE_DIR "/" TARGET32_MACHINE
+#endif
+
+#ifndef LOCAL32_INCLUDE_DIR
+#define LOCAL32_INCLUDE_DIR LOCAL_INCLUDE_DIR "/" TARGET32_MACHINE
+#endif
+
+#ifndef STANDARD64_INCLUDE_DIR
+#define STANDARD64_INCLUDE_DIR STANDARD_INCLUDE_DIR "/" TARGET64_MACHINE
+#endif
+
+#ifndef LOCAL64_INCLUDE_DIR
+#define LOCAL64_INCLUDE_DIR LOCAL_INCLUDE_DIR "/" TARGET64_MACHINE
+#endif
+
 #ifndef STANDARD_INCLUDE_COMPONENT
 #define STANDARD_INCLUDE_COMPONENT 0
 #endif
@@ -38,6 +54,10 @@
 # undef LOCAL_INCLUDE_DIR
 # undef SYSTEM_INCLUDE_DIR
 # undef STANDARD_INCLUDE_DIR
+# undef STANDARD32_INCLUDE_DIR
+# undef LOCAL32_INCLUDE_DIR
+# undef STANDARD64_INCLUDE_DIR
+# undef LOCAL64_INCLUDE_DIR
 #else
 # undef CROSS_INCLUDE_DIR
 #endif
@@ -63,6 +83,7 @@
 				   C++.  */
   const int add_sysroot;	/* FNAME should be prefixed by
 				   cpp_SYSROOT.  */
+  const char biarch;            /* 32/64 bit biarch include */
 };
 
 extern const struct default_include cpp_include_defaults[];
--- gcc/cppdefault.c.orig	2003-11-07 00:13:31.000000000 +0100
+++ gcc/cppdefault.c	2006-10-10 15:35:38.931938750 +0200
@@ -37,12 +37,21 @@
 #endif
 #ifdef GPLUSPLUS_TOOL_INCLUDE_DIR
     /* Pick up GNU C++ target-dependent include files.  */
-    { GPLUSPLUS_TOOL_INCLUDE_DIR, "G++", 1, 1, 0 },
+    { GPLUSPLUS_INCLUDE_DIR "/" TARGET32_MACHINE, "G++", 1, 1, 0, 32 },
+    { GPLUSPLUS_INCLUDE_DIR "/" TARGET64_MACHINE, "G++", 1, 1, 0, 64 },
 #endif
 #ifdef GPLUSPLUS_BACKWARD_INCLUDE_DIR
     /* Pick up GNU C++ backward and deprecated include files.  */
     { GPLUSPLUS_BACKWARD_INCLUDE_DIR, "G++", 1, 1, 0 },
 #endif
+#ifdef LOCAL32_INCLUDE_DIR
+    /* /usr/local/include/$target_alias comes before the fixincluded header files.  */
+    { LOCAL32_INCLUDE_DIR, 0, 0, 1, 1, 32 },
+#endif
+#ifdef LOCAL64_INCLUDE_DIR
+    /* /usr/local/include/$target_alias comes before the fixincluded header files.  */
+    { LOCAL64_INCLUDE_DIR, 0, 0, 1, 1, 64 },
+#endif
 #ifdef LOCAL_INCLUDE_DIR
     /* /usr/local/include comes before the fixincluded header files.  */
     { LOCAL_INCLUDE_DIR, 0, 0, 1, 1 },
@@ -58,7 +67,7 @@
     /* One place the target system's headers might be.  */
     { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0 },
 #endif
-#ifdef TOOL_INCLUDE_DIR
+#if defined(TOOL_INCLUDE_DIR) && !defined(STANDARD32_INCLUDE_DIR) && !defined(STANDARD64_INCLUDE_DIR)
     /* Another place the target system's headers might be.  */
     { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1, 0 },
 #endif
@@ -66,6 +75,14 @@
     /* Some systems have an extra dir of include files.  */
     { SYSTEM_INCLUDE_DIR, 0, 0, 0, 1 },
 #endif
+#ifdef STANDARD32_INCLUDE_DIR
+    /* /usr/include/$target_alias comes before the fixincluded header files.  */
+    { STANDARD32_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 1, 1, 32 },
+#endif
+#ifdef STANDARD64_INCLUDE_DIR
+    /* /usr/include/$target_alias comes before the fixincluded header files.  */
+    { STANDARD64_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 1, 1, 64 },
+#endif
 #ifdef STANDARD_INCLUDE_DIR
     /* /usr/include comes dead last.  */
     { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0, 1 },
--- gcc/Makefile.in.orig	2006-10-10 15:30:56.486287000 +0200
+++ gcc/Makefile.in	2006-10-10 15:35:38.955940250 +0200
@@ -2221,6 +2221,13 @@
   -DTOOL_INCLUDE_DIR=\"$(gcc_tooldir)/include\" \
   @TARGET_SYSTEM_ROOT_DEFINE@
 
+ifneq (,$(TARGET32_MACHINE))
+  PREPROCESSOR_DEFINES += -DTARGET32_MACHINE=\"$(strip $(TARGET32_MACHINE))\"
+endif
+ifneq (,$(TARGET64_MACHINE))
+  PREPROCESSOR_DEFINES += -DTARGET64_MACHINE=\"$(strip $(TARGET64_MACHINE))\"
+endif
+
 LIBCPP_OBJS =	cpplib.o cpplex.o cppmacro.o cppexp.o cppfiles.o cpptrad.o \
 		cpphash.o cpperror.o cppinit.o cppdefault.o cppmain.o \
 		hashtable.o line-map.o mkdeps.o prefix.o mbchar.o
--- libstdc++-v3/include/Makefile.am.orig	2003-08-11 15:58:09.000000000 +0200
+++ libstdc++-v3/include/Makefile.am	2006-10-10 15:55:43.747235000 +0200
@@ -311,6 +311,7 @@
 
 target_srcdir = ${glibcpp_srcdir}/@OS_INC_SRCDIR@
 target_builddir = ./${target_alias}/bits
+target_installdir = ./${target_alias}/bits
 target_headers = \
 	${target_srcdir}/ctype_base.h \
 	${target_srcdir}/ctype_inline.h \
@@ -504,10 +505,10 @@
 	$(mkinstalldirs) $(DESTDIR)${gxx_include_dir}/${std_builddir}
 	for file in ${std_headers_rename}; do \
 	  $(INSTALL_DATA) ${std_builddir}/$${file} $(DESTDIR)${gxx_include_dir}/${std_builddir}; done
-	$(mkinstalldirs) $(DESTDIR)${gxx_include_dir}/${target_builddir}
+	$(mkinstalldirs) $(DESTDIR)${gxx_include_dir}/${target_installdir}
 	for file in ${target_headers} ${target_headers_extra} \
 	 ${thread_target_headers}; do \
-	  $(INSTALL_DATA) $${file} $(DESTDIR)${gxx_include_dir}/${target_builddir}; done
+	  $(INSTALL_DATA) $${file} $(DESTDIR)${gxx_include_dir}/${target_installdir}; done
 
 # By adding these files here, automake will remove them for 'make clean'
 CLEANFILES = *.pch stamp-std-precompile
--- libstdc++-v3/include/Makefile.in.orig	2003-08-11 15:58:09.000000000 +0200
+++ libstdc++-v3/include/Makefile.in	2006-10-10 15:56:53.887618500 +0200
@@ -424,6 +424,7 @@
 
 target_srcdir = ${glibcpp_srcdir}/@OS_INC_SRCDIR@
 target_builddir = ./${target_alias}/bits
+target_installdir = ./${target_alias}/bits
 target_headers = \
 	${target_srcdir}/ctype_base.h \
 	${target_srcdir}/ctype_inline.h \
@@ -727,10 +728,10 @@
 	$(mkinstalldirs) $(DESTDIR)${gxx_include_dir}/${std_builddir}
 	for file in ${std_headers_rename}; do \
 	  $(INSTALL_DATA) ${std_builddir}/$${file} $(DESTDIR)${gxx_include_dir}/${std_builddir}; done
-	$(mkinstalldirs) $(DESTDIR)${gxx_include_dir}/${target_builddir}
+	$(mkinstalldirs) $(DESTDIR)${gxx_include_dir}/${target_installdir}
 	for file in ${target_headers} ${target_headers_extra} \
 	 ${thread_target_headers}; do \
-	  $(INSTALL_DATA) $${file} $(DESTDIR)${gxx_include_dir}/${target_builddir}; done
+	  $(INSTALL_DATA) $${file} $(DESTDIR)${gxx_include_dir}/${target_installdir}; done
 
 # Stop implicit '.o' make rules from ever stomping on extensionless
 # headers, in the improbable case where some foolish, crack-addled
