#! /bin/sh -e

# gdc-fix-build-kfreebsd.dpatch by Arthur Loiret <arthur.loiret@gmail.com>
# DP: Fix gdc build on k*bsd*-gnu, update configure and target-ver-syms.sh
# DP: to build k*bsd*-gnu architectures as a freebsd

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
        #cd ${dir}gcc && autoconf
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p0 < $0
        #rm ${dir}gcc/configure
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0


--- gcc/d/phobos/configure	2007-09-03 01:23:59.000000000 +0200
+++ gcc/d/phobos/configure	2007-09-03 01:27:18.000000000 +0200
@@ -4599,7 +4599,7 @@
 case "$target_os" in
   darwin6*) DCFG_LONG_DOUBLE_FUNCS=config/noldfuncs ;;
   darwin*)  DCFG_LONG_DOUBLE_FUNCS=config/ldfuncs-darwin ;;
-  linux*)
+  linux*|k*bsd*-gnu)
     case "$target_cpu" in
 	powerpc*)
 	    DCFG_LONG_DOUBLE_FUNCS=config/ldfuncs-ppclinux
@@ -5687,7 +5687,7 @@
 
 case "$d_target_os" in
     # use fpmath on Linux
-    linux*)  d_have_fpsb=''
+    linux*|k*bsd*-gnu)  d_have_fpsb=''
 	     ;;
 esac
 
@@ -7014,7 +7014,7 @@
 rm -f conftest.d conftest.$ac_objext
 
 case "$d_target_os" in
-  aix*|*bsd*|cygwin*|darwin*|linux*|skyos*|solaris*|sysv*) d_have_unix=1 ;;
+  aix*|*bsd*|cygwin*|darwin*|linux*|k*bsd*-gnu|skyos*|solaris*|sysv*) d_have_unix=1 ;;
 esac
 
 DCFG_CBRIDGE_STDIO=
@@ -7027,7 +7027,7 @@
 	     d_sem_impl="mach"
 	     d_have_loader=1
 	     ;;
-    freebsd*)
+    freebsd*|k*bsd*-gnu)
 	     d_have_loader=1
 	     D_EXTRA_OBJS="$D_EXTRA_OBJS gcc/cbridge_stdio.o"
 	     DCFG_CBRIDGE_STDIO=GNU_CBridge_Stdio
@@ -9046,7 +9046,7 @@
 	    d_gc_stack=GC_Use_Stack_Fixed
 	    d_gc_data="$d_gc_data GC_Use_Data_Dyld"
 	    ;;
-  freebsd*) D_GC_MODULES="$D_GC_MODULES internal/gc/gc_freebsd.o"
+  freebsd*|k*bsd*-gnu) D_GC_MODULES="$D_GC_MODULES internal/gc/gc_freebsd.o"
 	    d_gc_stack=GC_Use_Stack_FreeBSD
 	    d_gc_data="$d_gc_data GC_Use_Data_Fixed"
 	    	    ;;
--- gcc/d/target-ver-syms.sh	2007-09-03 02:14:56.000000000 +0200
+++ gcc/d/target-ver-syms.sh	2007-09-03 02:52:43.000000000 +0200
@@ -30,6 +30,7 @@
 darwin*) d_os_versym=darwin ; d_unix=1 ;;
 elf*) ;;
 freebsd*) d_os_versym=freebsd ; d_unix=1 ;;
+k*bsd*-gnu) d_os_versym=freebsd ; d_unix=1;;
 linux*) d_os_versym=linux ; d_unix=1 ;; 
 mingw32*) d_os_versym=Win32; d_windows=1 ;;
 pe*)    case "$target" in
