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

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

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

cd lib/runtime_tools/c_src
echo '--- trace_file_drv.c.orig        2008-07-19 19:14:30.000000000 -0400' > tracepatch
echo '+++ trace_file_drv.c    2008-07-19 19:14:42.000000000 -0400' >> tracepatch
echo '@@ -65,7 +65,7 @@' >> tracepatch
echo ' #          ifdef _MAX_PATH' >> tracepatch
echo ' #            define MAXPATHLEN _MAX_PATH' >> tracepatch
echo ' #         else' >> tracepatch
echo '-#            error Could not define MAXPATHLEN' >> tracepatch
echo '+#            define MAXPATHLEN 2048' >> tracepatch
echo ' #          endif' >> tracepatch
echo ' #        endif' >> tracepatch
echo ' #      endif' >> tracepatch
cat tracepatch | patch -p0
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
