# Contributor: Robert T. McGibbon <rmcgibbo@gmail.com>

_realname=netcdf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.7.4
pkgrel=2
pkgdesc="Interface for scientific data access to large binary data (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('custom')
url="https://www.unidata.ucar.edu/software/netcdf/"
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/Unidata/netcdf-c/archive/v${pkgver}.tar.gz"
        0001-mingw-cmake.patch
        0002-dutil-correcttypo.patch
        0003-ncconfigure-strlcat-export.patch
        0004-ddispatch-set-temp.patch
        0006-cvt-conversion.patch
        0007-mingw-config.patch
        0008-mingw-and-msvc-common.patch
        0009-mingw-getopt.patch
        0010-fix-typo.patch
        0011-fix-multiple-def.patch)
sha256sums=('99930ad7b3c4c1a8e8831fb061cb02b2170fc8e5ccaeda733bd99c3b9d31666b'
            'a306c20f6593d314701147323f952d4df80e77174c45ab201949f402d191e221'
            'da666e1f762b3ecd2becc9ad55fe1e32be9a47b5bae4f7990e7d94aae69697cd'
            '0116525b0b513b91c22519c38d9010a4eff0a7319cc2a3d882b296389119b632'
            'aeb61f0a4d69e26b2235b15948ac6b99ac92be5b0f8155bfa7aa4867af84efa8'
            'f5f5291c2486e9e765a93975d99871cee584bdd3d64e16745982aadeb799f00c'
            'a1aa112779a83ec74acd7de8a0a57915f5d37cae44633d0a4d0d20784a2d288c'
            '0c42dc0e93cff6e2f0a85340e08335f2eaed0a6f24f11152728b2739e0fcb7cc'
            '37e50632b805ea6de48c466010247b8e5ec30615248eb994204059dbe025a7e9'
            '278bea47ffc0566d5b6a60b3ccdb244405a64124f5905af01f6b834261dc72ac'
            'e7bebd703c23d1473b27de1d66832bd505076b073c49f2dc737c213796a1faf2')

prepare() {
  cd "${srcdir}/${_realname}-c-${pkgver}"
  patch -p1 -i ${srcdir}/0001-mingw-cmake.patch
  patch -p1 -i ${srcdir}/0002-dutil-correcttypo.patch
  patch -p1 -i ${srcdir}/0003-ncconfigure-strlcat-export.patch
  patch -p1 -i ${srcdir}/0004-ddispatch-set-temp.patch
  patch -p1 -i ${srcdir}/0006-cvt-conversion.patch
  patch -p1 -i ${srcdir}/0007-mingw-config.patch
  patch -p1 -i ${srcdir}/0008-mingw-and-msvc-common.patch
  patch -p1 -i ${srcdir}/0009-mingw-getopt.patch
  patch -p1 -i ${srcdir}/0010-fix-typo.patch

  # Fixed in upstream, just copy-paste :)
  patch -p1 -i ${srcdir}/0011-fix-multiple-def.patch
}

build() {
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-static-${MINGW_CHOST} && cd ${srcdir}/build-static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DENABLE_DLL=OFF \
    -DENABLE_DAP=ON \
    -DENABLE_TESTS=OFF \
    -DENABLE_NETCDF_4=ON \
    "${srcdir}/${_realname}-c-${pkgver}"
  make

  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-shared-${MINGW_CHOST} && cd ${srcdir}/build-shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_DAP=ON \
    -DENABLE_TESTS=OFF \
    -DENABLE_NETCDF_4=ON \
    -DENABLE_LOGGING=ON \
    "${srcdir}/${_realname}-c-${pkgver}"
  make
}

package() {
  cd ${srcdir}/build-static-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  cd ${srcdir}/build-shared-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  install -Dm644 ${srcdir}/${_realname}-c-${pkgver}/COPYRIGHT \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/netCDF/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/nc-config"
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/lib/libnetcdf.settings"
}
