#!/bin/bash
#  Copyright (C) 2010-2011  Matias A. Fonzo, <selk@dragora.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

CWD=$(pwd)

TMP=${TMP:-/tmp/sources}
OUT=${OUT:-/tmp/packages}

V=3.13.1
ARCH=${ARCH:-i486}
B=1

PKG=${TMP}/package-nss

rm -rf $PKG
mkdir -p $PKG $OUT

echo "Uncompressing the tarball..."
rm -rf ${TMP}/nss-${V}
lzip -cd ${CWD}/nss-${V}.tar.lz | tar -xvf - -C $TMP

cd ${TMP}/nss-${V}

# Set sane ownerships and permissions:
chown -R 0:0 .
find . \
 \( -perm 2777 -o \
    -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 {} +

pushd mozilla/security/nss/lib/ckfw/builtins
  make generate
popd

# Set the environment:
export BUILD_OPT=1
export PKG_CONFIG_ALLOW_SYSTEM_LIBS=1
export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1
export USE_SYSTEM_ZLIB=1
export ZLIB_LIBS=-lz
export NSPR_INCLUDE_DIR=$(/usr/bin/pkg-config --cflags-only-I nspr | sed 's/-I//')
export NSPR_LIB_DIR=$(/usr/bin/pkg-config --libs-only-L nspr | sed 's/-L//')

# Make the source:
make -C mozilla/security/coreconf
make -C mozilla/security/dbm
make -C mozilla/security/nss

# Install binaries, libraries and headers:
cd mozilla/dist/Linux?.?_*
  install -d ${PKG}/usr/{bin,lib}

  for file in certutil cmsutil crlutil modutil \
   pk12util signtool signver ssltap ; do
    cp -pL bin/${file} ${PKG}/usr/bin
  done

  for file in libsoftokn3 libfreebl3 libnss3 libnssutil3 \
   libssl3 libsmime3 libnssckbi libnssdbm3 ; do
    install -m 755 lib/${file}.so ${PKG}/usr/lib/
  done

  for file in libcrmf libnss* ; do
    install -m 644 lib/${file}.a ${PKG}/usr/lib/
  done

  ( cd ../private/nss
    for file in *.h ; do
      install -m 644 $file -D ${PKG}/usr/include/nss/private/${file}
    done
  )
cd -

# Strip binaries & libraries:
( cd $PKG
  find . -type f | xargs file | awk '/ELF/ && /executable/ || /shared object/' | \
   cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
strip --strip-debug ${PKG}/usr/lib/*.a

# Include our version of nss-config:
zcat ${CWD}/nss-config.gz > ${PKG}/usr/bin/nss-config
chmod 755 ${PKG}/usr/bin/nss-config

# Record the information according with the version of NSS:
MAJOR=$(awk '/VMAJOR/{ print $3 }' mozilla/security/nss/lib/nss/nss.h)
MINOR=$(awk '/VMINOR/{ print $3 }' mozilla/security/nss/lib/nss/nss.h)
PATCH=$(awk '/VPATCH/{ print $3 }' mozilla/security/nss/lib/nss/nss.h)
sed -i \
 -e "s|@MOD_MAJOR_VERSION@|$MAJOR|" \
 -e "s|@MOD_MINOR_VERSION@|$MINOR|" \
 -e "s|@MOD_PATCH_VERSION@|$PATCH|" \
  ${PKG}/usr/bin/nss-config

# Make the nss.pc for pkg-config:
install -d ${PKG}/usr/lib/pkgconfig

cat << EOF > ${PKG}/usr/lib/pkgconfig/nss.pc
prefix=/usr
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include/nss

Name: NSS
Description: Network Security Services
Version: $(sh ${PKG}/usr/bin/nss-config --version)
Requires: nspr >= 4.7.4
Libs: -L\${libdir} -lssl3 -lsmime3 -lnss3 -lnssutil3
Cflags: -I\${includedir}

EOF

# Copy the description files:
mkdir -p ${PKG}/description
cp ${CWD}/description/?? ${PKG}/description

cd $PKG
makepkg -l ${OUT}/nss-${V}-${ARCH}-${B}.tlz

