#!/bin/sh
# $Id: mozilla-nss.SlackBuild,v 1.2 2009/09/27 09:56:32 root Exp root $
# Copyright (c) 2005-2009  Eric Hameleers, Eindhoven, NL
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS 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.
# -----------------------------------------------------------------------------

PRGNAM=mozilla-nss
SRCNAM=nss
VERSION=${VERSION:-3.12.4}
NSPR=${NSPR:-4.8}

BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

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

set -e # Exit on most errors

# Prepare:
rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION-with-nspr-$NSPR.tar.gz
cd $SRCNAM-$VERSION-with-nspr-$NSPR
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Patch out "No rule to make target -lz.  Stop." error
patch -p1 < $CWD/signtool.zlib.diff

# Build the sources:
export LDFLAGS="$SLKLDFLAGS"
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export BUILD_OPT=1
export NSDISTMODE="copy"
export NSS_USE_SYSTEM_SQLITE=1
cd mozilla/security/nss
  make nss_build_all export
cd -

# Manually copy stuff into the package:
mkdir -p $PKG/usr/{bin,lib${LIBDIRSUFFIX},include/{nss,nspr}}
cd mozilla/dist/$(uname -s)*
  cp -pL bin/* $PKG/usr/bin/
  cp -pL lib/* $PKG/usr/lib${LIBDIRSUFFIX}/
  cp -rpL include/* $PKG/usr/include/nspr/
  cp -rpL ../public/nss/*.h $PKG/usr/include/nss/
  rm -f $PKG/usr/bin/*.so
  rm -f $PKG/usr/lib${LIBDIRSUFFIX}/*.a
cd -

# Add smime too:
cd mozilla/security/nss/cmd/smimetools
  cp -a smime $PKG/usr/bin/
  chmod 0755 $PKG/usr/bin/smime
  sed -i -e 's#/usr/local/bin#/usr/bin#g' $PKG/usr/bin/smime
cd -

# Provide pkg-config files:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
cat <<EOT > $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/mozilla-nss.pc
prefix=/usr
exec_prefix=\${prefix}
libdir=/usr/lib${LIBDIRSUFFIX}
includedir=\${prefix}/include/nss

Name: NSS
Description: Network Security Services
Version: $VERSION
Requires: nspr >= $NSPR sqlite3
Libs: -L\${libdir} -lnss3 -lsmime3 -lssl3 -lsoftokn3  -lnssutil3
Cflags: -I\${includedir}
EOT

cat <<EOT > $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/mozilla-nspr.pc
prefix=/usr
exec_prefix=\${prefix}
libdir=/usr/lib${LIBDIRSUFFIX}
includedir=\${prefix}/include/nspr

Name: NSPR
Description: The Netscape Portable Runtime
Version: $NSPR
Libs: -L\${libdir} -lplds4 -lplc4 -lnspr4
Cflags: -I\${includedir}
EOT

( cd $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
  ln -s mozilla-nspr.pc nspr.pc
  ln -s mozilla-nss.pc nss.pc
)

# Add documentation:
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  $CWD/MPL-1.1.txt $CWD/gpl-2.0.txt $CWD/lgpl-2.1.txt $CWD/faq.html \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chown -R root:root $PKG/usr/doc/$PRGNAM-$VERSION

# Strip binaries:
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Add a package description:
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-tgz}

