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

_realname=tidy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_commit=5f7e367cb54563dabda4bf4e3c11c6ecc68a0fa3
pkgver=5.7.16
pkgrel=1
pkgdesc="A tool to tidy down your HTML code to a clean style (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.html-tidy.org/"
license=(custom)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-libxslt")
conflicts=("${MINGW_PACKAGE_PREFIX}-tidyhtml")
provides=("${MINGW_PACKAGE_PREFIX}-tidyhtml")
replaces=("${MINGW_PACKAGE_PREFIX}-tidyhtml")
source=(#"${_realname}-${pkgver}.tar.gz::https://github.com/htacg/tidy-html5/archive/$pkgver.tar.gz"
        "${_realname}-${pkgver}.tar.gz::https://github.com/htacg/tidy-html5/archive/${_commit}.tar.gz")
sha256sums=('7ff3ef0a774f44e780df9b94e48c2ea070a6cfc791b5bd907359fd718a7f1004')

prepare() {
  mv tidy-html5-{${_commit},${pkgver}}
}

build() {
  [[ -d "$srcdir}/build-${CARCH}" ]] && rm -rf "$srcdir/build-${CARCH}"
  mkdir -p "$srcdir/build-${CARCH}" && cd "$srcdir/build-${CARCH}"

  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 \
    -G'MSYS Makefiles' \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_{SHARED,STATIC}_LIBS=ON \
    -DBUILD_TAB2SPACE=ON \
    ../tidy-html5-$pkgver
  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR="$pkgdir" 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"
}
