# Maintainer: PktSurf <smlinux@pktsurf.in>
app=mozilla-nss
version=3.87
nsprversion=4.35
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//')"

prepbuilddir() {
  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
}

build() {
  # 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/

  mkfinalpkg
}

sha512sums="
3f7b322e1b67cc94b2c807b38eb2ce46a0e1a61db755b553a5ba88965226e5db4c79fde5a4396d2740a9e58f1199c40e33cccae42d99b750629a54e190d67e45  nspr-4.35.tar.lz
820b7a4f1853698a8df1bb78a60f956fa5d698cced74527abd7fb1c9b57352f03191668c08403b2cde4597aa20b07277cd63ebf9125d652109f61954520bbdf0  nss-3.87.tar.lz
"
