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

_realname=netcdf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.9.3
pkgrel=6
pkgdesc="Interface for scientific data access to large binary data (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.unidata.ucar.edu/software/netcdf/"
msys2_repository_url="https://github.com/Unidata/netcdf-c"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libzip")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-blosc"
             "${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-libaec"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd")
optdepends=("${MINGW_PACKAGE_PREFIX}-netcdf-c++: C++ bindings"
            "${MINGW_PACKAGE_PREFIX}-netcdf-fortran: Fortran bindings"
            "${MINGW_PACKAGE_PREFIX}-blosc: blosc and zstdfilters plugins")
checkdepends=("unzip")
source=("https://github.com/Unidata/netcdf-c/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-no-debug-libraries.patch")
sha256sums=('990f46d49525d6ab5dc4249f8684c6deeaf54de6fec63a187e9fb382cc0ffdff'
            'a16300682b43de63bead0a42211b2219820599a6eb3d60bc83c27f85a1c64711')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

  apply_patch_with_msg \
    0001-no-debug-libraries.patch
}

build() {
  local -a _static_flags=(
    -DLIBXML_STATIC
  )

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # disable multitude of warnings
  CFLAGS+=" -Wno-sign-conversion -Wno-float-conversion -Wno-incompatible-pointer-types"

  msg2 "Building static libraries"
  mkdir -p "${srcdir}"/build-static-${MSYSTEM} && cd "${srcdir}"/build-static-${MSYSTEM}

  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    -DNETCDF_ENABLE_DLL=OFF \
    -DNETCDF_ENABLE_TESTS=OFF \
    -DNETCDF_ENABLE_DAP_REMOTE_TESTS=OFF \
    -DNETCDF_ENABLE_DAP=ON \
    -DNETCDF_ENABLE_HDF5=ON \
    -DNETCDF_ENABLE_BYTERANGE=ON \
    -DNETCDF_ENABLE_PLUGINS=OFF \
    -Wno-dev \
    "${srcdir}/${_realname}-c-${pkgver}"
  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  msg2 "Building shared libraries"
  mkdir -p "${srcdir}"/build-shared-${MSYSTEM} && cd "${srcdir}"/build-shared-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DNETCDF_WITH_PLUGIN_DIR=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DNETCDF_ENABLE_EXAMPLES=OFF \
    -DNETCDF_ENABLE_TESTS=OFF \
    -DNETCDF_ENABLE_DAP_REMOTE_TESTS=OFF \
    -DNETCDF_ENABLE_DAP=ON \
    -DNETCDF_ENABLE_HDF5=ON \
    -DNETCDF_ENABLE_BYTERANGE=ON \
    -DNETCDF_ENABLE_LOGGING=ON \
    -DENABLE_PLUGIN_INSTALL=ON \
    -DNETCDF_WITH_PLUGIN_DIR=${MINGW_PREFIX}/lib/netcdf \
    -Wno-dev \
    "${srcdir}/${_realname}-c-${pkgver}"
  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-shared-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/cmake.exe -DNETCDF_ENABLE_TESTS=ON ../${_realname}-c-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  PATH="${srcdir}"/build-shared-${MSYSTEM}:$PATH \
  "${MINGW_PREFIX}"/bin/ctest
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install build-static-${MSYSTEM}

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install build-shared-${MSYSTEM}

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})

  # Fix pkgconfig file
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/netcdf.pc
  sed -e "1n;s|${MINGW_PREFIX}|\$\{prefix\}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/netcdf.pc

  # Fix CMake files
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/cmake/netCDF/netCDFConfig.cmake
  sed -e "s|${MINGW_PREFIX}|\$\{PACKAGE_PREFIX_DIR\}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/cmake/netCDF/netCDFConfig.cmake

  # Remove source directory mention
  local SRCDIR_WIN=$(cygpath -wm "${srcdir}")
  for file in \
    "${pkgdir}${MINGW_PREFIX}/bin/nc-config" \
    "${pkgdir}${MINGW_PREFIX}/lib/libnetcdf.settings" \
    "${pkgdir}${MINGW_PREFIX}/lib/cmake/netCDF/netCDFConfig.cmake" \
    ; do
    sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
      -e "s|${SRCDIR_WIN}|\$\{_SRCDIR\}|g" "$file"
  done
}
