#!/bin/bash

# arm/build
# Check package dependencies, set metadata and launch
# package build script.
# by Stuart Winter <mozes@slackware.com>
#
source /usr/share/slackdev/buildkit.sh

# Package metadata:
export PKGNAM=mozilla-firefox
export VERSION=${VERSION:-140.10.0esr}
export BUILD=${BUILD:-1}
slack_var_pkgseries

# If building in /patches for a Stable Release, we drop the packages into the
# '/patches' directory within the configured Slackware tree:
slack_findpkgstore_is_stablerelease && {
    export PKGSTORE=$PORTPATCHPKGS
    export PKGSERIES=/ # unset as it's not required for patches
    export BUILD=1_slack15.1
    mkdir -vpm755 $PKGSTORE/$PKGSERIES ;}

# Set package name:
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

# For patches, you need to edit the location at which we can find
# autoconf.
#
## ******************************************************************* ##
# For test packages - best to store in another location rather than
# overwriting the working copy in the main tree:
# export PKGSTORE=/tmp/
# mkdir -vpm755 $PKGSTORE/$PKGSERIES
## ******************************************************************* ##

# Ensure base ARM packages are installed first:
slackbasedeps

# Ensure $PKGNAM isn't already installed:
slackfailpkgdeps $PKGNAM || removepkg $PKGNAM

# Firefox will link against these if installed during build time, but I'd rather make them independent:
removepkg mozilla-thunderbird seamonkey

# We also need autoconf-2.13 from the extra series.
# directory.  This is because the newer version of gtk+2-2.12.7
# causes the build to explode.  So we need to regenerate the autoconf
# stuff - which needs an older autoconf.
removepkg autoconf
# This isn't exactly portable outside of my own build environment
# but honestly, is anybody else actually using ARMedslack's build system?
# If so, send me a post card! (or plane tickets to an exotic
# island with dancing girls and fine wines!  :-)
# You must install the packages in this order:
upgradepkg --install-new $PORTSRC/extra/autoconf213/autoconf*t?z
#upgradepkg --install-new ~/armedslack/slackwarearm-current/extra/autoconf213/*z

# Ensure specific build dependencies (mainly libraries):

# Is a build time dependency - rust needs it.
{ slackcheckpkgdeps llvm            || installpkg $PKGSTORE/d/llvm-[0-9]*.t?z   || exit 99; }
# Build-time dep:
{ slackcheckpkgdeps nodejs          || installpkg $PKGSTORE/l/nodejs-[0-9]*.t?z   || exit 99; }
#
# Just Slackware ARM does this:
{ slackcheckpkgdeps libffi      || installpkg $PKGSTORE/l/libffi-*.t?z || exit 99 ; }

# Let's build only against mozilla-nss rather than anything else in the mozilla packages:
removepkg seamonkey seamonkey-solibs mozilla-thunderbird
{ slackcheckpkgdeps mozilla-nss     || installpkg $PKGSTORE/l/mozilla-nss-[0-9]*.t?z || exit 99; }

# We need an older version of Rust that we store in /extra:
#removepkg rust
#upgradepkg --install-new $PORTSRC/extra/rust-for-mozilla/rust-*t?z

# I know.  This is an awful hack but the libffi inside the GCC sources
# doesn't create a pkg-config file, and I'm not going to create another
# package from the independant libffi sources (that does create a .pc).
# We only need this file at build time.
#cat << EOF > /usr/share/pkgconfig/libffi.pc
#prefix=/usr/
#exec_prefix=${prefix}
#libdir=${exec_prefix}/lib
#includedir=$( ls -d /usr/lib/gcc/arm-slackware-linux-gnueabi/*/include )
#
#Name: libffi
#Description: Library supporting Foreign Function Interfaces
#Version: 3.0.9
#Libs: -lffi
#Cflags:
#EOF

# Overwrite the automatic NUMJOBS settings from the build system, since the
# amount of cores supplied causes firefox to segfault during the build due to RAM
# exhaustion!
#export NUMJOBS="-j1"

# Use the 'gold' ld linker due to RAM issues:
# There are other ways to achieve this, but none of them seem to last - the setting
# doesn't get filtered through the entire build.  Therefore, we'll force it
# by linking ld directly to ld.gold.
#ln -vfs /usr/bin/ld.gold /usr/bin/ld

# Launch the package build script:
BUILDLOG=$SLKPORTARCH-$( basename $SLACKPACKAGE .t?z ).build.log
( ./$PKGNAM.SlackBuild ) >& /dev/stdout | tee $BUILDLOG

# Compress the build log:
bzip2 -9fvz $BUILDLOG

# Clean up the hack:
#rm -f /usr/share/pkgconfig/libffi.pc

# Switch back to the normal autoconf:
removepkg autoconf
{ slackcheckpkgdeps autoconf || installpkg $PKGSTORE/d/autoconf-[0-9]*.t?z || exit 99; }

# Revert to the default linker:
ln -vfs /usr/bin/ld.bfd /usr/bin/ld

# Revert to 'rust' within the main tree:
#removepkg rust
#{ slackcheckpkgdeps rust || installpkg $PKGSTORE/d/rust-[0-9]*.t?z || exit 99; }
