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

# if tests fail due to timeout, you can try to increase the timeout
# by setting SYNCTHING_TEST_TIMEOUT_FACTOR

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

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

# set to non-empty string to enable KIO plugin to show Syncthing actions in
# Dolphin file browser
_enable_kio_plugin=${SYNCTHING_TRAY_ENABLE_KIO_PLUGIN_STATIC_COMPAT:-0}

# set to non-empty string to enable Plasmoid for Plasma 5 desktop
_enable_plasmoid=${SYNCTHING_TRAY_ENABLE_PLASMOID_STATIC_COMPAT:-0}

# set whether libsyncthing should be enabled: either ON or OFF
_enable_libsyncthing=${SYNCTHING_TRAY_ENABLE_LIBSYNCTHING:-ON}

# set whether the Qt Quick GUI should be enabled: either ON or OFF
_enable_quick_gui=${SYNCTHING_TRAY_ENABLE_QUICK_GUI:-ON}

[[ $_enable_kio_plugin == 0 ]] && _enable_kio_plugin=
[[ $_enable_plasmoid == 0 ]] && _enable_plasmoid=
[[ $_enable_quick_gui == 0 ]] && _enable_quick_gui=

_reponame=syncthingtray
pkgname=static-compat-syncthingtray
_name=${pkgname#static-compat-}
pkgver=2.0.10
[[ $BUILD_BEFORE_TAGGING ]] && _pkgver=branch=master || _pkgver=tag=v$pkgver
pkgrel=2.1
arch=('i686' 'x86_64' 'armv6h' 'armv7h' 'aarch64')
pkgdesc='Tray application for Syncthing'
license=(GPL-2.0-or-later)
depends=('static-compat-qtutilities' 'static-compat-qtforkawesome' 'static-compat-boost' 'static-compat-qt6-svg' 'static-compat-qt6-wayland' 'static-compat-openssl')
[[ $_webview_provider == none ]] && depends+=('static-compat-qt6-base')
[[ $_webview_provider == webkit ]] && depends+=('static-compat-qt6-webkit')
[[ $_webview_provider == webengine ]] && depends+=('static-compat-qt6-webengine')
[[ $_js_provider == script ]] && depends+=('static-compat-qt6-script')
[[ $_js_provider == qml ]] && depends+=('static-compat-qt6-declarative')
[[ $_enable_quick_gui ]] && depends+=('static-compat-qt6-declarative')
[[ $_enable_kio_plugin ]] && optdepends+=('static-compat-kio: KIO plugin for Syncthing actions in Dolphin')
[[ $_enable_plasmoid ]] && optdepends+=('static-compat-plasma-workspace: Plasmoid for Plasma 6 desktop')
makedepends=('static-compat-cmake' 'ninja' 'static-compat-qt6-tools' 'static-compat-qt6-translations' 'breeze-icons' 'clang' 'git')
[[ $_enable_libsyncthing == ON ]] && makedepends+=('go')
checkdepends=('static-compat-cppunit' 'syncthing' 'iproute2')
[[ $_enable_kio_plugin ]] && makedepends+=('static-compat-kio')
[[ $_enable_plasmoid ]] && makedepends+=('static-compat-plasma-framework' 'extra-cmake-modules')
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")
[[ $_enable_libsyncthing == ON ]] && source+=("syncthing::$_github_url/syncthing")
sha256sums=('SKIP'
            'SKIP')

[[ $_enable_libsyncthing == ON ]] && _disable_libsyncthing=OFF || _disable_libsyncthing=ON

ephemeral_port() {
  comm -23 <(seq 49152 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | shuf | head -n 1
}

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() {
  check_buildoption ccache y && ccache_args='
    -DCMAKE_C_COMPILER_LAUNCHER=ccache
    -DCMAKE_CXX_COMPILER_LAUNCHER=ccache'
  export CXXFLAGS+=' -static-libgcc -static-libstdc++'
  export LDFLAGS+=' -static-libgcc -static-libstdc++'
  source static-compat-environment
  export PATH=$PWD:$PATH
  printf '#!/usr/bin/bash\nexec /usr/bin/pkg-config --static "$@"\n' > pkg-config
  chmod +x pkg-config

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

  local additional_args=
  [[ $_enable_kio_plugin ]] || additional_args+=' -DNO_FILE_ITEM_ACTION_PLUGIN=ON'
  [[ $_enable_plasmoid ]] || additional_args+=' -DNO_PLASMOID=ON'
  [[ $_enable_quick_gui ]] || additional_args+=' -DQUICK_GUI:BOOL=ON -DQUICK_GUI_CONTROLS_STYLE:STRING=Fusion'

  static-compat-cmake -G Ninja \
    $ccache_args \
    -DCMAKE_SKIP_INSTALL_RPATH=ON \
    -DAPPEND_GIT_REVISION:BOOL=OFF \
    -DQT_PACKAGE_PREFIX:STRING='Qt6' \
    -DBUILTIN_TRANSLATIONS:BOOL=ON \
    -DBUILTIN_TRANSLATIONS_OF_QT:BOOL=ON \
    -DWEBVIEW_PROVIDER="${_webview_provider}" \
    -DJS_PROVIDER="${_js_provider}" \
    -DSYSTEMD_SUPPORT=ON \
    -DBUILTIN_ICON_THEMES:STRING='breeze;breeze-dark' \
    -DIMAGE_FORMAT_SUPPORT:STRING='Gif;ICO;Jpeg' \
    -DSVG_SUPPORT:BOOL=ON \
    -DSVG_ICON_SUPPORT:BOOL=ON \
    -DBoost_USE_STATIC_RUNTIME:BOOL=ON \
    -DGLIB2_USE_PKG_CONFIG:BOOL=ON \
    -DWAYLAND_USE_PKG_CONFIG:BOOL=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_harfbuzz=TRUE \
    -DNO_LIBSYNCTHING:BOOL="${_disable_libsyncthing}" \
    -DUSE_LIBSYNCTHING:BOOL="${_enable_libsyncthing}" \
    -DSETUP_TOOLS:BOOL=ON \
    $additional_args \
    .
  # fix linker flags for libharfbuzz (setting HARFBUZZ_LIBRARIES does not work)
  sed -i -e 's|/usr/static-compat/lib/libharfbuzz.a|/usr/static-compat/lib/libharfbuzz.a /usr/static-compat/lib/libfreetype.a /usr/static-compat/lib/libbz2.a|g' build.ninja
  cmake --build . --verbose
}

check() {
  check_buildoption ccache y && ccache_args='
    -DCMAKE_C_COMPILER_LAUNCHER=ccache
    -DCMAKE_CXX_COMPILER_LAUNCHER=ccache'
  export CXXFLAGS+=' -static-libgcc -static-libstdc++'
  export LDFLAGS+=' -static-libgcc -static-libstdc++'
  source static-compat-environment
  export PATH=$PWD:$PATH

  cd "$srcdir/$_reponame"
  # https://github.com/syncthing/syncthing/issues/8785
  HOME="$(mktemp -p "$PWD" -d testhome.XXX)" QT_QPA_PLATFORM=offscreen SYNCTHING_PORT=$(ephemeral_port) SYNCTHING_TEST_TIMEOUT_FACTOR=3 ninja check
}

package() {
  source static-compat-environment
  cd "$srcdir/$_reponame"
  DESTDIR="${pkgdir}" ninja install
}
