#!/bin/bash -v

RELEASE=otp_src_R12B-3
TARGET=arm-angstrom-linux-gnueabi
INSTALL_DIR=/tmp/cross-compile

export ac_cv_c_bigendian=no
export ac_cv_func_setvbuf_reversed=no
export ac_cv_func_mmap_fixed_mapped=yes
export ac_cv_sizeof_long_long=8 
export ac_cv_sizeof_off_t=8
export ac_cv_func_getaddrinfo=no
export ac_cv_prog_RX_LD=${TARGET}-ld

rm -rf ${INSTALL_DIR}
rm -rf ${RELEASE}
tar xvf "${RELEASE}.tar.gz"
cd ${RELEASE}

# Deal with the make patch
cd make
patch -p0 <<EOF
--- otp.mk.in.orig      2007-01-16 00:05:19.000000000 +0100
+++ otp.mk.in   2007-01-16 00:06:22.000000000 +0100
@@ -69,21 +69,7 @@
 #      Erlang language section
 # ----------------------------------------------------
 EMULATOR = beam
-ifeq ($(findstring vxworks,$(TARGET)),vxworks)
-# VxWorks object files should be compressed.
-# Other object files should have debug_info.
-  ERL_COMPILE_FLAGS += +compressed
-else
-  ifeq ($(findstring ose_ppc750,$(TARGET)),ose_ppc750)
-    ERL_COMPILE_FLAGS += +compressed
-  else
-    ifdef BOOTSTRAP
-      ERL_COMPILE_FLAGS += +slim
-    else
-      ERL_COMPILE_FLAGS += +debug_info
-    endif
-  endif
-endif
+ERL_COMPILE_FLAGS += +compressed +slim
 ERLC_WFLAGS = -W
 ERLC = erlc $(ERLC_WFLAGS) $(ERLC_FLAGS)
 ERL = erl -boot start_clean
EOF
cd ..

# Deal with configure patch
cd erts
patch -p0 <<EOF
--- configure.in.orig   2006-03-17 17:38:39.000000000 +0100
+++ configure.in        2006-03-17 17:29:23.000000000 +0100
@@ -1322,7 +1322,7 @@
   exit(0);
 #endif
 }
-], poll_works=true, poll_works=false, poll_works=false)
+], poll_works=true, poll_works=false, poll_works=true)
 case $poll_works in
 true)
        AC_MSG_RESULT(ok)
@@ -1365,7 +1365,12 @@
     DED_CFLAGS="$DED_CFLAGS -fPIC"
 fi
 
-DED_LD=ld
+if test "x$LD" = x; then
+ DED_LD=ld
+else
+ DED_LD=$LD
+fi
+
 DED_LD_FLAG_RUNTIME_LIBRARY_PATH="-R"
 STATIC_CFLAGS=""
EOF
autoconf configure.in > configure
cd ..

cd erts/emulator
patch -p0 <<EOF
--- Makefile.in.orig	2006-08-29 02:46:47.000000000 -0700
+++ Makefile.in	2006-08-31 21:46:25.000000000 -0700
@@ -792,9 +792,10 @@
 	$(CC) $(subst O2,O3, $(CFLAGS)) $(INCLUDES) -c $< -o $@
 
 $(BINDIR)/hipe_mkliterals$(TF_MARKER):	$(OBJDIR)/hipe_mkliterals.o
-	$(CC) $(CFLAGS) $(INCLUDES) -o $@ $<
+	gcc $(CFLAGS) $(INCLUDES) -o $@ hipe/hipe_mkliterals.c
 
 $(OBJDIR)/hipe_mkliterals.o:	$(TTF_DIR)/hipe_x86_asm.h $(TTF_DIR)/hipe_ppc_asm.h
+	gcc $(CFLAGS) $(INCLUDES) -c hipe/hipe_mkliterals.c
 
 $(TTF_DIR)/hipe_literals.h:	$(BINDIR)/hipe_mkliterals$(TF_MARKER)
 	$(BINDIR)/hipe_mkliterals$(TF_MARKER) -c > $@
EOF
cd ../..

cd lib/runtime_tools/c_src
patch -p0 <<EOF
--- trace_file_drv.c.orig        2008-07-19 19:14:30.000000000 -0400
+++ trace_file_drv.c    2008-07-19 19:14:42.000000000 -0400
@@ -65,7 +65,7 @@
 #          ifdef _MAX_PATH
 #            define MAXPATHLEN _MAX_PATH
 #         else
-#            error Could not define MAXPATHLEN
+#            define MAXPATHLEN 2048
 #          endif
 #        endif
 #      endif
EOF
cd ../../..

./configure --prefix=${INSTALL_DIR} --without-ssl --without-java --disable-hipe --disable-smp-support --host=${TARGET} --build=i686-pc-linux-gnu
TARGET=${TARGET} make noboot
TARGET=${TARGET} make install
