# Maintainer: Martchus <martchus@gmx.net>

# All my PKGBUILDs are managed at https://github.com/Martchus/PKGBUILDs where
# you also find the URL of a binary repository.

# set the web view provider: either webkit, webengine, auto or none
_webview_provider=${MINGW_W64_SYNCTHING_TRAY_WEBVIEW_PROVIDER:-none}

# set the JavaScript provider: either script, qml, auto or none
_js_provider=${MINGW_W64_SYNCTHING_TRAY_JS_PROVIDER:-qml}

# define package name with additional variants for experimentation that can be uncommented
pkgname=mingw-w64-syncthingtray-qt6
#pkgname=mingw-w64-syncthingtray-qt6-libst # enable libsyncthing regardless of env
#pkgname=mingw-w64-syncthingtray-qt6-libst-no-mguard-cgo # do not pass mguard to cgo flags
#pkgname=mingw-w64-syncthingtray-qt6-libst-no-mguard-all # do not use mguard at all (of course deps are still built with it)

_reponame=syncthingtray
_name=${pkgname#mingw-w64-}
_name=${_name%-qt6*}
pkgver=2.0.10
[[ $BUILD_BEFORE_TAGGING ]] && _pkgver=branch=master || _pkgver=tag=v$pkgver
pkgrel=1.1
arch=('any')
pkgdesc='Tray application for Syncthing (mingw-w64)'
license=(GPL-2.0-or-later)
depends=('mingw-w64-crt' 'mingw-w64-qt6-svg' 'mingw-w64-qtutilities-qt6' 'mingw-w64-qtforkawesome-qt6' 'mingw-w64-openssl' 'mingw-w64-boost'
         'mingw-w64-sqlite')
[[ $_webview_provider == none ]] && depends+=('mingw-w64-qt6-base')
[[ $_webview_provider == webkit ]] && depends+=('mingw-w64-qt6-webkit')
[[ $_webview_provider == webengine ]] && depends+=('mingw-w64-qt6-webengine')
[[ $_js_provider == script ]] && depends+=('mingw-w64-qt6-script')
[[ $_js_provider == qml ]] && depends+=('mingw-w64-qt6-declarative' 'qt6-declarative')
makedepends=('mingw-w64-gcc' 'mingw-w64-cmake' 'mingw-w64-qt6-tools' 'qt6-tools' 'clang' 'ffmpeg' 'ninja' 'git')
url=https://github.com/Martchus/${_reponame}
_github_url=git+https://github.com/Martchus
_git_url=${MARTCHUS_GIT_URL_PREFIX:-$_github_url}
source=("$_reponame::$_git_url/${_reponame}#$_pkgver")
sha256sums=('SKIP'
            'SKIP')
options=(!buildflags staticlibs !strip !emptydirs)

# set whether libsyncthing should be enabled: either ON or OFF
[[ $pkgname =~ .*-clang-.* ]] \
  && _enable_libsyncthing=${MINGW_W64_CLANG_SYNCTHING_TRAY_ENABLE_LIBSYNCTHING:-OFF} \
  || _enable_libsyncthing=${MINGW_W64_SYNCTHING_TRAY_ENABLE_LIBSYNCTHING:-ON}
[[ $pkgname =~ .*-libst.* ]] && _enable_libsyncthing=ON
if [[ $_enable_libsyncthing == ON ]]; then
  makedepends+=('go')
  source+=("syncthing::$_github_url/syncthing")
fi

_architectures=(${MINGW_W64_QT6_ARCHS:-x86_64-w64-mingw32})
_configurations=()
if ! [[ $NO_SHARED_LIBS ]]; then
    _configurations+=('shared')
fi
if ! [[ $NO_STATIC_LIBS ]]; then
    _configurations+=('static')
    makedepends+=('mingw-w64-cmake-static' 'mingw-w64-qt6-base-static' 'mingw-w64-qt6-svg-static' 'mingw-w64-qt6-translations' 'breeze-icons')
    [[ $_js_provider == script ]] && makedepends+=('mingw-w64-qt6-script-static')
    [[ $_js_provider == qml ]] && makedepends+=('mingw-w64-qt6-declarative-static')
fi
[[ $_enable_libsyncthing == ON ]] && _disable_libsyncthing=OFF || _disable_libsyncthing=ON

prepare() {
  cd "$srcdir/$_reponame"
  [[ $_enable_libsyncthing == ON ]] || return 0
  git submodule init
  git config submodule.libsyncthing/go/src/github.com/syncthing/syncthing.url "$srcdir/syncthing"
  git -c protocol.file.allow=always submodule update
}

build() {
  cd "$srcdir/$_reponame"
  echo "git rev: $(git rev-list --count HEAD).$(git rev-parse --short HEAD)"

  declare -A _config_flags=(
    [shared]="
        -DBUILD_SHARED_LIBS:BOOL=ON
        -DWEBVIEW_PROVIDER:STRING=${_webview_provider}
    "
    [static]='
        -DBUILD_SHARED_LIBS:BOOL=OFF
        -DCMAKE_FIND_LIBRARY_SUFFIXES:STRING=.a;.lib
        -DSTATIC_LINKAGE:BOOL=ON
        -DSTATIC_LIBRARY_LINKAGE:BOOL=ON
        -DBUILTIN_TRANSLATIONS:BOOL=ON
        -DBUILTIN_ICON_THEMES:STRING=breeze;breeze-dark
        -DIMAGE_FORMAT_SUPPORT:STRING=Gif;ICO;Jpeg
        -DSVG_SUPPORT:BOOL=ON
        -DSVG_ICON_SUPPORT:BOOL=ON
        -DWEBVIEW_PROVIDER:STRING=none
        -DSETUP_TOOLS:BOOL=ON
    '
  )
  declare -A _cfg_suffix=(
    [static]='-static'
  )
  for _arch in "${_architectures[@]}"; do
    for _cfg in "${_configurations[@]}"; do
      msg2 "${_arch}-${_cfg}"
      local arch_flags=()
      local additional_flags=()
      # add arch-specific configuration
      [[ $_arch =~ aarch64-.* ]] && arch_flags+=(
        -DUSE_BOOST_PROCESS:BOOL=ON # supposed to work on Windows on ARM as of Boost 1.87.0
      )
      # allow building experimental variants without mguard
      if [[ $pkgname =~ .*-clang-.*-no-mguard-.* ]]; then
        cppflags_no_mguard=${CPPFLAGS//-mguard=cf/}
        cflags_no_mguard=${CFLAGS//-mguard=cf/}
        cxxflags_no_mguard=${CXXFLAGS//-mguard=cf/}
        ldflags_no_mguard=${LDFLAGS//-mguard=cf/}
        if [[ $pkgname =~ .*-clang-.*-no-mguard-all.* ]]; then
          arch_flags+=(
            -DCMAKE_C_FLAGS="$cflags_no_mguard"
            -DCMAKE_CXX_FLAGS="$cxxflags_no_mguard"
            -DCMAKE_EXE_LINKER_FLAGS="$ldflags_no_mguard"
            -DCMAKE_SHARED_LINKER_FLAGS="$ldflags_no_mguard"
            -DCMAKE_MODULE_LINKER_FLAGS="$ldflags_no_mguard"
          )
        elif [[ $pkgname =~ .*-clang-.*-no-mguard-cgo.* ]]; then
          arch_flags+=(
            -DCGO_CFLAGS_OVERRIDE="$cflags_no_mguard"
            -DCGO_CXXFLAGS_OVERRIDE="$cxxflags_no_mguard"
            -DCGO_LDFLAGS_OVERRIDE="$ldflags_no_mguard"
          )
        fi
      fi
      ${_arch}-cmake${_cfg_suffix[$_cfg]} \
        -G Ninja \
        -S "$srcdir/$_reponame" \
        -B "build-${_arch}-${_cfg}" \
        -DCMAKE_BUILD_TYPE:STRING='Release' \
        -DCMAKE_INSTALL_PREFIX="/usr/${_arch}" \
        -DAPPEND_GIT_REVISION:BOOL=OFF \
        -DCONFIGURATION_NAME:STRING="${_cfg}-qt6" \
        -DCONFIGURATION_DISPLAY_NAME="" \
        -DCONFIGURATION_PACKAGE_SUFFIX:STRING="-${_cfg}" \
        -DCONFIGURATION_PACKAGE_SUFFIX_QTUTILITIES:STRING="-${_cfg}-qt6" \
        -DCONFIGURATION_TARGET_SUFFIX:STRING="qt6" \
        -DSYNCTHINGCTL_CONFIGURATION_TARGET_SUFFIX:STRING="qt6${_cfg_suffix[$_cfg]}" \
        -DSYNCTHINGTRAY_CONFIGURATION_TARGET_SUFFIX:STRING="qt6${_cfg_suffix[$_cfg]}" \
        -DQT_PACKAGE_PREFIX:STRING=Qt6 \
        -DKF_PACKAGE_PREFIX:STRING=KF6 \
        -DJS_PROVIDER:STRING="${_js_provider}" \
        -DSYSTEMD_SUPPORT=OFF \
        -DNO_FILE_ITEM_ACTION_PLUGIN=ON \
        -DENABLE_TARGETS_FOR_MINGW64_CROSS_PACKAGING:BOOL=ON \
        -DVERSIONED_MINGW_LIBRARIES:BOOL=ON \
        -DNO_LIBSYNCTHING:BOOL="${_disable_libsyncthing}" \
        -DUSE_LIBSYNCTHING:BOOL="${_enable_libsyncthing}" \
        ${_config_flags[$_cfg]} \
        "${additional_flags[@]}" \
        "${arch_flags[@]}"
      VERBOSE=1 cmake --build "build-${_arch}-${_cfg}"
    done
  done
}

check() {
  for _arch in "${_architectures[@]}"; do
    [[ $_arch =~ i?86 ]] && continue
    for _cfg in 'static'; do
      msg2 "${_arch}-${_cfg}"
      export WINEPATH="/usr/${_arch}/bin" WINEDEBUG=-all
      export QT_QPA_PLATFORM=offscreen
      [[ $_cfg == shared ]] && cmake --build "build-${_arch}-${_cfg}" --target check || true
    done
  done
}

package() {
  install \
    -D --target-directory="$pkgdir/usr/share/licenses/$pkgname" \
    "$srcdir/${PROJECT_DIR_NAME:-$_reponame}/LICENSES-windows-distribution.md"

  cd "$srcdir/$_reponame"
  for _arch in "${_architectures[@]}"; do
    for _cfg in "${_configurations[@]}"; do
      msg2 "${_arch}-${_cfg}"
      DESTDIR="${pkgdir}" cmake --build "build-${_arch}-${_cfg}" --target install-mingw-w64-strip
    done
  done
}
