# Maintainer: Jonathan Schleifer <js@nil.im>

_realname=objfw
pkgname=("${MINGW_PACKAGE_PREFIX}-libobjfw"
         "${MINGW_PACKAGE_PREFIX}-libobjfwhid"
         "${MINGW_PACKAGE_PREFIX}-libobjfwrt"
         "${MINGW_PACKAGE_PREFIX}-libobjfwtls"
         "${MINGW_PACKAGE_PREFIX}-ofarc"
         "${MINGW_PACKAGE_PREFIX}-ofdns"
         "${MINGW_PACKAGE_PREFIX}-ofhash"
         "${MINGW_PACKAGE_PREFIX}-ofhttp"
         "${MINGW_PACKAGE_PREFIX}-ofgctester")
pkgbase=mingw-w64-${_realname}
pkgver=1.4.2
pkgrel=1
pkgdesc="Portable, lightweight framework for the Objective-C language (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://objfw.nil.im/"
msys2_repository_url="https://github.com/ObjFW/ObjFW"
msys2_changelog_url="https://github.com/ObjFW/ObjFW/blob/main/ChangeLog"
license=('spdx:LGPL-3.0-only')
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-openssl")
groups=("${MINGW_PACKAGE_PREFIX}-${_realname}")
source=("https://objfw.nil.im/downloads/${_realname}-${pkgver}.tar.gz")
sha256sums=('8e6d0cd39271130a0b6c2789fa08f2598c77d9b88acbd0e2c15c8eb1144baa08')

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

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --with-tls=openssl \
    OBJC=clang

  make
}

check() {
  cd build-${MSYSTEM}
  make check
}

package_libobjfw() {
  pkgdesc="ObjFW library (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt")

  cd build-${MSYSTEM}
  make DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done

  # Those are in subpackages
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/objfwhid1.dll"
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/objfwrt1.dll"
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/objfwtls1.dll"
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/ofarc.exe"
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/ofdns.exe"
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/ofgctester.exe"
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/ofhash.exe"
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/ofhttp.exe"
  rm -fr "${pkgdir}/${MINGW_PREFIX}/include/ObjFWHID"
  rm -fr "${pkgdir}/${MINGW_PREFIX}/include/ObjFWRT"
  rm -fr "${pkgdir}/${MINGW_PREFIX}/include/ObjFWTLS"
  rm -f "${pkgdir}/${MINGW_PREFIX}/lib/libobjfwhid.dll.a"
  rm -f "${pkgdir}/${MINGW_PREFIX}/lib/libobjfwrt.dll.a"
  rm -f "${pkgdir}/${MINGW_PREFIX}/lib/libobjfwtls.dll.a"
  rm -f "${pkgdir}/${MINGW_PREFIX}/lib/objfw-config/ObjFWTLS.oc"
  rm -f "${pkgdir}/${MINGW_PREFIX}/lib/objfw-config/ObjFWHID.oc"
  rm -fr "${pkgdir}/${MINGW_PREFIX}/share/ofarc"
  rm -fr "${pkgdir}/${MINGW_PREFIX}/share/ofdns"
  rm -fr "${pkgdir}/${MINGW_PREFIX}/share/ofhash"
  rm -fr "${pkgdir}/${MINGW_PREFIX}/share/ofhttp"
  rm -f "${pkgdir}/${MINGW_PREFIX}/share/man/man1/ofarc.1"
  rm -f "${pkgdir}/${MINGW_PREFIX}/share/man/man1/ofdns.1"
  rm -f "${pkgdir}/${MINGW_PREFIX}/share/man/man1/ofgctester.1"
  rm -f "${pkgdir}/${MINGW_PREFIX}/share/man/man1/ofhash.1"
  rm -f "${pkgdir}/${MINGW_PREFIX}/share/man/man1/ofhttp.1"
}

package_libobjfwrt() {
  pkgdesc="ObjFW Objective-C runtime library (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")

  cd build-${MSYSTEM}
  make -C src/runtime DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}

package_libobjfwtls() {
  pkgdesc="TLS support for ObjFW (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-libobjfw"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt"
           "${MINGW_PACKAGE_PREFIX}-openssl")

  cd build-${MSYSTEM}
  make -C src/tls DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}

package_libobjfwhid() {
  pkgdesc="HID support for ObjFW (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-libobjfw"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt")

  cd build-${MSYSTEM}
  make -C src/hid DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}

package_ofarc() {
  pkgdesc="Utility for handling ZIP, Tar and LHA archives (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libobjfw"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt")

  cd build-${MSYSTEM}
  make -C utils/ofarc DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}

package_ofdns() {
  pkgdesc="Utility for performing DNS requests on the command line (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libobjfw"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt")

  cd build-${MSYSTEM}
  make -C utils/ofdns DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}

package_ofhash() {
  pkgdesc="Utility to hash files with various cryptographic hash functions (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libobjfw"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt")

  cd build-${MSYSTEM}
  make -C utils/ofhash DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}

package_ofhttp() {
  pkgdesc="Command line downloader for HTTP(S) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libobjfw"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt"
           "${MINGW_PACKAGE_PREFIX}-libobjfwtls")

  cd build-${MSYSTEM}
  make -C utils/ofhttp DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}


package_ofgctester() {
  pkgdesc="Game controller tester for the terminal (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libobjfw"
           "${MINGW_PACKAGE_PREFIX}-libobjfwrt"
           "${MINGW_PACKAGE_PREFIX}-libobjfwhid")

  cd build-${MSYSTEM}
  make -C utils/ofgctester DESTDIR="${pkgdir}/" install

  for i in COPYING COPYING.LESSER; do
    install -D -m 644 "${i}" \
      "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname/${MINGW_PACKAGE_PREFIX}-/}/${i}"
  done
}

# 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;
