# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=tidy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.8.0
pkgrel=1
pkgdesc="A tool to tidy down your HTML code to a clean style (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.html-tidy.org/"
msys2_repository_url="https://github.com/htacg/tidy-html5"
msys2_references=(
  "cpe: cpe:/a:htacg:tidy"
)
license=(spdx:HTMLTIDY)
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-libxslt"
  "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/htacg/tidy-html5/archive/$pkgver/${_realname}-${pkgver}.tar.gz")
sha256sums=('59c86d5b2e452f63c5cdb29c866a12a4c55b1741d7025cf2f3ce0cde99b0660e')

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

  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 \
    -DBUILD_TAB2SPACE=ON \
    ../tidy-html5-${pkgver}

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

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

# Compatibility symlinks until everything is ported
  ln -s tidybuffio.h "$pkgdir"/${MINGW_PREFIX}/include/buffio.h
  ln -s tidyplatform.h "$pkgdir"/${MINGW_PREFIX}/include/platform.h

  install -Dm755 tab2space.exe $pkgdir/${MINGW_PREFIX}/bin
  install -Dm644 "$srcdir"/${_realname}-html5-$pkgver/README/LICENSE.md \
    "$pkgdir${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
