# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=double-conversion
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.3.1
pkgrel=3
pkgdesc="Binary-decimal and decimal-binary routines for IEEE doubles (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/google/double-conversion"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/google/double-conversion/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        cmake-version.patch
        cmake-dual-lib.patch)
sha256sums=('fe54901055c71302dcdc5c3ccbe265a6c191978f3761ce1414d0895d6b0ea90e'
            '677de958606a7884147525cd985d0b133c95eafaac580d0eed1fd84ff339eedf'
            '66e1bf3216676dce9f5149e7a1b381e692653679e206f0e729fe5e9d158459a6')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/cmake-version.patch
  patch -p1 -i ${srcdir}/cmake-dual-lib.patch
}

build() {
  # shared
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realname}-${pkgver}

  cmake --build .

  # static
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DBUILD_SHARED_LIBS=OFF \
      ../${_realname}-${pkgver}

  cmake --build .
}

package() {
  # static
  cd "${srcdir}"/build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" cmake --install .

  # shared
  cd "${srcdir}"/build-${MSYSTEM}-shared
  DESTDIR="${pkgdir}" cmake --install .

  # License
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
