# Maintainer: yumetodo <yume-wikijp@live.jp>

_realname=lpsolve
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.5.2.5
pkgrel=2
pkgdesc="Mixed Integer Linear Programming (MILP) solver (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://sourceforge.net/projects/lpsolve/"
license=('LGPL2.1')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc")
source=("https://downloads.sourceforge.net/project/${_realname}/${_realname}/${pkgver}/lp_solve_${pkgver}_source.tar.gz"
        "CMakeLists.txt"
        "lpsolveConfig.cmake.in"
        "avoid_undefined_reference.patch")
sha256sums=('201A7C62B8B3360C884EE2A73ED7667E5716FC1E809755053B398C2F5B0CF28A'
            'b37e745e83b1a3e7b3a282fc0ffbb60f0a29fc4ca7533e3e97f10e21c6ae0ecd'
            '11E739B216BA402AE6AF1E3A6BCF281F5BBB859EF0521362E82AC583A723D97D'
            '0737FC393B2791089DC87CB546494FC6957725DA36423E22A4AB4E80F929DB48')

prepare() {
  cd "${srcdir}/lp_solve_5.5"
  cp "${srcdir}/CMakeLists.txt" .
  cp "${srcdir}/lpsolveConfig.cmake.in" .
  patch -p0 -i "${srcdir}/avoid_undefined_reference.patch"
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # shared build
  [[ -d ${srcdir}/build-shared ]] && rm -rf ${srcdir}/build-shared
  mkdir -p ${srcdir}/build-shared && cd ${srcdir}/build-shared
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DLIB_TYPE=SHARED \
    ../lp_solve_5.5
  make

  # static build
  [[ -d ${srcdir}/build-static ]] && rm -rf ${srcdir}/build-static
  mkdir -p ${srcdir}/build-static && cd ${srcdir}/build-static
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DLIB_TYPE=STATIC \
    ../lp_solve_5.5
  make
}

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

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

warn_build_references() {
  : # I like __FILE__ and don't consider build references to be a problem
}
