# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=seexpr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.11
pkgrel=1
pkgdesc="A simple expression language (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://hoene.github.io/seexpr/"
license=("Apache")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("https://github.com/wdas/SeExpr/archive/v${pkgver}.tar.gz"
        "0001-seexpr-2.11-fix-cxxflags.patch"
        "0002-seexpr-2.11-fix-inline-error.patch"
        "0003-seexpr-2.11-fix-dll-installation.patch")
sha512sums=('4de683502c9d78486a299984298b9b5923e66835faa1229c4757916edebb060c50148a58d381a330e0685499a1ffd2af3c5992d8fb4f54de4f9db6f23ec33df7'
            '32ae31d6ed443f954056e1d99f08c2cf93eebc52fd504e581638a4067c5527d3992e7cedd852d511ac40fd1582e6652e15c4b65931ee2e496ecd352b8cf4ee45'
            'cad8a911215d84a09fe3fa79458c52449cbfb4d6c704e0442438e56b19ca1dd2087070d9ba8de43730ffdd8d5a86d6df0bb15c49261b60625bc824fcf7e29570'
            'fff6a72253cbce998377f23a101ee4ccfeebc726f56c62bb6770fb30827d82c7da62f8ea105daf9c9d8c16e6db84326e328fe06743b700665ef33a1ae30f6e4f')

prepare() {
  cd "${srcdir}/SeExpr-${pkgver}"
  patch -p1 -i "${srcdir}/0001-seexpr-2.11-fix-cxxflags.patch"
  patch -p1 -i "${srcdir}/0002-seexpr-2.11-fix-inline-error.patch"
  patch -p1 -i "${srcdir}/0003-seexpr-2.11-fix-dll-installation.patch"
  
  # Don't build the demo even if QT4 is found
  # sed -i 's/if(QT4_FOUND)/if(FALSE)/' "${srcdir}/SeExpr-${pkgver}/src/demos/segraph/CMakeLists.txt"

  # Don't build doc, demos and tests as they are not needed
  sed -i 's/ADD_SUBDIRECTORY (src\/doc)/ /' "${srcdir}/SeExpr-${pkgver}/CMakeLists.txt"
  sed -i 's/ADD_SUBDIRECTORY (src\/demos)/ /' "${srcdir}/SeExpr-${pkgver}/CMakeLists.txt"
  sed -i 's/ADD_SUBDIRECTORY (src\/tests)/ /' "${srcdir}/SeExpr-${pkgver}/CMakeLists.txt"
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
  -G"MSYS Makefiles" \
  -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_SHARED_LIBS:BOOL='ON' \
  -DBUILD_TESTS:BOOL='OFF' \
  -DCMAKE_COLOR_MAKEFILE:BOOL='ON' \
  -Wno-dev \
  ../SeExpr-${pkgver}
  
  make
  
  # Static Build
  [[ -d "build-${MINGW_CHOST}-static" ]] && rm -rf "build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static"
  cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
  -G"MSYS Makefiles" \
  -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_SHARED_LIBS:BOOL='OFF' \
  -DBUILD_TESTS:BOOL='OFF' \
  -DCMAKE_COLOR_MAKEFILE:BOOL='ON' \
  -Wno-dev \
  ../SeExpr-${pkgver}
  
  make
}

package() {
  # Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make DESTDIR=${pkgdir} install

  # Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  make DESTDIR=${pkgdir} install
}
