# Maintainer: Balló György <ballogyor+arch at gmail dot com>
# Contributor: Giovanni Scafora <giovanni@archlinux.org>
# Contributor: Maxwell Pray a.k.a. Synthead <synthead@gmail.com>

pkgbase=guvcview
pkgname=(guvcview guvcview-common guvcview-qt)
pkgver=2.2.1
pkgrel=2
pkgdesc='Simple interface for capturing and viewing video from v4l2 devices'
arch=('x86_64')
url='http://guvcview.sourceforge.net/'
license=('GPL3')
depends=('ffmpeg' 'gsl' 'gtk3' 'qt6-base' 'portaudio')
makedepends=('cmake')
options=('!emptydirs')
source=("https://downloads.sourceforge.net/$pkgbase/$pkgbase-src-$pkgver.tar.bz2")
sha256sums=('d2adc7ce762961e853c3e16b511bad61504492d12f3e2eb7e30da4a2f7ade5af')
noextract=($pkgbase-src-$pkgver-2.tar.bz2) # missing root dir

prepare() {
  # Fix appdata install dir
  sed -e 's|\/appdata|\/metainfo|' -i $pkgbase-src-$pkgver/data/CMakeLists.txt
  # Build fix for ffmpeg 8
  sed -i 's#FF_PROFILE#AV_PROFILE#g' "$pkgbase-src-$pkgver"/gview_encoder/audio_codecs.c
}

build() {
  # GTK+ version
  cmake -B build-gtk -S $pkgbase-src-$pkgver \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DUSE_SFML=ON
  cmake --build build-gtk

  # Qt version
  cmake -B build-qt -S $pkgbase-src-$pkgver \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DUSE_SFML=ON \
    -DUSE_QT6=ON \
    -DUSE_GTK3=OFF
  cmake --build build-qt
}

package_guvcview-common() {
  pkgdesc="Common files for guvcview"
  depends=('ffmpeg' 'gsl' 'libusb' 'portaudio')

  DESTDIR="$pkgdir" cmake --install build-gtk
  rm -r "$pkgdir"/usr/{bin,share/{man,metainfo,applications}}
  install -Dm644 $pkgbase-src-$pkgver/data/icons/guvcview.png "$pkgdir"/usr/share/icons/hicolor/128x128/apps/guvcview.png
}

package_guvcview() {
  pkgdesc="Simple GTK+ interface for capturing and viewing video from v4l2 devices"
  depends=('guvcview-common' 'gtk3')

  DESTDIR="$pkgdir" cmake --install build-gtk
  rm -r "$pkgdir"/usr/{lib,share/{pixmaps,locale}}
  sed -i 's/^Icon=.*/Icon=guvcview/' "$pkgdir"/usr/share/applications/guvcview.desktop
}

package_guvcview-qt() {
  pkgdesc="Simple Qt interface for capturing and viewing video from v4l2 devices"
  depends=('guvcview-common' 'qt6-base')

  DESTDIR="$pkgdir" cmake --install build-qt
  rm -r "$pkgdir"/usr/{lib,share/{pixmaps,locale}}
  mv "$pkgdir"/usr/bin/guvcview{,-qt}
  mv "$pkgdir"/usr/share/man/man1/guvcview{,-qt}.1.gz
  mv "$pkgdir"/usr/share/applications/guvcview{,-qt}.desktop
  sed -i 's/Exec=guvcview/Exec=guvcview-qt/
          s/^Icon=.*/Icon=guvcview/' "$pkgdir"/usr/share/applications/guvcview-qt.desktop
  mv "$pkgdir"/usr/share/metainfo/guvcview{,-qt}.appdata.xml
  sed -i 's/net.sourceforge.guvcview/net.sourceforge.guvcview-qt/
          s/guvcview.desktop/guvcview-qt.desktop/' "$pkgdir"/usr/share/metainfo/guvcview-qt.appdata.xml
}
