APP=mozilla-nss
VERSION=3.73.1
NSPRVERSION=4.32
BUILD=1sml
HOMEPAGE='https://mozilla.org'
DESC="set of cryptographic libraries from Mozilla for use in security-focused applications"
REQUIRES="gyp"

NJOBS="(echo $MAKEFLAGS | sed 's/-j//')"

build() {
  mkandenterbuilddir
  rm -rf nss-$VERSION
  rm -rf nspr-*

  tar xf $SRCDIR/nss-$VERSION.tar.?z*
  cd nss-$VERSION
  tar xf $SRCDIR/nspr-$NSPRVERSION.tar.?z*
  mv nspr*/nspr .
  fixbuilddirpermissions

  # NSS doesn't like our ARM64-specific optimization C/CXXFLAGS
  if [ "$ARCH" == "aarch64" ] ; then
    unset CFLAGS CXXFLAGS
  fi

  # Disable Werror
  export NSS_ENABLE_WERROR=0

  # Disable deprecated ciphers
  export NSS_DISABLE_DEPRECATED_SEED=1
  export NSS_DISABLE_DEPRECATED_RC2=1

  cd nss
  ./build.sh -v -j 8 --opt --system-sqlite --enable-libpkix --disable-tests
  cd -

  # Install all the needed stuff to the package directory
  mkdir -p $PKG/{bin,lib,include/{nss,nspr}}
  cd dist/Release
  cp -pL bin/{certutil,cmsutil,crlutil,modutil,pk12util,shlibsign,signtool,signver,ssltap} $PKG/bin/
  cp -pL lib/*.{so,a} $PKG/lib/ 
  chmod 755 $PKG/lib/*.so*
  cp -rL include/* $PKG/include/
  cp -rpL ../public/nss/*.h $PKG/include/nss/

  cd -

  cd nss/cmd/smimetools
  install -m 755 smime $PKG/bin/smime
  sed -i -e 's#/usr/local/bin#/bin#g' $PKG/bin/smime
  cd -

  # Install nspr-config
  cat nspr/Release/config/nspr-config | sed -e "s,^prefix=.*$,prefix=,g" > $PKG/bin/nspr-config
  chmod 755 $PKG/bin/nspr-config

  # Install nss-config
  sed -e "s,@prefix@,,g" \
   -e "s,@MOD_MAJOR_VERSION@,$(printf $VERSION | cut -d. -f1),g" \
   -e "s,@MOD_MAJOR_VERSION@,$(printf $VERSION | cut -d. -f2),g" \
   -e "s,@MOD_MAJOR_VERSION@,$(printf $VERSION | cut -d. -f3),g" \
   $SRCDIR/nss-config.in > $PKG/bin/nss-config
  chmod 755 $PKG/bin/nss-config
    
  # Provide pkg-config files
  mkdir -p $PKG/lib/pkgconfig
  cat <<EOT > $PKG/lib/pkgconfig/mozilla-nss.pc
prefix=
exec_prefix=\${prefix}
libdir=/lib
includedir=\${prefix}/include/nss

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

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

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

  # Create pkgconfig symlinks
  ( 
    cd $PKG/lib/pkgconfig 
    ln -s mozilla-nspr.pc nspr.pc
    ln -s mozilla-nss.pc nss.pc
  )

  cp -a $SRCDIR/{MPL-1.1.txt,gpl-2.0.txt,lgpl-2.1.txt} $PKGDOCS/
  removestaticlibs

  mkfinalpkg
}

SHA512SUMS="
f9d9f04f2ae95d569d100561db57b00c2ecc305ddff89698cb6c82352e5a279354546d4df12598bdac1b619bedfdb3b491120a364b959a591a2fbd3f7e40a705  nspr-4.32.tar.lz
3987f82d8d75eb18721ba94cd86261fbf3f61d2d21687fc975d9753b5b722e597ce6f7a5aabd34128b087ddee6aa3691a647f916c2227fd3d783683f4971a814  nss-3.73.1.tar.lz
"
