# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=transmission
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-cli"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-gtk"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt")
pkgver=4.0.6
pkgrel=5
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: transmission-gtk'
  "cpe: cpe:/a:transmissionbt:transmission"
)
url='https://www.transmissionbt.com/'
license=('spdx:GPL-2.0-or-later AND GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-libb64"
         "${MINGW_PACKAGE_PREFIX}-libdeflate"
         "${MINGW_PACKAGE_PREFIX}-libevent"
         "${MINGW_PACKAGE_PREFIX}-libnatpmp"
         "${MINGW_PACKAGE_PREFIX}-libpsl"
         "${MINGW_PACKAGE_PREFIX}-libutp"
         "${MINGW_PACKAGE_PREFIX}-miniupnpc"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-dht"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-gtkmm3"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools")
source=("https://github.com/transmission/transmission/releases/download/${pkgver}/transmission-${pkgver}.tar.xz"
        "0001-transmission-relocate-web-dir.patch"
        "0002-miniupnpc-backport.patch")
sha256sums=('2a38fe6d8a23991680b691c277a335f8875bdeca2b97c6b26b598bc9c7b0c45f'
            '6e898ad756af8a907c5a0afc19dd684d723b0cea43e2c31b97003f867b13a535'
            '7de7916ee8acc75cb442d491d038d13d94b4c8405ea10ca2ed99f8412288083b')
noextract=("transmission-${pkgver}.tar.xz")

prepare() {
  tar -xf "transmission-${pkgver}.tar.xz" 2> /dev/null || tar -xf "transmission-${pkgver}.tar.xz" || true
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/0001-transmission-relocate-web-dir.patch"
  # https://github.com/transmission/transmission/commit/febfe49ca3ecab1a7142ecb34012c1f0b2bcdee8
  patch -p1 -i "${srcdir}/0002-miniupnpc-backport.patch"
}

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

  # This icon is missing in the tarball, and does not get copied to the build directory,
  # but we can borrow the Qt one as it's identical
  mkdir gtk
  cp "${srcdir}/${_realname}-${pkgver}"/qt/qtr.ico gtk/transmission.ico

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_TESTS=OFF \
      -DENABLE_QT=ON \
      -DENABLE_GTK=ON \
      -DENABLE_CLI=ON \
      -DENABLE_UTP=ON \
      -DUSE_GTK_VERSION=3 \
      -DUSE_QT_VERSION=6 \
      -DUSE_SYSTEM_EVENT2=ON \
      -DUSE_SYSTEM_DEFLATE=ON \
      -DUSE_SYSTEM_DHT=ON \
      -DUSE_SYSTEM_MINIUPNPC=ON \
      -DUSE_SYSTEM_NATPMP=ON \
      -DUSE_SYSTEM_PSL=ON \
      -DUSE_SYSTEM_UTP=ON \
      -DUSE_SYSTEM_B64=ON \
      -DWITH_CRYPTO=openssl \
      -DINSTALL_DOC=ON \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realname}-${pkgver}

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

package_transmission-cli() {
  pkgdesc="Fast, easy, and free BitTorrent client - CLI tools, daemon and web client (mingw-w64)"

  for dir in daemon cli utils; do
    cd "${srcdir}/build-${MSYSTEM}/${dir}"

    DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  done

  cd "${srcdir}/${_realname}-${pkgver}"
  install -d "${pkgdir}${MINGW_PREFIX}"/share/transmission
  cp -a web/public_html/ "${pkgdir}${MINGW_PREFIX}"/share/transmission

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licences/transmission-cli/COPYING"
}

package_transmission-gtk() {
  pkgdesc="Fast, easy, and free BitTorrent client - GTK+ GUI (mingw-w64)"
  depends+=(
    "${MINGW_PACKAGE_PREFIX}-gtk3"
    "${MINGW_PACKAGE_PREFIX}-gtkmm3"
    "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme")
  optdepends=("${MINGW_PACKAGE_PREFIX}-libnotify: Desktop notification support"
              "${MINGW_PACKAGE_PREFIX}-transmission-cli: daemon and web support")
  replaces=("${MINGW_PACKAGE_PREFIX}-transmission")
  conflicts=("${MINGW_PACKAGE_PREFIX}-transmission")
  provides=("${MINGW_PACKAGE_PREFIX}-transmission")

  for dir in gtk po; do
    cd "${srcdir}/build-${MSYSTEM}/${dir}"

    DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  done

  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licences/transmission-gtk/COPYING"
}

package_transmission-qt() {
  pkgdesc='Fast, easy, and free BitTorrent client - Qt GUI (mingw-w64)'
  depends+=("${MINGW_PACKAGE_PREFIX}-qt6-base"
            "${MINGW_PACKAGE_PREFIX}-qt6-svg")
  optdepends=("${MINGW_PACKAGE_PREFIX}-transmission-cli: daemon and web support")

  cd "${srcdir}/build-${MSYSTEM}/qt"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  rm -rf "${pkgdir}${MINGW_PREFIX}/etc"

  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licences/transmission-qt/COPYING"
}

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