#!/bin/sh

# Heavily based on the Slackware64 13.1 SlackBuild
# Packager Andrea Sciucca  ( gohanz at infinito.it )
# Last build from anycolouryoulike ( www.slacky.eu )
 
# Slackware build script for XULRunner
# Official Site: https://developer.mozilla.org/en/XULRunner

# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Required to build:
# jdk

# Exit the script on errors
set -e
trap 'echo "$0 FAILED at line ${LINENO}"' ERR

# Catching variables
PKGNAME=${PKGNAME:-xulrunner}
VERSION=${VERSION:-1.9.1.16}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}
ARCH=${ARCH:-x86_64}
SOURCE="http://releases.mozilla.org/pub/mozilla.org/$PKGNAME/releases/$VERSION/source/$PKGNAME-$VERSION.source.tar.bz2"
JOBS="-j4"

CWD=${CWD:-`pwd`}
TMP=${TMP:-/tmp/buildpkgs/$PKGNAME}
PKG=${PKG:-$TMP/package-$PKGNAME}
OUTPUT=${OUTPUT:-$CWD}

CHOST="i486"
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  SLKLDFLAGS=""
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  SLKLDFLAGS=""
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  SLKLDFLAGS=""
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  SLKLDFLAGS="-L/usr/lib64"
  LIBDIRSUFFIX="64"
  CHOST="x86_64"
fi

# Source file availability
if [ ! -e $PKGNAME-$VERSION.source.tar.bz2 ]; then
  wget -c $SOURCE
fi

# Create working directories
rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

# Package building
cd $TMP
tar xvjf $CWD/$PKGNAME-$VERSION.source.tar.bz2
cd mozilla-*

chmod -R u+w,go+r-w,a-s .
chown -R root:root .

echo -e "\E[0;32m+----------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild xulrunner |\E[0;0m"
echo -e "\E[0;32m+----------------------------+\E[0;0m"

# Add /usr/lib/mozilla/plugins to MOZ_PLUGINS_PATH:
zcat $CWD/$PKGNAME.diff.gz | patch -p0 -E --backup --verbose

LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --with-default-mozilla-five-home=/usr/lib${LIBDIRSUFFIX}/$PKGNAME-$VERSION \
  --enable-shared \
  --enable-application=xulrunner \
  --enable-optimize="$CFLAGS" \
  --enable-javaxpcom \
  --enable-strip \
  --enable-svg \
  --enable-svg-renderer=cairo \
  --enable-canvas \
  --enable-default-toolkit=cairo-gtk2 \
  --enable-xft \
  --enable-pango \
  --enable-extensions=default \
  --enable-image-encoder=all \
  --enable-safe-browsing \
  --enable-xprint \
  --enable-xinerama \
  --enable-crypto \
  --enable-system-cairo \
  --enable-system-sqlite \
  --enable-system-lcms \
  --enable-system-hunspell \
  --disable-installer \
  --disable-updater \
  --disable-mailnews \
  --disable-crashreporter \
  --disable-debug \
  --disable-mochitest \
  --disable-pedantic \
  --disable-tests \
  --with-system-zlib \
  --with-system-jpeg \
  --without-system-png \
  --with-system-mng \
  --with-system-bz2 \
  --enable-nspr-autoconf \
  --without-system-nspr \
  --with-pthreads \
  --with-x \
  --x-includes=/usr/X11/include \
  --x-libraries=/usr/X11/lib \
  --program-prefix= \
  --program-suffix= \
  --build=$CHOST-slackware-linux
make $JOBS
make install DESTDIR=$PKG

# Make sure gre configuration files do not overwrite one another on multilib,
# the filename is not important, all *.conf files in the directory are parsed
GECKOVERS=$(./config/milestone.pl --topsrcdir=.)
mv $PKG/etc/gre.d/${GECKOVERS}.system.conf $PKG/etc/gre.d/gre${LIBDIRSUFFIX}.conf.new

# Add missing xulrunner pkg-config files
( cd $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
  for FILE in gtkmozembed-embedding gtkmozembed js nspr nss plugin xpcom ; do
    ln -sf mozilla-${FILE}.pc xulrunner-${FILE}.pc
  done
)

# Add links to the dynamic libraries in the SDK directory
( cd $PKG/usr/lib${LIBDIRSUFFIX}/xulrunner-${VERSION}
  for i in *.so; do
    ln -sf /usr/lib${LIBDIRSUFFIX}/xulrunner-${VERSION}/$i \
      $PKG/usr/lib${LIBDIRSUFFIX}/xulrunner-devel-${VERSION}/sdk/lib/$i
  done
)

# Make symlinks to prevent version information from entering dependencies
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  ln -sf xulrunner-${VERSION} xulrunner
  ln -sf xulrunner-devel-${VERSION} xulrunner-devel
)

# Create plugin-path.
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/mozilla/plugins

# Install nss headers.
# On newer releases of Slackware this is necessary if Seamonkey has been
# uninstalled. In this case evolution-data-server will fail to build.
mkdir -p $PKG/usr/include/$PKGNAME-$VERSION/nss
find security/nss/lib -name "*.h" -type f -exec cp -a {} $PKG/usr/include/$PKGNAME-$VERSION/nss \;
chown -R root:root $PKG/usr/include/$PKGNAME-$VERSION/nss
chmod 644 $PKG/usr/include/$PKGNAME-$VERSION/nss/*

# Adding documentation
mkdir -p $PKG/usr/doc/$PKGNAME-$VERSION
cp -a \
  LEGAL LICENSE README.txt \
  $PKG/usr/doc/$PKGNAME-$VERSION
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$VERSION/$PKGNAME.SlackBuild
cat $CWD/slack-desc > $PKG/usr/doc/$PKGNAME-$VERSION/slack-desc

# Stripping binaries
cd $PKG
chown -R root:root $PKG

find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true

# Adding slack-desc, doinst.sh and slack-required
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat <<EOT >> $PKG/install/doinst.sh

config etc/gre.d/gre${LIBDIRSUFFIX}.conf.new

# Ensure that xulrunner libraries are found by applications that need them
# (this way you can remove seamonkey completely):
if ! grep -q /usr/lib${LIBDIRSUFFIX}/xulrunner etc/ld.so.conf ; then
  echo "/usr/lib${LIBDIRSUFFIX}/xulrunner" >> etc/ld.so.conf
fi

EOT

if [ -x "$(which requiredbuilder 2>/dev/null)" ];then
  requiredbuilder -y -v -s $CWD $PKG
fi

# Make the package
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

# Clean up the extra stuff
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP $PKG
fi
