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

_realname=uncrustify
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.73.0
pkgrel=1
pkgdesc="Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://uncrustify.sourceforge.net/"
license=(GPL)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/uncrustify/uncrustify/archive/${_realname}-${pkgver}.tar.gz")
sha512sums=('24b7eb2a76e2dcfca64223df40a434fcdcc281f5937cbf5e78832deb7716ee01b596aedff473af1e3023b33914357582500e4954f4ded009684bc4fb700e64d9')

prepare() {
  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"

}

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

  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" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "../${_realname}-${_realname}-${pkgver}"

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

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

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

  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"
  install -d -m755 "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
  install -m644 etc/*.cfg "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
}
