# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=nss
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.52.1
_nsprver=4.25
pkgrel=1
pkgdesc="Mozilla Network Security Services (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS"
license=(MPL2)
depends=("${MINGW_PACKAGE_PREFIX}-nspr"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "zip" "perl")
options=('staticlibs' 'strip' '!emptydirs')
source=(https://ftp.mozilla.org/pub/security/nss/releases/NSS_${pkgver//./_}_RTM/src/${_realname}-${pkgver}.tar.gz
        https://ftp.mozilla.org/pub/mozilla.org/mozilla/libraries/win32/moztools-static.zip
        nss.pc.in
        nss-config.in
        ssl-renegotiate-transitional.patch
        nss-build.patch
        manifest
        blank-cert8.db
        blank-key3.db
        blank-secmod.db
        nss-3.20.1-headers.patch)
sha256sums=('bcc81ac33aeb4ecad182dc21e34d2c97bcc148fd7b9e76f85bebb892405a9278'
            '1894bc68a0badd6e6f68f66abc4c6cd8e222791dd194f6b631ce536011ab6707'
            'b9f1428ca2305bf30b109507ff335fa00bce5a7ce0434b50acd26ad7c47dd5bd'
            'e44ac5095b4d88f24ec7b2e6a9f1581560bd3ad41a3d198596d67ef22f67adb9'
            '12df04bccbf674db1eef7a519a28987927b5e9c107b1dc386686f05e64f49a97'
            'a6891594ca65fc2d5ba18e82e4b6364a96538974314942f45fbeb917b2948f57'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855'
            'e45105a21696a26c834cfaa3f664c42426c99546094e22fbe3a5e1dd3fbc1f33'
            '6115cab6d646a05dd6b63e21c488da6bc36975f6e5ad8d6371c30a166c41cddc'
            '3790a5404f6b7edb652544eb75bfaa8f1c515f41ef14369248151d7c52cd249e'
            'f9d9b858a87c49c3ba1f058279b885fe704e23d9bcaaba00f17e534f6a313822')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  # Adds transitional SSL renegotiate support - patch from Debian
  # patch -Np3 -i ${srcdir}/ssl-renegotiate-transitional.patch
  patch -Np1 -i ${srcdir}/nss-build.patch
  patch -Np1 -i ${srcdir}/nss-3.20.1-headers.patch
  # Respect LDFLAGS
  sed -e 's/\$(MKSHLIB) -o/\$(MKSHLIB) \$(LDFLAGS) -o/' \
      -i nss/coreconf/rules.mk

  cp ${srcdir}/moztools/bin/nsinstall.exe ${srcdir}/${_realname}-${pkgver}/
  cp ${srcdir}/manifest ${srcdir}/${_realname}-${pkgver}/nsinstall.exe.manifest
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  cp -rf ${srcdir}/${_realname}-${pkgver}/* "${srcdir}"/build-${CARCH}

  export BUILD_OPT=1
  export NSS_NO_SSL2_NO_EXPORT=1
  export NSS_USE_SYSTEM_SQLITE=1
  export IMPORT_LIB_SUFFIX=.dll.a
  export NSS_DISABLE_GTESTS=1
  export NSS_ENABLE_ECC=1
  export NSS_DISABLE_DBM=1
  export NSPR_INCLUDE_DIR="`nspr-config --includedir`"
  export NSPR_LIB_DIR="`nspr-config --libdir`"
  export SQLITE_LIB_DIR="${MINGW_PREFIX}/lib"
  export XCFLAGS="${CFLAGS}"
  export EXTRA_SHARED_LIBS="-lplc4 -lplds4 -lnspr4 -lz -lcrypt32 -lws2_32"
  local conf64=
  [[ "$CARCH" = "x86_64" ]] && {
    conf64="USE_64=1"
  }

  make -j1 -C nss OS_TARGET=WINNT OS_RELEASE=5.0 XP_WIN=1 \
    NSINSTALL="${srcdir}"/build-${CARCH}/nsinstall NS_USE_GCC=1 ${conf64} \
    NSS_USE_SYSTEM_SQLITE=1 USE_SYSTEM_ZLIB=1 ZLIB_LIBS=${MINGW_PREFIX}/lib/libz.dll.a \
    CC=gcc RC=windres RANLIB=ranlib
}

package() {
  cd "${srcdir}"/build-${CARCH}
  install -d "${pkgdir}${MINGW_PREFIX}"/{bin,include/nss3,lib/{nss,pkgconfig}}
  NSS_VMAJOR=$(grep '#define.*NSS_VMAJOR' nss/lib/nss/nss.h | awk '{print $3}')
  NSS_VMINOR=$(grep '#define.*NSS_VMINOR' nss/lib/nss/nss.h | awk '{print $3}')
  NSS_VPATCH=$(grep '#define.*NSS_VPATCH' nss/lib/nss/nss.h | awk '{print $3}')

  sed ../nss.pc.in \
    -e "s,%libdir%,${MINGW_PREFIX}/lib,g" \
    -e "s,%prefix%,${MINGW_PREFIX},g" \
    -e "s,%exec_prefix%,${MINGW_PREFIX}/bin,g" \
    -e "s,%includedir%,${MINGW_PREFIX}/include/nss3,g" \
    -e "s,%NSPR_VERSION%,${_nsprver},g" \
    -e "s,%NSS_VERSION%,${pkgver},g" \
    > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/nss.pc"
  cp -f ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/nss.pc ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/mozilla-nss.pc

  sed ../nss-config.in \
    -e "s,@libdir@,${MINGW_PREFIX}/lib,g" \
    -e "s,@prefix@,${MINGW_PREFIX}/bin,g" \
    -e "s,@exec_prefix@,${MINGW_PREFIX}/bin,g" \
    -e "s,@includedir@,${MINGW_PREFIX}/include/nss3,g" \
    -e "s,@MOD_MAJOR_VERSION@,${NSS_VMAJOR},g" \
    -e "s,@MOD_MINOR_VERSION@,${NSS_VMINOR},g" \
    -e "s,@MOD_PATCH_VERSION@,${NSS_VPATCH},g" \
    > "${pkgdir}${MINGW_PREFIX}/bin/nss-config"
  chmod 755 ${pkgdir}${MINGW_PREFIX}/bin/nss-config

  # Copy the binary libraries we want
  for file in softokn nss nssutil ssl smime  #nssdbm
  do
    install -m 755 dist/*.OBJ/lib/${file}3.dll ${pkgdir}${MINGW_PREFIX}/bin/
    install -m 644 dist/*.OBJ/lib/lib${file}3.dll.a ${pkgdir}${MINGW_PREFIX}/lib/lib${file}3.dll.a
  done

  install -m 755 dist/*.OBJ/lib/nssckbi.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install -m 755 dist/*.OBJ/lib/freebl3.dll ${pkgdir}${MINGW_PREFIX}/bin/

  # Install the empty NSS db files
  mkdir -p ${pkgdir}${MINGW_PREFIX}/etc/pki/nssdb
  install -m 644 ${srcdir}/blank-cert8.db ${pkgdir}${MINGW_PREFIX}/etc/pki/nssdb/cert8.db
  install -m 644 ${srcdir}/blank-key3.db ${pkgdir}${MINGW_PREFIX}/etc/pki/nssdb/key3.db
  install -m 644 ${srcdir}/blank-secmod.db ${pkgdir}${MINGW_PREFIX}/etc/pki/nssdb/secmod.db

  # Copy the development libraries we want
  for file in libcrmf.dll.a libnssb.dll.a libnssckfw.dll.a libfreebl.dll.a libcryptohi.dll.a libcerthi.dll.a libcertdb.dll.a libsoftokn.dll.a libpkixutil.dll.a
  do
    install -m 644 dist/*.OBJ/lib/${file} ${pkgdir}${MINGW_PREFIX}/lib/
  done

  # Copy the binaries we want
  for file in certutil cmsutil crlutil modutil pk12util signtool signver ssltap
  do
    install -m 755 dist/*.OBJ/bin/${file}.exe ${pkgdir}${MINGW_PREFIX}/bin/
  done

  # Copy the binaries we ship as unsupported
  for file in atob btoa derdump ocspclnt pp selfserv shlibsign strsclnt symkeyutil \
   tstclnt vfyserv vfychain
  do
    install -m 755 dist/*.OBJ/bin/${file}.exe ${pkgdir}${MINGW_PREFIX}/lib/nss/
  done

  # Copy the include files we want
  for file in dist/public/nss/*.h
  do
    install -m 644 ${file} ${pkgdir}${MINGW_PREFIX}/include/nss3
  done

  # License
  install -Dm644 nss/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
