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

_realname=discount
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.7
pkgrel=1
pkgdesc="A Markdown implementation written in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.pell.portland.or.us/~orc/Code/discount/"
license=('custom:BSD')
provides=("${MINGW_PACKAGE_PREFIX}-markdown")
conflicts=("${MINGW_PACKAGE_PREFIX}-markdown")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/Orc/discount/archive/v${pkgver}.tar.gz"
        "discount-mingw-building.patch")
sha256sums=('72c1325ddfc40871d6810f1e272cf2d45b361f26357eb38f170fd04d737bb9f2'
            '1dafea93f8d546f00b3f57928124392e28fd30e4ef60268736d4837075e3e462')

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-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  CC=${MINGW_PREFIX}/bin/gcc.exe \
  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-${CARCH}"
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,include,lib}
  make PREFIX=${MINGW_PREFIX} DESTDIR="${pkgdir}" install.everything
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install -Dm644 COPYRIGHT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
}
