#!/bin/sh
#
# Copyright 2009 Diffie
# Available under the terms of the GNU GPL.
#

NAME="nss"
VERSION="3.12.5"
REVISION="1"

PKG_SOURCEURL='http://ftp.mozilla.org/pub/mozilla.org/security/$NAME/releases/NSS_${VER_MAJ}_${VER_MIN}_${VER_MIC}_RTM/src/$NAME-$VERSION.tar.gz'

PKG_PATCH[0]="nss-nspr-location-32bit-libs.patch"
PKG_PATCH[1]="nss.pc"
PKG_PATCH[2]="nss-nspr-location-64bit-libs.patch"
PKG_PATCH[3]="nss-system-zlib-no-target-fix-64bit.patch"

. $DLG_ROOT/config
. $DLG_ROOT/SCRIPTS/dropline-functions.sh

cflags 
cxxflags
prepare
analyzesource

if [ "$DLG_ARCH" == "x86-64" ]; then
	applypatch 2 1
	applypatch 3 1
	export LIB_DIR="/usr/lib64"
	x64_BUILD="USE_64=1"
	spewdo "cd $PKG_SRCROOT"
	# This sed and LDFLAGS lines comes from cblfs-cross-lfs.org
	spewdo "sed -i 's@\$(MKSHLIB) -o@\$(MKSHLIB) \$(LDFLAGS) -o@g' ./mozilla/security/coreconf/rules.mk"
	export LDFLAGS="-Wl,-rpath,/usr/lib64/nss -Wl,-rpath,/usr/lib64/nspr"
else
	applypatch 0 0
	export LIB_DIR="/usr/lib"
	spewdo "cd $PKG_SRCROOT"
	# This sed and LDFLAGS lines comes from cblfs-cross-lfs.org
	spewdo "sed -i 's@\$(MKSHLIB) -o@\$(MKSHLIB) \$(LDFLAGS) -o@g' ./mozilla/security/coreconf/rules.mk"
	export LDFLAGS="-Wl,-rpath,/usr/lib/nss -Wl,-rpath,/usr/lib/nspr"
fi

# Let's make sure the package doesn't use its builting sqlite3 nor zlib. It can lead
# to bad things when compiling other packages.
spewdo "make -j 1 -C ./mozilla/security/coreconf USE_SYSTEM_ZLIB=1 ${x64_BUILD}|| return 1"
spewdo "make -j 1 -C ./mozilla/security/dbm ${x64_BUILD}|| return 1"
spewdo "make -j 1 -C ./mozilla/security/nss NSS_USE_SYSTEM_SQLITE=1 USE_SYSTEM_ZLIB=1 ${x64_BUILD} || return 1"

# There's no "make install" target for this pack so we will have
# to do things by hand... great...

# The installation files are thrown under a directory whose
# name depends on the arch, gcc and all that stuff, so lets
# use a variable to make the script more portable.
DIST_DIRECTORY="$(ls -d $PKG_WORKROOT/$NAME-$VERSION/mozilla/dist/Linux*)"

# Now the actual installation! (roughly based on the fedore spec)
createdir $PKG_STAGEROOT/usr/bin
install -m755 $DIST_DIRECTORY/bin/{cmsutil,crlutil,modutil,pk12util,signtool,signver,ssltap} $PKG_STAGEROOT/usr/bin

createdir $PKG_STAGEROOT$LIB_DIR
install -m644 $DIST_DIRECTORY/lib/*.chk $PKG_STAGEROOT$LIB_DIR
install -m644 $DIST_DIRECTORY/lib/{libcrmf.a,libnssb.a,libnssckfw.a} $PKG_STAGEROOT$LIB_DIR
install -m755 $DIST_DIRECTORY/lib/{libsoftokn3.so,libfreebl3.so,libnss3.so,libnssutil3.so,libssl3.so,libsmime3.so,libnssckbi.so,libnsspem.so,libnssdbm3.so} $PKG_STAGEROOT$LIB_DIR

createdir $PKG_STAGEROOT/usr/include/nss
install -m644 $PKG_WORKROOT/$NAME-$VERSION/mozilla/dist/public/nss/* $PKG_STAGEROOT/usr/include/nss

# This patch is based on the build instruction by CBLFS
# http://cblfs.cross-lfs.org/index.php/NSS
createdir "$PKG_STAGEROOT$LIB_DIR/pkgconfig"
spewdo "cp $PKG_COREREPOS/nss.pc $PKG_STAGEROOT$LIB_DIR/pkgconfig"
spew 3 "Adjusting pkg-config file"
sed -i -e "s@%LIB_LOCATION%@${LIB_DIR}@" $PKG_STAGEROOT$LIB_DIR/pkgconfig/nss.pc
     
tidydocumentation
analyzebinaries
finalizepackage
packagize
cleanup

