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

NAME="nspr"
VERSION="4.8.2"
REVISION="2"

PKG_SOURCEURL='http://ftp.mozilla.org/pub/mozilla.org/$NAME/releases/v$VERSION/src/$NAME-$VERSION.tar.gz'

PKG_PATCH[0]="nspr32.pc"
PKG_PATCH[1]="nspr64.pc"

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


cflags
cxxflags
prepare
analyzesource

PKG_BUILDROOT=$PKG_BUILDROOT/mozilla/nsprpub

# Lets check on which arch we build and prepare the system correctly.

createdir "$PKG_STAGEROOT$DLG_LIBDIR/pkgconfig/"

if [ "$DLG_ARCH" == "x86-64" ]; then
	export LIB_DIR="$PKG_STAGEROOT/usr/lib64"
	spewdo "cp $PKG_COREREPOS/nspr64.pc $PKG_STAGEROOT$DLG_LIBDIR/pkgconfig/nspr.pc"
	spewdo "cd $PKG_BUILDROOT"
	spewdo "sed -i 's%libdirs=-L$libdir%libdirs=\"-Wl,-rpath,$libdir -L$libdir\"%' ./config/nspr-config.in"
	export LDFLAGS="-Wl,-rpath,/usr/lib64/nspr"
	unset PKG_LIB64DIR
	configure --enable-64bit --libdir=/usr/lib64/nspr \
		  --infodir=/usr/share/info --disable-debug --enable-system-sqlite 
else
	export LIB_DIR="$PKG_STAGEROOT/usr/lib"
	spewdo "cp $PKG_COREREPOS/nspr32.pc $PKG_STAGEROOT$DLG_LIBDIR/pkgconfig/nspr.pc"
	spewdo "cd $PKG_BUILDROOT"
	spewdo "sed -i 's%libdirs=-L$libdir%libdirs=\"-Wl,-rpath,$libdir -L$libdir\"%' ./config/nspr-config.in"
	export LDFLAGS="-Wl,-rpath,/usr/lib/nspr"
	unset PKG_LIBDIR
	configure --libdir=/usr/lib/nspr \
		  --infodir=/usr/share/info --disable-debug --enable-system-sqlite 
fi

make
makeinstall

# This patch is based on the build instruction by CBLFS 
# http://cblfs.cross-lfs.org/index.php/NSPR 
spew 3 "Adjusting pkg-config file" 
NSPR_LIBS=$($PKG_STAGEROOT/usr/bin/nspr-config --libs) 
NSPR_CFLAGS=$($PKG_STAGEROOT/usr/bin/nspr-config --cflags) 
NSPR_VERSION=$($PKG_STAGEROOT/usr/bin/nspr-config --version) 
	 
sed -i -e "s@%NSPR_VERSION%@${NSPR_VERSION}@" \
	   -e "s@%FULL_NSPR_LIBS%@${NSPR_LIBS}@" \
       -e "s@%FULL_NSPR_CFLAGS%@${NSPR_CFLAGS}@" $PKG_STAGEROOT$DLG_LIBDIR/pkgconfig/nspr.pc 
 	

cd $PKG_STAGEROOT$DLG_LIBDIR/nspr
for lib in *
do
  NEW_LIB="$(echo $lib | sed 's/lib//')"
  spewdo "ln -sf $lib $NEW_LIB"
done
tidydocumentation
analyzebinaries
finalizepackage
packagize
cleanup

