# Maintainer: Jordan Irwin <antumdeluge@gmail.com>

_realname=md4c
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgbase="mingw-w64-${_realname}"
pkgver=0.5.2
pkgrel=1
pkgdesc="C Markdown parser implementation compliant to CommonMark specification"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/mity/md4c"
msys2_references=(
  "cpe: cpe:/a:md4c_project:md4c"
)
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${url}/archive/release-${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('55d0111d48fb11883aaee91465e642b8b640775a4d6993c2d0e7a8092758ef21')

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

  dir_shared="${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${dir_shared}" && cd "${dir_shared}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake" \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_SHARED_LIBS=ON \
      "${extra_config[@]}" \
      "../${_realname}-release-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake --build .

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake" \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_SHARED_LIBS=OFF \
      "${extra_config[@]}" \
      "../${_realname}-release-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  dir_shared="${srcdir}/build-${MSYSTEM}-shared"

  cd "${dir_shared}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # static libs
  dir_static="${srcdir}/build-${MSYSTEM}-static"
  install -vDm644 "${dir_static}/src/libmd4c.a" "${pkgdir}${MINGW_PREFIX}/lib/libmd4c.a"
  install -vDm644 "${dir_static}/src/libmd4c-html.a" "${pkgdir}${MINGW_PREFIX}/lib/libmd4c-html.a"

  # license
  install -vDm644 "${srcdir}/${_realname}-release-${pkgver}/LICENSE.md" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
