# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=libimobiledevice
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.0
pkgrel=13
pkgdesc='A cross-platform protocol library to communicate with iOS devices (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.libimobiledevice.org/'
msys2_repository_url="https://github.com/libimobiledevice/libimobiledevice"
msys2_references=(
  "cpe: cpe:/a:libimobiledevice:libimobiledevice"
)
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libusbmuxd"
         "${MINGW_PACKAGE_PREFIX}-libplist"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings")
source=("https://github.com/libimobiledevice/libimobiledevice/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-1.3.0-Fix-windows-build.patch
        002-1.3.0-Link-Iphlpapi.patch
        003-1.3.0-PyString_AsString-now-PyBytes_AsString.patch
        004-clang.patch::https://github.com/libimobiledevice/libimobiledevice/commit/19f9a4c3.patch
        005-libplist-2.3.0.patch
        006-fix-build-with-cython3.patch::https://github.com/libimobiledevice/libimobiledevice/commit/59ec38cb.patch
        007-cython31-fix.patch)
sha256sums=('acbfb73eabee162e64c0d9de207d71c0a5f47c40cd5ad32a5097f734328ce10a'
            'c72055c417d6fd10106793fd193c64e0147240f584c9025561cb9cd28bee112d'
            '693d2df13fd344ef67e41c7a769c12843a60a7d89a825e118a40914ed407a4fb'
            '438469e5bfce0eca723ec597ae8e97cbaca22ee548559f2d65e1fd63aba46b4b'
            'f6a5ef4500f65290d5b145bf7591e17bc3b7e420c6f7b10cf07330e076788304'
            '57fb4616c68c240aad47df52168fa30e4bf43845bfbe53df23cc412003ae64fd'
            '4e54aaf77618e72083d7dcad49328f7d68beb6dea7da503f2c18ec0a471a31b4'
            '0df46672305cbea07c4c2a52406b704a8256ccdc08821f2f85e93e54293b8044')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  # https://github.com/libimobiledevice/libimobiledevice/commit/52371bccdcb1b0fe98930e79ac2ab9bdad2700e2
  apply_patch_with_msg \
    001-1.3.0-Fix-windows-build.patch
  # https://github.com/libimobiledevice/libimobiledevice/commit/7044571b4f243d2fda4dd3aa491f2f279787ee19
  apply_patch_with_msg \
    003-1.3.0-PyString_AsString-now-PyBytes_AsString.patch
  # https://github.com/archlinux/svntogit-packages/blob/master/libimobiledevice/trunk/libplist-2.3.0.diff
  apply_patch_with_msg \
    005-libplist-2.3.0.patch

  apply_patch_with_msg \
    002-1.3.0-Link-Iphlpapi.patch \
    004-clang.patch \
    006-fix-build-with-cython3.patch

  # https://github.com/libimobiledevice/libimobiledevice/issues/1666
  apply_patch_with_msg \
    007-cython31-fix.patch

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  PYTHON=${MINGW_PREFIX}/bin/python \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-static \
    --enable-shared

  make
}

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

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

  rm -f "${pkgdir}"${MINGW_PREFIX}/lib/python*/site-packages/*.a
}
