# Maintainer: aur.chaotic.cx

: ${_install_path:=usr/lib}

_pkgname="nyrna"
pkgname="$_pkgname"
pkgver=2.27.2
pkgrel=2
pkgdesc='Suspend games and applications at any time and resume whenever you wish'
url="https://github.com/Merrit/nyrna"
license=('GPL-3.0-or-later')
arch=('x86_64')

depends=(
  'gtk3'
  'libappindicator-gtk3'
  'libkeybinder3'
  'util-linux'
  'wmctrl'
  'xdotool'
)
makedepends=(
  'clang'
  'cmake'
  'fvm'
  'git'
  'lld'
  'llvm'
  'ninja'
  'patchelf'
)

_pkgsrc="$_pkgname-$pkgver"
_pkgext="tar.gz"
source=("$_pkgsrc.$_pkgext"::"https://github.com/Merrit/nyrna/archive/refs/tags/v$pkgver.$_pkgext")
sha256sums=('85849f51aad90fb8f17ea73ba9863d173d6c9fb4d25b09d1be5de8cd408ef790')

prepare() {
  sed -i 's/\-Werror//g' "$_pkgsrc/linux/CMakeLists.txt"
}

build() {
  export FVM_CACHE_PATH="$SRCDEST/fvm-cache"

  cd "$_pkgsrc"
  local _ver_series=$(grep -Pom1 '^\s*flutter: "[^0-9]*\K[0-9]+\.[0-9]+' pubspec.lock)
  local _ver_latest=$(fvm releases | grep -Po "${_ver_series//./\.}\.[0-9]+" | tail -1)

  fvm install "${_ver_latest}"
  fvm use "${_ver_latest}" --force

  fvm flutter --disable-analytics
  #fvm flutter pub upgrade --major-versions
  fvm flutter --no-version-check pub get

  fvm dart run build_runner build --delete-conflicting-outputs
  fvm flutter build linux --no-pub --release
}

package() {
  cd "$_pkgsrc"

  # files
  install -Dm755 "build/linux/x64/release/bundle/$_pkgname" -t "$pkgdir/$_install_path/$_pkgname/"
  cp -r "build/linux/x64/release/bundle/data" "$pkgdir/$_install_path/$_pkgname/"
  cp -r "build/linux/x64/release/bundle/lib" "$pkgdir/$_install_path/$_pkgname/"

  # symlink
  mkdir -pm755 "$pkgdir/usr/bin/"
  ln -s "/$_install_path/$_pkgname/$_pkgname" "$pkgdir/usr/bin/$_pkgname"

  # launcher
  install -Dm644 "packaging/linux/codes.merritt.Nyrna.desktop" -t "$pkgdir/usr/share/applications/"

  # icon
  install -Dm644 "assets/icons/codes.merritt.Nyrna.svg" -t "$pkgdir/usr/share/icons/hicolor/scalable/apps/"

  # fix insecure rpath
  find "$pkgdir/$_install_path/$_pkgname/lib" -type f -name "*.so" -exec patchelf --set-rpath '$ORIGIN' {} \;
}
