#!/bin/sh -e

# Build autoconf 2.13 for browser's sole use.
(
    cd autoconf2.13

    ./configure \
        --prefix="$PWD/../junk" \
        --program-suffix=-2.13

    make
    make install
)

export PATH="$PWD/junk/bin:$PATH"

cp doc/mozconfig.example ./.mozconfig
sed -i 's^enable-jack^disable-jack^g' .mozconfig
sed -i 's^--with-system-lib.*^^' .mozconfig
sed -i 's#%SRCDIR%#'"$(realpath .)"'#g' .mozconfig
sed -i "s#%CFLAGS%#$CFLAGS#g" .mozconfig

for patch in *.patch; do
    patch -p1 < "$patch"
done

export CFLAGS="$CFLAGS -fno-exceptions"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="$LDFLAGS -Wl,-rpath=/usr/lib/browser"

./mach build
./mach stage

mkdir -p "$1/usr/bin"
mkdir -p "$1/usr/lib"

cp -r ~/build/wbobjects/dist/webbrowser "$1/usr/lib/browser"
ln -s ../lib/browser/webbrowser "$1/usr/bin/browser"
rm -r ~/build
