#!/usr/sbin/sh

#--with-nspr-prefix ???

#export PATH=$HOME/bin:$PATH
#---------------------------
#js-public-value changed
#------------------------------
#nspr add architecture
#--------------------
# change nspr-config from machine or use
#--with-nspr-prefix
#--------------
#add libmkv/vpx/vpx_int.h from repository
#--------------
#where is S_NUNBOX32 defined???
#build/src/js/js-config.h add following line
#define JS_NUNBOX32 1
#--------------
#add support
#/firefox/ipc/chromium/src/build/build_config.h
#define ARCH_CPU_OR1K 1
#define ARCH_CPU_32_BITS 1 
#--------------------------
# hardware specific, damn
# xpcom/reflect/xptcall/src/md/unix/moz.build
#--------------------------

set -e
#set -x
set -v

export SHELL=/usr/sbin/sh

cd src
rm -rf firefox
tar -xvjf  ../downloads/firefox-31.0.source.tar.bz2
mv mozilla-release firefox
cd firefox
rm -rf firefox-build-dir

patch -p1 < ../../patches/firefox-getprotobyname_r.patch
patch -p1 < ../../patches/firefox-finite.patch
patch -p1 < ../../patches/firefox-gregor.patch
patch -p1 < ../../patches/firefox-hunspell.patch

patch -p1 < ../../patches/firefox-31-nonascii.patch
patch -p1 < ../../patches/firefox-31-basename.patch
patch -p1 < ../../patches/firefox-29-pthread_t.patch


cat << EOF > mozconfig
mk_add_options MOZ_MAKE_FLAGS="-j1"
mk_add_options MOZ_GL_DEFAULT_PROVIDER=NONE

export CROSS_COMPILE=1
ac_add_options --target=or1k-linux-musl

ac_add_options --disable-dbus
ac_add_options --disable-necko-wifi
ac_add_options --disable-libnotify
ac_add_options --disable-jemalloc
ac_add_options --disable-replace-malloc
ac_add_options --disable-wrap-malloc

ac_add_options --disable-profiling
ac_add_options --disable-shark
ac_add_options --disable-callgrind
ac_add_options --disable-vtune

ac_add_options --disable-elf-hack
ac_add_options --disable-gold

#ac_add_options --disable-system-hunspell
#ac_add_options --disable-startup-notification

#ac_add_options --disable-ogg
#ac_add_options --disable-opus
ac_add_options --disable-pulseaudio
#ac_add_options --disable-webm
ac_add_options --disable-webrtc
ac_add_options --disable-libjpeg-turbo
ac_add_options --disable-webgl

ac_add_options --disable-gstreamer


ac_add_options --disable-system-sqlite

ac_add_options --prefix=/usr
ac_add_options --enable-application=browser

ac_add_options --disable-crashreporter
ac_add_options --disable-installer
ac_add_options --disable-updater
ac_add_options --disable-debug
ac_add_options --disable-tests

#ac_add_options --enable-optimize
#ac_add_options --enable-strip
#ac_add_options --enable-install-strip

ac_add_options --enable-gio
ac_add_options --enable-official-branding
ac_add_options --enable-safe-browsing
ac_add_options --enable-url-classifier

#ac_add_options --enable-system-cairo
ac_add_options --enable-system-ffi
ac_add_options --enable-system-pixman

#ac_add_options --with-pthreads

#ac_add_options --with-system-bz2
ac_add_options --with-system-jpeg
#ac_add_options --with-system-png
ac_add_options --with-system-zlib
ac_add_options --with-system-icu
ac_add_options --with-system-nspr
ac_add_options --with-system-libevent
ac_add_options --with-system-libvpx
#ac_add_options --with-system-nss

#ac_add_options --with-soft-float=yes
#ac_add_options --disable-extensions
#ac_add_options --disable-plugins
#ac_add_options --without-x
#ac_add_options --without-libIDL

#ac_add_options --disable-websms-backend
#ac_add_options --disable-jsd
#ac_add_options --disable-dbm
#ac_add_options --disable-accessibility
#ac_add_options --disable-webspeech
#ac_add_options --disable-opus
#ac_add_options --disable-wmf
#ac_add_options --disable-ffmpeg
#ac_add_options --disable-fmp4
#ac_add_options --disable-media-plugins 
#ac_add_options --disable-media-navigator
#ac_add_options --disable-alsa
#ac_add_options --disable-pref-extensions
#ac_add_options --disable-system-extension-dirs
#ac_add_options --disable-tests
#ac_add_options --disable-logging
#ac_add_options --disable-xul



mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/firefox-build-dir
EOF

#----------------------------------------------


mkdir -p include/sys
cat << EOF > include/sys/cdefs.h
#ifndef __P
#define __P(foo) foo
#endif
EOF

find . -name '*.[ch]' | xargs sed -i s'@#include <sys/cdefs.h>@#define __P(x) x@g'


#---------------------------------------------


# prevent overriding our own CFLAGS with built-in -O3 -g by appending
sed -i 's@MOZ_OPTIMIZE_FLAGS=@MOZ_OPTIMIZE_FLAGS= #@g' configure
sed -i 's@MOZ_PGO_OPTIMIZE_FLAGS=@MOZ_PGO_OPTIMIZE_FLAGS= #@g' configure
sed -i 's@MOZ_DEBUG_FLAGS=@MOZ_DEBUG_FLAGS= #@g' configure
sed -i 's@HOST_OPTIMIZE_FLAGS=@HOST_OPTIMIZE_FLAGS= #@g' configure

#----------------------------------------------------------------------------


allflags="-D_GNU_SOURCE -I$PWD/include -O2 -g0 -fno-unwind-tables -fno-asynchronous-unwind-tables -Wa,--noexecstack  -Wl,--no-keep-memory"
#allflags="-D_GNU_SOURCE -I$PWD/include -Os -g0 -Wl,--no-keep-memory"

#CFLAGS="$allflags" \
#CXXFLAGS="$allflags" \

make -f client.mk || exit 1
