# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=cmake
pkgname=("${_realname}" "${_realname}-emacs" "${_realname}-vim")
pkgver=4.1.2
pkgrel=1
pkgdesc="A cross-platform open-source make system"
arch=('i686' 'x86_64')
url="https://www.cmake.org/"
msys2_repository_url="https://gitlab.kitware.com/cmake/cmake"
msys2_references=(
  "anitya: 306"
  "cpe: cpe:2.3:a:cmake_project:cmake"
)
license=(spdx:BSD-3-Clause)
makedepends=('gcc'
             'emacs'
             'autotools'
             'jsoncpp-devel'
             'libarchive-devel'
             'libbz2-devel'
             'libcurl-devel'
             'libexpat-devel'
             'liblzma-devel'
             'librhash-devel'
             'libutil-linux-devel'
             'libuv-devel'
             'libzstd-devel'
             'ncurses-devel'
             'zlib-devel')
depends=('gcc-libs'
         'jsoncpp'
         'libcurl'
         'libexpat'
         'libarchive'
         'librhash'
         'libutil-linux'
         'libuv'
         'ncurses'
         'pkgconf'
         'zlib'
         'cppdap')
options=('staticlibs' 'strip')
source=(https://www.cmake.org/files/v${pkgver%.*}/${pkgname}-${pkgver}.tar.gz)
sha256sums=('643f04182b7ba323ab31f526f785134fb79cba3188a852206ef0473fee282a15')
# =========================================== #

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH}
  cd ${srcdir}/build-${CARCH}
  MSYSTEM=MSYS ${srcdir}/${_realname}-${pkgver}/configure \
    --prefix=/usr \
    --system-libs \
    --no-qt-gui \
    --parallel=$(nproc) \
    --mandir=share/man \
    --docdir=share/doc/${_realname} \
    --datadir=share/${_realname} \
    -- -DCURSES_FORM_LIBRARY=/usr/lib/libformw.dll.a \
      -DCTEST_TEST_CTEST=ON \
      -DCMAKE_BUILD_TYPE=Release
  plain "Start building..."
  make
  make DESTDIR=${srcdir}/dest install
}

#Enable once we can get all tests to pass
#check() {
#  cd ${srcdir}/build-${CARCH}
#  make test
#}


package_cmake() {
  cd ${srcdir}/dest

  install -d  ${pkgdir}/usr/bin/
  install -d  ${pkgdir}/usr/share/aclocal
  install -d  ${pkgdir}/usr/share/${_realname}

  cp -r usr/bin/* ${pkgdir}/usr/bin/
  cp -r usr/share/aclocal/* ${pkgdir}/usr/share/aclocal/
  cp -r usr/share/cmake/* ${pkgdir}/usr/share/${_realname}/

  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/LICENSE.rst \
    ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_cmake-vim() {
  pkgdesc="A cross-platform open-source make system (vim mode)"
  depends=("cmake=${pkgver}" 'vim')
  cd ${srcdir}/dest
  install -d ${pkgdir}/usr/share

  cp -rf usr/share/vim ${pkgdir}/usr/share/
}

package_cmake-emacs() {
  pkgdesc="A cross-platform open-source make system (Emacs mode)"
  depends=(cmake=${pkgver} 'emacs')
  cd ${srcdir}/dest

  install -d ${pkgdir}/usr/share
  cp -rf usr/share/emacs ${pkgdir}/usr/share/

  /usr/bin/emacs -batch -f batch-byte-compile \
    ${pkgdir}/usr/share/emacs/site-lisp/cmake-mode.el
}

