#!/bin/bash

# mozilla-firefox.SlackBuild
# by Stuart Winter
# Based on Unbranded 'Firefox' build script from Slackware 13.
# 03-April-2009

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Major version number - needed for /usr/lib/firefox dir:
MAJORVER=$VERSION
MOZVERS=${MOZVERS:-release}
RELEASEVER=$(echo $VERSION | cut -f 1 -d r)

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-D__ARM_PCS -march=armv5te" ## -O2 
            export LDFLAGS="-Wl,--no-as-needed"
            # Anything < armv7l needs:
            SLKCONFARGS="--disable-tracejit \
                         --disable-methodjit \
                         --with-cpu-arch=armv5te \
                         --with-arch=armv5te"
            # Other options that make sense:
#                    --with-system-libvpx=/tmp/pkg-libvpx \
            export SLKCONFARGS="$SLKCONFARGS \
                    --with-thumb=toolchain-default \
                    --with-endian=little \
                    --with-thumb-interwork=toolchain-default \
                    --with-fpu=toolchain-default \
                    --with-float-abi=toolchain-default \
                    --with-soft-float=toolchain-default \
                    --disable-thumb2 \
                    --disable-elf-dynstr-gc \
                    --disable-mochitest \
                    --disable-javaxpcom \
                    --disable-long-long-warning \
                    --disable-pedantic \
                    --disable-crashreporter \
                    --disable-elf-hack \
                    --enable-mathml \
                    --enable-gio \
                    --disable-strip \
                    --disable-install-strip \
                    --enable-system-ffi \
                    --host=$ARCH-slackware-linux-gnueabi \
                    --target=$ARCH-slackware-linux-gnueabi" 

            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac


# Extract source:
echo "Extracting source"
tar xf $CWD/firefox-$VERSION*source*tar.bz2
#tar xf $PORTCWD/sources/firefox-$VERSION*source*tar*
cd mozilla* || exit 1 
slackhousekeeping

# Apply patches from Gentoo:
#mkdir patches
#tar xvvf $PORTCWD/sources/firefox*patches* -C patches/
#for i in \
#  ; do
#  auto_apply_patch patches/firefox/$i || failpatch
#done

# Apply patches from Debian's patch set:
#tar xf $PORTCWD/sources/iceweasel*debian*xz -C.
#rm -f debian/patches/porting/Allow-ipc-code-to-build-on-GNU-Hurd.patch
#for pf in \
#  debian/patches/configure.patch \
#  debian/patches/fixes/* \
#  debian/patches/porting/* \
#  ; do auto_apply_patch $pf || exit 1
#done

# Fix a long standing bug that's prevented staying current on GTK+.
# Thanks to the BLFS folks.  :-)
cat << EOF >> layout/build/Makefile.in

ifdef MOZ_ENABLE_CANVAS
EXTRA_DSO_LDOPTS += \$(XLDFLAGS) -lX11 -lXrender
endif

EOF

# Work around a glitch where .py files go missing.
# Don't apply this to future versions...  we'll see how those go then.
if [ "$VERSION" = "13.0" ]; then
  zcat $CWD/firefox_cache_dir.patch.gz | patch -p1 --verbose || exit 1
fi

# Configure:
#autoconf2.13 || failconfig
# Turn errors back into warnings:
sed -i 's?-Werror?-Wall?g' configure

# Mozilla devs enforce using an objdir for building
# and launching configure with the absolute path
# https://developer.mozilla.org/en/Configuring_Build_Options#Building_with_an_objdir
ODIR=$PWD
mkdir obj
cd obj
# Force build tests to run as armv4 even though the build box might be >armv5.
export ARM_FORCE_PLATFORM=5
#
export MOZILLA_OFFICIAL="1" &&
export BUILD_OFFICIAL="1" &&
export MOZ_PHOENIX="1" &&
export CFLAGS="$SLKCFLAGS" &&
export CXXFLAGS="$SLKCFLAGS" &&
export MOZ_MAKE_FLAGS="$NUMJOBS" &&
$ODIR/configure $SLKCONFARGS \
  --enable-official-branding \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --with-default-mozilla-five-home=/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER \
  --with-system-zlib \
  --enable-application=browser \
  --enable-default-toolkit=cairo-gtk2 \
  --enable-startup-notification \
  --enable-crypto \
  --enable-svg \
  --enable-canvas \
  --enable-logging \
  --enable-webm \
  --enable-xft \
  --enable-xinerama \
  --enable-reorder \
  --enable-strip \
  --enable-cpp-rtti \
  --enable-single-profile \
  --disable-ldap \
  --disable-accessibility \
  --disable-debug \
  --disable-tests \
  --disable-pedantic \
  --disable-installer \
  --disable-mailnews \
  --disable-composer \
  --disable-profilesharing || exit 1
  # Complains about missing APNG support in Slackware's libpng:
  #--with-system-png \

  # won't build:
  #--enable-system-cairo \

# Build:
make MOZ_MAKE_FLAGS="$NUMJOBS" $NUMJOBS || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall
cd ..

# We don't need these (just symlinks anyway):
rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/firefox-devel-$RELEASEVER

# Nor these:
rm -rf $PKG/usr/include

( cd $PKG/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER
  #mv defaults/profile/mimeTypes.rdf defaults/profile/mimeTypes.rdf.orig || exit 1
  if [ -d defaults/profile ]; then
    zcat $CWD/mimeTypes.rdf > defaults/profile/mimeTypes.rdf || exit 1
  fi
  # OK, this patch is useless on 7.x.  We'll float without it and see what happens.
  # Perhaps it won't make a difference or should be worked around elsewhere.
  #zcat $CWD/firefox.moz_plugin_path.diff.gz \
  #  | sed -e "s#usr/lib#usr/lib${LIBDIRSUFFIX}#g" \
  #  | patch -p1 --verbose --backup --suffix=.orig || exit 1
  # Clean up if the above patch was successful:
  #rm -f firefox.orig
) || exit

mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/mozilla/plugins
mkdir -p $PKG/usr/share/applications
cat $CWD/mozilla-firefox.desktop > $PKG/usr/share/applications/mozilla-firefox.desktop
mkdir -p $PKG/usr/share/pixmaps
cat $CWD/firefox.png > $PKG/usr/share/pixmaps/firefox.png

# These files/directories are usually created if Firefox is run as root,
# which on many systems might (and possibly should) be never.  Therefore, if we
# don't see them we'll put stubs in place to prevent startup errors.
( cd $PKG/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER
  if [ -d extensions/talkback\@mozilla.org ]; then
    if [ ! -r extensions/talkback\@mozilla.org/chrome.manifest ]; then
      echo > extensions/talkback\@mozilla.org/chrome.manifest
    fi
  fi
  if [ ! -d updates ]; then
    mkdir -p updates/0
  fi
)

# Need some default icons in the right place:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER/chrome/icons/default
install -m 644 other-licenses/branding/firefox/default16.png \
  $PKG/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER/icons/
install -m 644 other-licenses/branding/firefox/default16.png \
  $PKG/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER/chrome/icons/default/
( cd $PKG/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER
  install -m 644 icons/{default,mozicon50}.xpm chrome/icons/default/
)

# Copy over the LICENSE
install -p -c -m 644 LICENSE $PKG/usr/lib${LIBDIRSUFFIX}/firefox-$RELEASEVER/

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Change the architecture of the package name since Firefox 10 and upwards
# do not support armv5:
export SLACKPACKAGE=$PKGNAM-$VERSION-armhfp-$BUILD.tgz

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG

~/armedslack/find-armv7

slackhlinks     # search for any hard links
