#!/bin/bash -e

#---------------------------------------------------------------------
# To be documented.

LACTRIPLET=

if [ "@$LACTRIPLET" == "@" ]; then
    UM=`uname -m`
    if [ "@$UM" == "@i386" ]; then
        LACTRIPLET=i586-pc-linux-gnu
    fi
fi

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

# You can specify the top-level target directory using a setting simi-
# lar to the following:
#
# export PREFIX=/opt/browsers

# If the specified directory doesn't already exist, it'll be created.

# The default is equivalent to:
# export PREFIX=$HOME/browsers

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

# You can specify the number of  build-time parallel jobs using a set-
# ting similar to the following:
#
# export NUMJOBS=4

# The default is equivalent to:
# export NUMJOBS=6

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

# You can build with sound support in any of three modes:
#
# export USE_ALSA=true      # Enable  ALSA, disable PulseAudio
# export USE_ALSA=false     # Disable ALSA, enable  PulseAudio
# export SKIP_SOUND=true    # Disable sound support

# If PulseAudio is installed,  as determined by "which padsp", the de-
# fault is equivalent to:
#
# export USE_ALSA=false

# If PulseAudio isn't installed,  as determined by "which padsp", the
# default is equivalent to:
#
# export USE_ALSA=true

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

# You can build with Gtk2 or Gtk3:
#
# export USE_GTK2=false     # Use Gtk3
# export USE_GTK2=true      # Use Gtk2

# The default is equivalent to:
# export USE_GTK2=false

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

# You can build with or without DBus support:
#
# export SKIP_DBUS=false    # Enable  DBus
# export SKIP_DBUS=true     # Disable DBus

# The default is equivalent to:
# export SKIP_DBUS=true

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

# You can build with or without OpenLDAP support:
#
# export SKIP_LDAP=false    # Enable  OpenLDAP
# export SKIP_LDAP=true     # Disable OpenLDAP

# The default is equivalent to:
# export SKIP_LDAP=false

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

# You can select a "lite" build mode as follows:
#
# export LITEBUILD=true

# The preceding setting is equivalent to:
#
# export SKIP_DBUS=true
# export SKIP_LDAP=true
# export SKIP_SOUND=true
# export USE_GTK2=true

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

# Each of the programs  has a separate "bin" directory  located in the
# program's own directory tree.

# For your  convenience,  an extra  "bin" directory  is created in the
# PREFIX directory  which contains symlinks  to each of  the programs.
# For example,  the following configuration results in a  "bin" direc-
# tory with the full path "/opt/browsers/bin":
#
#   export PREFIX=/opt/browsers
#   # ...
#   bash -e ./buildme.sh

# In this example,  add "/opt/browsers/bin" to PATH and you're good to
# go.

# If you don't want the extra "bin" directory, you can use an environ-
# ment variable named "ADDBINDIR" as follows to disable it:
#
#   export PREFIX=/opt/browsers
#   export ADDBINDIR=false
#   # ...
#   bash -e ./buildme.sh

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

if [ "@$ADDBINDIR"  == "@"     ]; then
    ADDBINDIR=true
fi

if [ "@$LITEBUILD"  == "@"     ]; then
    LITEBUILD=false
fi

if [ "@$NUMJOBS"    == "@"     ]; then
    NUMJOBS=6
fi

if [ "@$PREFIX"     == "@"     ]; then
    PREFIX=$HOME/browsers
fi

if [ "@$SKIP_DBUS"  == "@"     ]; then
    SKIP_DBUS=true
fi

if [ "@$SKIP_LDAP"  == "@"     ]; then
    SKIP_LDAP=false
fi

if [ "@$SKIP_SOUND" == "@"     ]; then
    SKIP_SOUND=false
fi

if [ "@$USE_ALSA"   == "@"     ]; then
    USE_ALSA=false
    (which padsp >& /dev/null) || USE_ALSA=true
fi

if [ "@$USE_GTK2"   == "@"     ]; then
    USE_GTK2=false
fi

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

if [ "@$LITEBUILD"  == "@true" ]; then
    SKIP_DBUS=true
    SKIP_LDAP=true
    SKIP_SOUND=true
    USE_GTK2=true
fi

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

if [ "@$USE_ALSA"   == "@true" ]; then
    SSALSAX=--enable-alsa
    SSPULSE=--disable-pulseaudio
else
    SSALSAX=--disable-alsa
    SSPULSE=--enable-pulseaudio
fi

if [ "@$SKIP_SOUND" == "@true" ]; then
    SSALSAX=--disable-alsa
    SSPULSE=--disable-pulseaudio
fi

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

function checkprog {
    prog=$1
    echo Checking for program $prog
    OKFLAG=true
    which $prog 2>/dev/null || OKFLAG=false

    if [ "@$OKFLAG" == "@false" ]; then
        cat << END
Error: Required program wasn't found: $prog
END
        exit 1
    fi
}

#---------------------------------------------------------------------
# Check for a few required programs.

# This step is  just a  smoke-check and not a full check for dependen-
# cies.

for prog in \
    autoconf autoconf-2.13 \
    brotli perl pkg-config python yasm
do
    checkprog $prog
done

if [ "@$SSPULSE" == "@--enable-pulseaudio" ]; then
    checkprog padsp
fi

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

if [ \!   -d $PREFIX ]; then
    mkdir -p $PREFIX                                || exit 1
fi

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

TESTFILE=$PREFIX/test42.temp

OKFLAG=true
touch $TESTFILE || OKFLAG=false
rm    $TESTFILE || OKFLAG=false

if [ "@$OKFLAG" == "@false" ]; then
    cat << END
Error: This script needs write access to the following directory:
$PREFIX

Or you can set the PREFIX  environment variable to specify a different
directory that you have write access to. Remember to "export" the var-
iable.

For example: export PREFIX=/opt/browsers
END
    exit 1
fi

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

PRODDIR_ALBUSLUNA=$PREFIX/albusluna
PRODDIR_AMBASSADOR=$PREFIX/ambassador
PRODDIR_BASILISK=$PREFIX/basilisk
PRODDIR_BLUEGORILLA=$PREFIX/bluegorilla
PRODDIR_EPYRUS=$PREFIX/epyrus
PRODDIR_ICEDOVE=$PREFIX/icedove

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

rm -fr  $PRODDIR_ALBUSLUNA   \
        $PRODDIR_AMBASSADOR  \
        $PRODDIR_BASILISK    \
        $PRODDIR_BLUEGORILLA \
        $PRODDIR_EPYRUS      \
        $PRODDIR_ICEDOVE

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

for dir in \
    albusluna  ambassador  basilisk  bluegorilla  epyrus \
    rest
do
    if [ \! -d $dir ]; then
        cat << END
Error: This script  should be executed in the top-level of a directory
tree unpacked from "uxp-browsers*.tar.bz".
END
        exit 1
    fi
done

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

TOPDIR=`pwd`

#---------------------------------------------------------------------
# Make a required multi-program edit.

sed -e '/content_geckomediaplugins.xpt/d' \
    -i `find . -type f -name package-manifest.in | \
grep -v 'albusluna'`                                || exit 1

#---------------------------------------------------------------------
# Set up Basilisk build.

echo `date` Step: Set up Basilisk build

THISTOP=$TOPDIR/basilisk                            || exit 1
cd $THISTOP                                         || exit 1

cp -a ../albusluna/platform .                       || exit 1
rm -fr platform/configure platform/old-configure    || exit 1
rm -fr obj-*                                        || exit 1

sed -e 's/198001030000/198101030000/g' \
    -i `find . -type f -name autotargets.mk`        || exit 1

#---------------------------------------------------------------------
# Create Basilisk ".mozconfig" file.

echo `date` Step: Create Basilisk ".mozconfig" file

cat > .mozconfig << END                             || exit 1
mk_add_options MOZ_MAKE_FLAGS="-j$NUMJOBS"
export MOZ_PKG_SPECIAL=gtk3

ac_add_options --enable-application=basilisk
ac_add_options --prefix=$PRODDIR_BASILISK
ac_add_options --with-app-basename=basilisk
ac_add_options --with-pthreads

# Sound
ac_add_options $SSALSAX
ac_add_options $SSPULSE

# Other disable
ac_add_options --disable-crashreporter
ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
ac_add_options --disable-gamepad
ac_add_options --disable-maintenance-service
ac_add_options --disable-necko-wifi
ac_add_options --disable-sandbox
ac_add_options --disable-tests
ac_add_options --disable-updater
ac_add_options --disable-webrtc

# Other enable
ac_add_options --enable-default-toolkit=cairo-gtk3
ac_add_options --enable-devtools
ac_add_options --enable-jemalloc
ac_add_options --enable-optimize="-O2 -w"
ac_add_options --enable-strip

# Misc.
END

#---------------------------------------------------------------------
# Adjust build settings.

# Note: Not all of the settings used here are confirmed to be support-
# ed.

if [ "@$LATRIPLET" \!= "@" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --target=$LACTRIPLET
END
fi

if [ "@$SKIP_DBUS"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-dbus
END
else
    cat >> .mozconfig << END                        || exit 1
ac_add_options --enable-dbus
END
fi

if [ "@$SKIP_LDAP"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-ldap
END
fi

if [ "@$USE_GTK2"   == "@true" ]; then
    sed -e "s@=cairo-gtk3@=cairo-gtk2@" \
        -i .mozconfig                               || exit 1
fi

if [ "@$LITEBUILD"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-system-cairo
ac_add_options --disable-system-pixman
END
fi

#---------------------------------------------------------------------
# Compile Basilisk.

echo `date` Step: Compile Basilisk

DONE=false
for attempt in 1 2
do
    if [ "@$DONE" == "@false" ]; then
        echo "basilisk attempt #$attempt"
        rm -fr obj-*/dist/bin/                      || exit 1
        DONE=true
        .//mach build || DONE=false
    fi
done
if [ "@$DONE" == "@false" ]; then exit 1; fi

#---------------------------------------------------------------------
# Install Basilisk.

echo `date` Step: Install Basilisk

comp=`echo obj-*/dist/bin/components`               || exit 1
touch $comp/nsAbLDAPAutoCompleteSearch.js           || exit 1

./mach install || ./mach install                    || exit 1

# Replace an  absolute symlink with a relative symlink.  Additionally,
# confirm that the current program has been built and do a smoke-check
# on the symlink.

pushd $PRODDIR_BASILISK/bin                         || exit 1
ln -nsf ../lib/*-*/basilisk .                       || exit 1
wc -c basilisk                                      || exit 1
popd                                                || exit 1

# Replace  a redundant copy of  "libxul.so"  with a  relative symlink.
# Additionally,  confirm  that  "libxul.so"  has been built  and  do a
# smoke-check on the symlink.

pushd $PRODDIR_BASILISK/lib/basilisk-[0-9]*         || exit 1
ln -nsf ../*-devel-*/sdk/lib/libxul.so .            || exit 1
wc -c libxul.so                                     || exit 1
popd                                                || exit 1

#---------------------------------------------------------------------
# Install Basilisk XPI extensions.

echo `date` Step: Install Basilisk XPI extensions

# This part is application-specific.
L=$PRODDIR_BASILISK/lib
A=`ls $L | grep basilisk- | grep -v devel`
XPI_DEST=$L/$A/extensions
XPI_SOURCE=$TOPDIR/xpi/basilisk

if [ -d   $XPI_SOURCE ]; then
    mkdir -p                $XPI_DEST               || exit 1
    cp -a $XPI_SOURCE/*.xpi $XPI_DEST/              || exit 1
fi

# This part is presently needed only for Basilisk.
TRIB=$L/$A/distribution
mkdir -p $TRIB
ln -s ../extensions $TRIB/extensions

#---------------------------------------------------------------------
# End of Basilisk build.

echo `date` Step: End of Basilisk build

#---------------------------------------------------------------------
# Set up Icedove build.

echo `date` Step: Set up Icedove build

THISTOP=$TOPDIR/rest                                || exit 1
cd $THISTOP                                         || exit 1

rm -fr platform/configure platform/old-configure    || exit 1
rm -fr obj-*                                        || exit 1

#---------------------------------------------------------------------
# Create Icedove ".mozconfig" file.

echo `date` Step: Create Icedove ".mozconfig" file

cat > .mozconfig << END                             || exit 1
mk_add_options MOZ_MAKE_FLAGS="-j$NUMJOBS"

ac_add_options --enable-application=mail
ac_add_options --prefix=$PRODDIR_ICEDOVE
ac_add_options --with-app-basename=icedove
ac_add_options --with-pthreads

# Branding
ac_add_options --disable-official-branding
ac_add_options --with-branding=mail/branding/icedove

# Sound
ac_add_options $SSALSAX
ac_add_options $SSPULSE

# Other disable
ac_add_options --disable-accessibility
ac_add_options --disable-cpp-rtti
ac_add_options --disable-crashreporter
ac_add_options --disable-necko-wifi
ac_add_options --disable-negotiateauth
ac_add_options --disable-tests
ac_add_options --disable-updater
ac_add_options --disable-webrtc
ac_add_options --disable-safe-browsing
ac_add_options --disable-url-classifier
ac_add_options --disable-sandbox
ac_add_options --disable-debug-symbols
ac_add_options --disable-debug

# Other enable
ac_add_options --enable-calendar
ac_add_options --enable-default-toolkit=cairo-gtk3
ac_add_options --enable-gio
ac_add_options --enable-nspr-build
ac_add_options --enable-optimize="-O2 -w"
ac_add_options --enable-strip

# Misc.
END

#---------------------------------------------------------------------
# Adjust build settings.

# Note: Not all of the settings used here are confirmed to be support-
# ed.

if [ "@$LATRIPLET" \!= "@" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --target=$LACTRIPLET
END
fi

if [ "@$SKIP_DBUS"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-dbus
END
else
    cat >> .mozconfig << END                        || exit 1
ac_add_options --enable-dbus
END
fi

if [ "@$SKIP_LDAP"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-ldap
END
fi

if [ "@$USE_GTK2"   == "@true" ]; then
    sed -e "s@=cairo-gtk3@=cairo-gtk2@" \
        -i .mozconfig                               || exit 1
fi

if [ "@$LITEBUILD"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-system-cairo
ac_add_options --disable-system-pixman
END
fi

#---------------------------------------------------------------------
# Compile Icedove.

echo `date` Step: Compile Icedove

DONE=false
for attempt in 1 2
do
    if [ "@$DONE" == "@false" ]; then
        echo "icedove attempt #$attempt"
        rm -fr obj-*/dist/bin/                      || exit 1
        DONE=true
        mozilla/mach build || DONE=false
    fi
done
if [ "@$DONE" == "@false" ]; then exit 1; fi

#---------------------------------------------------------------------
# Install Icedove.

echo `date` Step: Install Icedove

comp=`echo obj-*/dist/bin/components`               || exit 1
touch $comp/nsAbLDAPAutoCompleteSearch.js           || exit 1

mozilla/mach install || mozilla/mach install        || exit 1

# Replace an  absolute symlink with a relative symlink.  Additionally,
# confirm that the current program has been built and do a smoke-check
# on the symlink.

pushd $PRODDIR_ICEDOVE/bin                          || exit 1
ln -nsf ../lib/*-*/icedove-uxp .                    || exit 1
wc -c   icedove-uxp                                 || exit 1
ln -nsf icedove-uxp icedove                         || exit 1
popd                                                || exit 1

# Replace  a redundant copy of  "libxul.so"  with a  relative symlink.
# Additionally,  confirm  that  "libxul.so"  has been built  and  do a
# smoke-check on the symlink.

pushd $PRODDIR_ICEDOVE/lib/icedove-uxp-[0-9]*       || exit 1
ln -nsf ../*-devel-*/sdk/lib/libxul.so .            || exit 1
wc -c libxul.so                                     || exit 1
popd                                                || exit 1

#---------------------------------------------------------------------
# Install Icedove XPI extensions.

echo `date` Step: Install Icedove XPI extensions

# This part is application-specific.
L=$PRODDIR_ICEDOVE/lib
A=`ls $L | grep icedove- | grep -v devel`
XPI_DEST=$L/$A/extensions
XPI_SOURCE=$TOPDIR/xpi/icedove

if [ -d   $XPI_SOURCE ]; then
    mkdir -p                $XPI_DEST               || exit 1
    cp -a $XPI_SOURCE/*.xpi $XPI_DEST/              || exit 1
fi

#---------------------------------------------------------------------
# End of Icedove-UXP build.

echo `date` Step: End of Icedove-UXP build

#---------------------------------------------------------------------
# Set up AlbusLuna build.

echo `date` Step: Set up AlbusLuna build

THISTOP=$TOPDIR/albusluna                           || exit 1
cd $THISTOP                                         || exit 1

rm -fr configure old-configure                      || exit 1
rm -fr obj-*                                        || exit 1

#---------------------------------------------------------------------
# Create AlbusLuna ".mozconfig" file.

echo `date` Step: Create AlbusLuna ".mozconfig" file

cat > .mozconfig << END                             || exit 1
mk_add_options MOZ_MAKE_FLAGS="-j$NUMJOBS"
export MOZ_PKG_SPECIAL=gtk3

ac_add_options --enable-application=albusluna
ac_add_options --prefix=$PRODDIR_ALBUSLUNA
ac_add_options --with-app-basename=albusluna
ac_add_options --with-pthreads

# Branding
ac_add_options --enable-official-branding

# Sound
ac_add_options $SSALSAX
ac_add_options $SSPULSE

# Other disable
ac_add_options --disable-crashreporter
ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
ac_add_options --disable-gamepad
ac_add_options --disable-maintenance-service
ac_add_options --disable-necko-wifi
ac_add_options --disable-sandbox
ac_add_options --disable-tests
ac_add_options --disable-updater
ac_add_options --disable-webrtc

# Other enable
ac_add_options --enable-default-toolkit=cairo-gtk3
ac_add_options --enable-devtools
ac_add_options --enable-jemalloc
ac_add_options --enable-nspr-build
ac_add_options --enable-optimize="-O2 -w"
ac_add_options --enable-strip

# Misc.
END

#---------------------------------------------------------------------
# Adjust build settings.

# Note: Not all of the settings used here are confirmed to be support-
# ed.

if [ "@$LATRIPLET" \!= "@" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --target=$LACTRIPLET
END
fi

if [ "@$SKIP_DBUS"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-dbus
END
else
    cat >> .mozconfig << END                        || exit 1
ac_add_options --enable-dbus
END
fi

if [ "@$SKIP_LDAP"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-ldap
END
fi

if [ "@$USE_GTK2"   == "@true" ]; then
    sed -e "s@=cairo-gtk3@=cairo-gtk2@" \
        -i .mozconfig                               || exit 1
fi

if [ "@$LITEBUILD"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-system-cairo
ac_add_options --disable-system-pixman
END
fi

#---------------------------------------------------------------------
# Compile AlbusLuna.

echo `date` Step: Compile AlbusLuna

DONE=false
for attempt in 1 2
do
    if [ "@$DONE" == "@false" ]; then
        echo "albusluna attempt #$attempt"
        rm -fr obj-*/dist/bin/                      || exit 1
        DONE=true
        ./mach build || DONE=false
    fi
done
if [ "@$DONE" == "@false" ]; then exit 1; fi

#---------------------------------------------------------------------
# Install AlbusLuna.

echo `date` Step: Install AlbusLuna

comp=`echo obj-*/dist/bin/components`               || exit 1
touch $comp/nsAbLDAPAutoCompleteSearch.js           || exit 1

./mach install || ./mach install                    || exit 1

# Replace an  absolute symlink with a relative symlink.  Additionally,
# confirm that the current program has been built and do a smoke-check
# on the symlink.

pushd $PRODDIR_ALBUSLUNA/bin                        || exit 1
ln -nsf ../lib/*-*/albusluna .                      || exit 1
wc -c albusluna                                     || exit 1
popd                                                || exit 1

# Replace  a redundant copy of  "libxul.so"  with a  relative symlink.
# Additionally,  confirm  that  "libxul.so"  has been built  and  do a
# smoke-check on the symlink.

pushd $PRODDIR_ALBUSLUNA/lib/albusluna-[0-9]*       || exit 1
ln -nsf ../*-devel-*/sdk/lib/libxul.so .            || exit 1
wc -c libxul.so                                     || exit 1
popd                                                || exit 1

#---------------------------------------------------------------------
# Install AlbusLuna XPI extensions.

echo `date` Step: Install AlbusLuna XPI extensions

# This part is application-specific.
L=$PRODDIR_ALBUSLUNA/lib
A=`ls $L | grep albusluna- | grep -v devel`
XPI_DEST=$L/$A/browser/extensions
XPI_SOURCE=$TOPDIR/xpi/albusluna

if [ -d   $XPI_SOURCE ]; then
    mkdir -p                $XPI_DEST               || exit 1
    cp -a $XPI_SOURCE/*.xpi $XPI_DEST/              || exit 1
else
    echo Internal error 0619
    exit 1
fi

#---------------------------------------------------------------------
# End of AlbusLuna build.

echo `date` Step: End of AlbusLuna build

#---------------------------------------------------------------------
# Set up Ambassador build.

echo `date` Step: Set up Ambassador build

THISTOP=$TOPDIR/ambassador                          || exit 1
cd $THISTOP                                         || exit 1

rm -fr configure old-configure                      || exit 1
rm -fr obj-*                                        || exit 1

#---------------------------------------------------------------------
# Create Ambassador ".mozconfig" file.

echo `date` Step: Create Ambassador ".mozconfig" file

cat > .mozconfig << END                             || exit 1
mk_add_options MOZ_MAKE_FLAGS="-j$NUMJOBS"
export MOZ_PKG_SPECIAL=gtk3

ac_add_options --enable-application=ambassador
ac_add_options --prefix=$PRODDIR_AMBASSADOR
ac_add_options --with-app-basename=ambassador
ac_add_options --with-pthreads

# Branding
ac_add_options --disable-official-branding
ac_add_options --with-branding=ambassador/branding

# Sound
ac_add_options $SSALSAX
ac_add_options $SSPULSE

# Other disable
ac_add_options --disable-crashreporter
ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
ac_add_options --disable-gamepad
ac_add_options --disable-maintenance-service
ac_add_options --disable-necko-wifi
ac_add_options --disable-sandbox
ac_add_options --disable-tests
ac_add_options --disable-updater
ac_add_options --disable-webrtc

# Other enable
ac_add_options --enable-default-toolkit=cairo-gtk3
ac_add_options --enable-devtools
ac_add_options --enable-jemalloc
ac_add_options --enable-nspr-build
ac_add_options --enable-optimize="-O2 -w"
ac_add_options --enable-strip

# Misc.
END

#---------------------------------------------------------------------
# Adjust build settings.

# Note: Not all of the settings used here are confirmed to be support-
# ed.

if [ "@$LATRIPLET" \!= "@" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --target=$LACTRIPLET
END
fi

if [ "@$SKIP_DBUS"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-dbus
END
else
    cat >> .mozconfig << END                        || exit 1
ac_add_options --enable-dbus
END
fi

if [ "@$SKIP_LDAP"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-ldap
END
fi

if [ "@$USE_GTK2"   == "@true" ]; then
    sed -e "s@=cairo-gtk3@=cairo-gtk2@" \
        -i .mozconfig                               || exit 1
fi

if [ "@$LITEBUILD"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-system-cairo
ac_add_options --disable-system-pixman
END
fi

#---------------------------------------------------------------------
# Compile Ambassador.

echo `date` Step: Compile Ambassador

DONE=false
for attempt in 1 2
do
    if [ "@$DONE" == "@false" ]; then
        echo "ambassador attempt #$attempt"
        rm -fr obj-*/dist/bin/                      || exit 1
        DONE=true
        ./mach build || DONE=false
    fi
done
if [ "@$DONE" == "@false" ]; then exit 1; fi

#---------------------------------------------------------------------
# Install Ambassador.

echo `date` Step: Install Ambassador

comp=`echo obj-*/dist/bin/components`               || exit 1
touch $comp/nsAbLDAPAutoCompleteSearch.js           || exit 1

./mach install || ./mach install                    || exit 1

# Replace an  absolute symlink with a relative symlink.  Additionally,
# confirm that the current program has been built and do a smoke-check
# on the symlink.

pushd $PRODDIR_AMBASSADOR/bin                       || exit 1
ln -nsf ../lib/*-*/ambassador .                     || exit 1
wc -c ambassador                                    || exit 1
popd                                                || exit 1

# Replace  a redundant copy of  "libxul.so"  with a  relative symlink.
# Additionally,  confirm  that  "libxul.so"  has been built  and  do a
# smoke-check on the symlink.

pushd $PRODDIR_AMBASSADOR/lib/ambassador-[0-9]*     || exit 1
ln -nsf ../*-devel-*/sdk/lib/libxul.so .            || exit 1
wc -c libxul.so                                     || exit 1
popd                                                || exit 1

#---------------------------------------------------------------------
# Install Ambassador XPI extensions.

echo `date` Step: Install Ambassador XPI extensions

# This part is application-specific.
L=$PRODDIR_AMBASSADOR/lib
A=`ls $L | grep ambassador- | grep -v devel`
XPI_DEST=$L/$A/extensions
XPI_SOURCE=$TOPDIR/xpi/ambassador

if [ -d   $XPI_SOURCE ]; then
    mkdir -p                $XPI_DEST               || exit 1
    cp -a $XPI_SOURCE/*.xpi $XPI_DEST/              || exit 1
fi

#---------------------------------------------------------------------
# End of Ambassador build.

echo `date` Step: End of Ambassador build

#---------------------------------------------------------------------
# Set up Blue Gorilla build.

echo `date` Step: Set up Blue Gorilla build

THISTOP=$TOPDIR/bluegorilla                         || exit 1
cd $THISTOP                                         || exit 1

rm -fr configure old-configure                      || exit 1
rm -fr obj-*                                        || exit 1

rm -fr     mozilla                                  || exit 1
mv ../rest/mozilla .                                || exit 1

touch mozilla/.gdbinit                              || exit 1
touch mozilla/.ycm_extra_conf.py                    || exit 1

#---------------------------------------------------------------------
# Create Blue Gorilla ".mozconfig" file.

echo `date` Step: Create Blue Gorilla ".mozconfig" file

cat > .mozconfig << END                             || exit 1
mk_add_options MOZ_MAKE_FLAGS="-j$NUMJOBS"

ac_add_options --enable-application=suite
ac_add_options --prefix=$PRODDIR_BLUEGORILLA
ac_add_options --with-app-basename=bluegorilla
ac_add_options --with-pthreads

# Branding
ac_add_options --disable-official-branding
ac_add_options --with-branding=suite/branding/bluegorilla

# Sound
ac_add_options $SSALSAX
ac_add_options $SSPULSE

# Other disable
ac_add_options --disable-accessibility
ac_add_options --disable-cpp-rtti
ac_add_options --disable-crashreporter
ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
ac_add_options --disable-gconf
ac_add_options --disable-necko-wifi
ac_add_options --disable-negotiateauth
ac_add_options --disable-safe-browsing
ac_add_options --disable-sandbox
ac_add_options --disable-tests
ac_add_options --disable-updater
ac_add_options --disable-url-classifier
ac_add_options --disable-webrtc

# Other enable
ac_add_options --enable-calendar
ac_add_options --enable-classic-account-wizard
ac_add_options --enable-default-toolkit=cairo-gtk3
ac_add_options --enable-gio
ac_add_options --enable-nspr-build
ac_add_options --enable-optimize="-O2 -w"
ac_add_options --enable-strip

# Misc.
END

#---------------------------------------------------------------------
# Adjust build settings.

# Note: Not all of the settings used here are confirmed to be support-
# ed.

if [ "@$LATRIPLET" \!= "@" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --target=$LACTRIPLET
END
fi

if [ "@$SKIP_DBUS"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-dbus
END
else
    cat >> .mozconfig << END                        || exit 1
ac_add_options --enable-dbus
END
fi

if [ "@$SKIP_LDAP"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-ldap
END
fi

if [ "@$USE_GTK2"   == "@true" ]; then
    sed -e "s@=cairo-gtk3@=cairo-gtk2@" \
        -i .mozconfig                               || exit 1
fi

if [ "@$LITEBUILD"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-system-cairo
ac_add_options --disable-system-pixman
END
fi

#---------------------------------------------------------------------
# Compile Blue Gorilla.

echo `date` Step: Compile Blue Gorilla

DONE=false
for attempt in 1 2
do
    if [ "@$DONE" == "@false" ]; then
        echo "bluegorilla attempt #$attempt"
        rm -fr obj-*/dist/bin/                      || exit 1
        DONE=true
        ./mach build || DONE=false
    fi
done
if [ "@$DONE" == "@false" ]; then exit 1; fi

#---------------------------------------------------------------------
# Install Blue Gorilla.

echo `date` Step: Install Blue Gorilla

comp=`echo obj-*/dist/bin/components`               || exit 1
touch $comp/nsAbLDAPAutoCompleteSearch.js           || exit 1

./mach install || ./mach install                    || exit 1

# Replace an  absolute symlink with a relative symlink.  Additionally,
# confirm that the current program has been built and do a smoke-check
# on the symlink.

pushd $PRODDIR_BLUEGORILLA/bin                      || exit 1
ln -nsf ../lib/*-*/bluegorilla .                    || exit 1
wc -c bluegorilla                                   || exit 1
popd                                                || exit 1

# Replace  a redundant copy of  "libxul.so"  with a  relative symlink.
# Additionally,  confirm  that  "libxul.so"  has been built  and  do a
# smoke-check on the symlink.

# This  code also  creates a  required symlink to a copy of  a library
# file named "libhunspell.so".

pushd $PRODDIR_BLUEGORILLA/lib/bluegorilla-[0-9]*   || exit 1
ln -nsf ../*-devel-*/sdk/lib/libxul.so .            || exit 1
wc -c libxul.so                                     || exit 1

ln -nsf ../../../albusluna/lib/albusluna-[0-9]*/libhunspell.so . \
                                                    || exit 1
popd                                                || exit 1

#---------------------------------------------------------------------
# Install Blue Gorilla XPI extensions.

echo `date` Step: Install Blue Gorilla XPI extensions

# This part is application-specific.
L=$PRODDIR_BLUEGORILLA/lib
A=`ls $L | grep bluegorilla- | grep -v devel`
XPI_DEST=$L/$A/extensions
XPI_SOURCE=$TOPDIR/xpi/bluegorilla

if [ -d   $XPI_SOURCE ]; then
    mkdir -p                $XPI_DEST               || exit 1
    cp -a $XPI_SOURCE/*.xpi $XPI_DEST/              || exit 1
fi

#---------------------------------------------------------------------
# End of Blue Gorilla build.

echo `date` Step: End of Blue Gorilla build

#---------------------------------------------------------------------
# Set up Epyrus build.

echo `date` Step: Set up Epyrus build

THISTOP=$TOPDIR/epyrus                              || exit 1
cd $THISTOP                                         || exit 1

rm -fr configure old-configure                      || exit 1
rm -fr obj-*                                        || exit 1

rm -fr platform                                     || exit 1
ln -s ../albusluna/platform .                       || exit 1

#---------------------------------------------------------------------
# Create Epyrus ".mozconfig" file.

echo `date` Step: Create Epyrus ".mozconfig" file

cat > .mozconfig << END                             || exit 1
mk_add_options MOZ_MAKE_FLAGS="-j$NUMJOBS"
export MOZ_PKG_SPECIAL=gtk3

ac_add_options --enable-application=mail
ac_add_options --prefix=$PRODDIR_EPYRUS
ac_add_options --with-app-basename=epyrus
ac_add_options --with-pthreads

# Branding
ac_add_options --disable-official-branding
ac_add_options --with-branding=mail/branding/aurora 

# Sound
ac_add_options $SSALSAX
ac_add_options $SSPULSE

# Other disable
ac_add_options --disable-crashreporter
ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
ac_add_options --disable-devtools
ac_add_options --disable-devtools-server
ac_add_options --disable-gamepad
ac_add_options --disable-maintenance-service
ac_add_options --disable-necko-wifi
ac_add_options --disable-sandbox
ac_add_options --disable-tests
ac_add_options --disable-updater
ac_add_options --disable-webrtc

# Other enable
ac_add_options --enable-default-toolkit=cairo-gtk3
ac_add_options --enable-jemalloc
ac_add_options --enable-nspr-build
ac_add_options --enable-optimize="-O2 -w"
ac_add_options --enable-strip

# Misc.
END

#---------------------------------------------------------------------
# Adjust build settings.

# Note: Not all of the settings used here are confirmed to be support-
# ed.

if [ "@$LATRIPLET" \!= "@" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --target=$LACTRIPLET
END
fi

if [ "@$SKIP_DBUS"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-dbus
END
else
    cat >> .mozconfig << END                        || exit 1
ac_add_options --enable-dbus
END
fi

if [ "@$SKIP_LDAP"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-ldap
END
fi

if [ "@$USE_GTK2"   == "@true" ]; then
    sed -e "s@=cairo-gtk3@=cairo-gtk2@" \
        -i .mozconfig                               || exit 1
fi

if [ "@$LITEBUILD"  == "@true" ]; then
    cat >> .mozconfig << END                        || exit 1
ac_add_options --disable-system-cairo
ac_add_options --disable-system-pixman
END
fi

#---------------------------------------------------------------------
# Compile Epyrus.

echo `date` Step: Compile Epyrus
chmod 755 mach platform/mach

DONE=false
for attempt in 1 2
do
    if [ "@$DONE" == "@false" ]; then
        echo "epyrus attempt #$attempt"
        rm -fr obj-*/dist/bin/                      || exit 1
        DONE=true
        ./mach build || DONE=false
    fi
done
if [ "@$DONE" == "@false" ]; then exit 1; fi

#---------------------------------------------------------------------
# Install Epyrus.

echo `date` Step: Install Epyrus

comp=`echo obj-*/dist/bin/components`               || exit 1
touch $comp/nsAbLDAPAutoCompleteSearch.js           || exit 1

./mach install || ./mach install                    || exit 1

# Replace an  absolute symlink with a relative symlink.  Additionally,
# confirm that the current program has been built and do a smoke-check
# on the symlink.

pushd $PRODDIR_EPYRUS/bin                           || exit 1
ln -nsf ../lib/*-*/epyrus .                         || exit 1
wc -c epyrus                                        || exit 1
popd                                                || exit 1

# Replace  a redundant copy of  "libxul.so"  with a  relative symlink.
# Additionally,  confirm  that  "libxul.so"  has been built  and  do a
# smoke-check on the symlink.

pushd $PRODDIR_EPYRUS/lib/epyrus-[0-9]*             || exit 1
ln -nsf ../*-devel-*/sdk/lib/libxul.so .            || exit 1
wc -c libxul.so                                     || exit 1
popd                                                || exit 1

#---------------------------------------------------------------------
# Install Epyrus XPI extensions.

echo `date` Step: Install Epyrus XPI extensions

# This part is application-specific.
L=$PRODDIR_EPYRUS/lib
A=`ls $L | grep epyrus- | grep -v devel`
XPI_DEST=$L/$A/extensions
XPI_SOURCE=$TOPDIR/xpi/epyrus

if [ -d   $XPI_SOURCE ]; then
    mkdir -p                $XPI_DEST               || exit 1
    cp -a $XPI_SOURCE/*.xpi $XPI_DEST/              || exit 1
fi

#---------------------------------------------------------------------
# End of Epyrus build.

echo `date` Step: End of Epyrus build

#---------------------------------------------------------------------
# Add a unified "bin" directory.

if [ "@$ADDBINDIR" == "@true" ]; then
    cd $PREFIX                                      || exit 1
    mkdir -p bin                                    || exit 1
    cd       bin                                    || exit 1

    for pkg in \
        albusluna  ambassador  basilisk   bluegorilla \
        epyrus     icedove
    do
        ln -nsf ../$pkg/bin/$pkg .                  || exit 1
    done
fi

#---------------------------------------------------------------------
# End of file.
