# Maintainer: luau-project <luau.project@gmail.com>

_realname=libminpack
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.0
pkgrel=2
pkgdesc="A software for solving nonlinear equations and nonlinear least squares problems (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.netlib.org/minpack'
license=('spdx:Minpack')
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${url}/disclaimer"
        "${url}/chkder.f"
        "${url}/dogleg.f"
        "${url}/dpmpar.f"
        "${url}/enorm.f"
        "${url}/fdjac1.f"
        "${url}/fdjac2.f"
        "${url}/hybrd1.f"
        "${url}/hybrd.f"
        "${url}/hybrj1.f"
        "${url}/hybrj.f"
        "${url}/lmder1.f"
        "${url}/lmder.f"
        "${url}/lmdif1.f"
        "${url}/lmdif.f"
        "${url}/lmpar.f"
        "${url}/lmstr1.f"
        "${url}/lmstr.f"
        "${url}/qform.f"
        "${url}/qrfac.f"
        "${url}/qrsolv.f"
        "${url}/r1mpyq.f"
        "${url}/r1updt.f"
        "${url}/rwupdt.f"
        "0001-pkgconfig-input-to-cmake.patch"
        "0002-minpack-c-cpp-header.patch"
        "0003-cmake-build-script.patch")
sha256sums=("f5b330efdad110cdd84d585ec61220b0650461fa599e36b13e1726c9346dcfb9"
            "6a71d2df5e75a800b42386d1096d4a55c32cdcd1e930a8957bed845dc77e5ef1"
            "5d29950bfbfa6415b4e07ce536b0388edd3ceb4d6b260b23eb11d9c3b6f135ba"
            "5c56b40119ddb0d00f60730a2f6ad8ceaac70422172b51af8dbde3944b324e7e"
            "c8ec66eeb809e5a1a8c896037597c73bd8966511a3b028fcc1f8be0e47310dd7"
            "b87bc037c7b1f728d1ec6417bc228a7cdb7a37c85627e8730f6c40267ef53009"
            "30ca8685efeab6f47bb40fb7c6f20f84d8467f39308bf10a54c9dac41c061391"
            "427ee1ab96f8583ccb661e76173758b8a98ae06af710bdce1acf8022ed27ac7b"
            "dae9685deaf24a3625ab65fc25c896cf0416aee1e09c93be17204273a381f521"
            "8cdb1da05e2bc9c2356c966136bef0f452f8d8a74ff0ff8d44988ef9a46da5c6"
            "f1f5994dba9b9e2a392bbc08ae1b1b74075dd0f8f6339531011fdedeb4654f96"
            "bd9bec43c803c20ab523410cd55476833bec63c96b10088c0d87c2a7c1df6ec4"
            "bcb863d4e71751e9c2e8bc163f3606823b206d4ded525645704238694d4b2cca"
            "bc6fac1d9b45d0331587705b47c0bc403b485883bcd8c9af9b1a2a858f57c1f7"
            "5e59e7d18e53bb588411f411d108851f8b3e24221c5983afe718dbf897874856"
            "01495a05ec1c0ba4ec3ace1f8f5e0db0eb5a2b8cc38991213792083005d73b3a"
            "1371ac7d94b943a647bc81bdf2df5826114c744a016a6ce78fe559c4205f1e61"
            "d2a59ac39a1d5c1dc6ff69a3dcc317509df89ef7775ce53f30ca44e184e03503"
            "2663c845eaefcbd398f13525f33e090b8dfe005fa4b4a3157caa8c06599f1bc5"
            "8183b99d4550d2f20b1ffc7456d0afa756e9d0ac3d8976148628416632b3aa42"
            "768eb92e2d4d257f81202b8de83d30e390ca92a0763e96b59175aee02bf45e7f"
            "a8e8e5038e60823c273898b8c723f423bb9abbb5da377a73c33bce8f152b5b12"
            "33924ea50382e1c74b7f8b0662bea19c7f6772468b5d07e3250384c7c18d1666"
            "d5a5c0a934368be75905c0620898d6061970f4553f4045f8f19be25e492043c3"
            "c9a6f6161c9aee0997e1e42a09111c106aaa6b05e0cdbe4b55c56aa0ad46bb0e"
            "b062e25cc9d9d874fe8a5f6f27a912c7b9f927840bffecb6d7290bf47a80221b"
            "033049e5833d7b133d59c7c52bbe4ed5c7d377264e241148ee944b188ce85ade")

prepare() {
  mkdir -p "${srcdir}" && cd "${srcdir}"
  patch -Np1 -i "${srcdir}"/0001-pkgconfig-input-to-cmake.patch
  patch -Np1 -i "${srcdir}"/0002-minpack-c-cpp-header.patch
  patch -Np1 -i "${srcdir}"/0003-cmake-build-script.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -DBUILD_{SHARED,STATIC}_LIBS=ON \
    -DMINPACK_VERSION="${pkgver}" \
    -S . \
    -B build-${MSYSTEM}

  "${MINGW_PREFIX}"/bin/cmake.exe --build build-${MSYSTEM}
}

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

  install -Dm644 "${srcdir}/disclaimer" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
