# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=discount
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.0.d
_pkgver=3.0.0d
pkgrel=1
pkgdesc="A Markdown implementation written in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.pell.portland.or.us/~orc/Code/discount/"
msys2_repository_url="https://github.com/Orc/discount"
license=('spdx:BSD-3-Clause')
conflicts=("${MINGW_PACKAGE_PREFIX}-markdown")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://github.com/Orc/discount/archive/v${_pkgver}/${_realname}-${pkgver}.tar.gz"
        "discount-mingw-building.patch")
sha256sums=('0ed8cc27ac5d46dc6a8beedd5e5673ac8b466a6474bdb7d35f37c815f670385f'
            'e555c2a2af5003e165cf94495fd5924097149241cf56fdb5e11debe8362260da')

prepare() {
  cd "${srcdir}"/${_realname}-${_pkgver}
  patch -p1 -i "${srcdir}"/discount-mingw-building.patch
  sed -e "s/m 444/m 644/g" -i configure.inc
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf "${srcdir}"/${_realname}-${_pkgver} "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  AC_CPP_PROG=${MINGW_PREFIX}/bin/cpp.exe \
  AC_LN_PROG="cp -pR" \
  ./configure.sh \
      --prefix=${MINGW_PREFIX} \
      --enable-all-features \
      --shared

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make PREFIX=${MINGW_PREFIX} DESTDIR="${pkgdir}" install.everything

  mv "${pkgdir}"${MINGW_PREFIX}/lib/*.dll "${pkgdir}"${MINGW_PREFIX}/bin/

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -e "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/libmarkdown.pc
  sed -e "1n;s|${MINGW_PREFIX}|\$\{prefix\}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/libmarkdown.pc

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