# Maintainer: Peter Budai <peterbudai@hotmail.com>

_realname=dlib
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=19.21
pkgrel=1
pkgdesc="A toolkit for making real world machine learning and data analysis applications in C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='http://dlib.net/'
license=('Boost')
depends=("${MINGW_PACKAGE_PREFIX}-lapack"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-lapack"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('strip')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/davisking/${_realname}/archive/v${pkgver}.tar.gz"
        "001-python-binding.patch"
        "003-proper-link-libraries.patch")
sha256sums=('116f52e58be04b47dab52057eaad4b5c4d5c3032d927fe23d55b0741fc4107a0'
            '25f0a3da2e8ca7490e8e44689d2e34c2e16a7fa1fc00cab46fbc171a349bd69c'
            'b330f72fe0b679f9245d7ef9b0780ed0712a8806d325cb072afdb02ffdba3196')

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

  patch -Np1 -i "${srcdir}/001-python-binding.patch"
  patch -Np1 -i "${srcdir}/003-proper-link-libraries.patch"

  cd "${srcdir}"
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DUSE_AVX_INSTRUCTIONS=ON \
      -DDLIB_ISO_CPP_ONLY=OFF \
      -DDLIB_IN_PROJECT_BUILD=OFF \
      -DDLIB_USE_CUDA=OFF \
      -DDLIB_JPEG_SUPPORT=ON \
      -DDLIB_PNG_SUPPORT=ON \
      -DDLIB_GIF_SUPPORT=ON \
      -DDLIB_LINK_WITH_SQLITE3=ON \
      -DDLIB_USE_FFTW=ON \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realname}-${pkgver}
  cmake --build . --config Release

  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/python setup.py build -G "MSYS Makefiles" \
    --set CMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    --set USE_AVX_INSTRUCTIONS=ON \
    --set DLIB_ISO_CPP_ONLY=OFF \
    --set DLIB_IN_PROJECT_BUILD=ON \
    --set DLIB_USE_CUDA=OFF \
    --set DLIB_JPEG_SUPPORT=ON \
    --set DLIB_PNG_SUPPORT=ON \
    --set DLIB_GIF_SUPPORT=ON \
    --set DLIB_LINK_WITH_SQLITE3=ON \
    --set DLIB_USE_FFTW=ON
}

#check() {
  #cd "${srcdir}"/build-${CARCH}
  #make check
#}

package_dlib() {
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-dlib")
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

  # Remove hard coded library path from cmake files
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  pushd "${pkgdir}${MINGW_PREFIX}/lib/cmake/dlib" > /dev/null
  sed -s "s|${PREFIX_DEPS}/include||g" -i ./dlibConfig.cmake
  sed -s -e "s|${PREFIX_DEPS}/lib/||g" -e "s|${PREFIX_DEPS}/include||g" -i ./dlib.cmake
  popd > /dev/null
}

package_python-dlib() {
  depends=("${MINGW_PACKAGE_PREFIX}-dlib"
           "${MINGW_PACKAGE_PREFIX}-python")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: Python interface")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-dlib")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-dlib")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-dlib")

  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
