# Maintainer: Peter Budai <peterbudai at hotmail dot com>

_realname=libgphoto2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.33
pkgrel=1
pkgdesc="The libgphoto2 camera access and control library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://gphoto.org/proj/libgphoto2/"
license=("spdx:LGPL-2.1-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libgd"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-libltdl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("https://github.com/gphoto/libgphoto2/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "libgphoto2-gp_system_filename-fix.patch"
        "libgphoto2-configure-ws232.patch"
        "0003-missing-min-max.patch")
sha256sums=('28825f767a85544cb58f6e15028f8e53a5bb37a62148b3f1708b524781c3bef2'
            'SKIP'
            '49bdeae623754c53b515a1ca92712aa9b62c228f8edb9e317cd3efb643dc52e6'
            '9303cb0b7fa0d58a066a45c54a53d4d3f69149d54576c31df04439169b6651ee'
            'c53954f16cc7c83d0703343f2af481ff5e8ea364f7d3566549ccd6e3d8b3d897')
validpgpkeys=('7C4AFD61D8AAE7570796A5172209D6902F969C95') # Marcus Meissner 

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/libgphoto2-gp_system_filename-fix.patch"
  patch -Np1 -i "${srcdir}/libgphoto2-configure-ws232.patch"
  patch -Np1 -i "${srcdir}/0003-missing-min-max.patch"

  autoreconf -fiv
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  cp -r ${_realname}-${pkgver} build-${MSYSTEM}

  #Note: --with-libusb=no is required for Windows 10 to work.
  cd "${srcdir}/build-${MSYSTEM}"
  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-libusb=no \
    --with-gdlib=auto \
    --disable-internal-docs

  # Clang cannot handle '-Wl, -rpath' properly
  find . -depth -name 'config.status' -exec sed -e "s|\${wl}-rpath \${wl}|-L|g" -i {} ';'

  make
}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  # Remove unused udev helper
  rm -rf "${pkgdir}/${MINGW_PREFIX}/lib/udev"

  # Remove recursive symlink
  rm -rfv "${pkgdir}/${MINGW_PREFIX}/include/gphoto2/gphoto2"  

  # install profile script
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/etc/profile.d

  cat > "${pkgdir}"${MINGW_PREFIX}/etc/profile.d/${_realname}.sh <<EOF
export CAMLIBS=${MINGW_PREFIX}/lib/${_realname}/${pkgver}
export IOLIBS=${MINGW_PREFIX}/lib/${_realname}_port/0.12.2
EOF

  install -Dm644 COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
