# Maintainer: Richard Shaw <hobbes1069@gmail.com>

_realname=fltk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.5
pkgrel=2
pkgdesc="C++ user interface toolkit (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.fltk.org/"
license=('LGPLv2+ with exceptions')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' 'staticlibs' 'buildflags')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/${_realname}/${_realname}/archive/release-${pkgver}.tar.gz"
        "0001-cmake-fix-lib-mess.patch"
        "0002-cmake-disable-dlfcn-search.patch"
        "0003-cmake-fix-install.patch")
sha256sums=('5c534287b0e03b9520ff866704a5649268986b371bdf8f6ac003fa240e761901'
            'b8061f26d989f76441d68a07ba4089b50eceb90fb3234106a88511a12d9ddb50'
            '05daefb5031defa3c9fbba48c46d1322f226bd4a3e89c31a1230817ba6d6d6ed'
            '52c1b13eaceec9fbefdd13e19cc71120033aa8168ec6e4149715eb63e74fe224')

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

  # Copy from ArchLinux
  # https://github.com/fltk/fltk/issues/20
  patch -p1 -i "${srcdir}/0001-cmake-fix-lib-mess.patch"

  patch -p1 -i "${srcdir}/0002-cmake-disable-dlfcn-search.patch"
  patch -p1 -i "${srcdir}/0003-cmake-fix-install.patch"
}

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

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DOPTION_BUILD_EXAMPLES=OFF \
    -DOPTION_BUILD_SHARED_LIBS=ON \
    "../${_realname}-release-${pkgver}"

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

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/fltk/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/fltk-config"

  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/COPYING" \
  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
