# Maintainer (arch): Maxime Gauduin <alucryd@archlinux.org>
# Contributor: Themaister <maister@archlinux.us>
# Contributor: lifning <definelightning@gmail.com>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Nikita Epifanov <nikgreens@protonmail.com>
# Contributor: bill-auger <bill-auger@programmer.net>


# rationale for inclusion in [libre]:
#  - disable auto-updater/downloader


pkgname=retroarch
pkgver=1.9.6
pkgrel=2
pkgrel+=.parabola2
pkgdesc='Reference frontend for the libretro API'
arch=(x86_64)
arch+=(i686 armv7h)
url=http://www.libretro.com/
license=(GPL)
groups=(libretro)
depends=(
  alsa-lib
  flac
  libass.so
  libavcodec.so
  libavformat.so
  libavutil.so
  libdrm
  libfreetype.so
  libgl
  libpulse
  libswresample.so
  libswscale.so
  libudev.so
  libusb-1.0.so
  mbedtls
  mesa
  miniupnpc
  openal
  qt5-base
  sdl2
  v4l-utils
  zlib
)
depends+=(libmbedcrypto.so)
makedepends=(
  git
  libx11
  libxcb
  libxext
  libxinerama
  libxkbcommon
  libxrandr
  libxv
  libxxf86vm
  vulkan-icd-loader
  wayland
  wayland-protocols
)
optdepends=(
  'libretro-overlays: Collection of overlays'
  'libretro-shaders: Collection of shaders'
  'libxinerama: X11 support'
  'libxrandr: X11 support'
  'python: retroarch-cg2glsl'
  'retroarch-assets-ozone: Ozone menu assets'
  'retroarch-assets-xmb: XMB menu assets'
  'wayland: Wayland support'
)
backup=(etc/retroarch.cfg)
_tag=4a6dc36b1cc026bf337537e2d7527a117727bb91
source=(
  git+https://github.com/libretro/RetroArch.git#tag=${_tag}
  retroarch-config.patch
  disable-auto-update.patch
)
sha256sums=(
  SKIP
  7857cff30c45721b66666828ca9edbb2923817c6c64591be3f58fe019277103e
  cb3915a5ada76e29abd2a8b2c06c8dc50909473b5c53ec112dffab87fcc585e1
)

pkgver() {
  cd RetroArch

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd RetroArch

  patch -Np1 -i ../retroarch-config.patch

  git cherry-pick -n 685948368e6b28b97e1b91bdfe5921519359a033 # FS#66843

  echo "applying disable-auto-update.patch"
  sed -i 's|\(#define DEFAULT_BUILDBOT_SERVER_URL\) ".*|\1 ""|'        config.def.h
  sed -i 's|\(#define DEFAULT_BUILDBOT_ASSETS_SERVER_URL\) ".*|\1 ""|' config.def.h
  patch -Np1 -i ${srcdir}/disable-auto-update.patch
}

build() {
  cd RetroArch

  ./configure \
    --prefix=/usr \
    --disable-builtinflac \
    --disable-builtinmbedtls \
    --disable-builtinminiupnpc \
    --disable-builtinzlib \
    --disable-cg \
    --disable-jack \
    --disable-oss \
    --disable-sdl \
    --enable-dbus
  make
  make -C libretro-common/audio/dsp_filters
  make -C gfx/video_filters
}

package() {
  cd RetroArch

  make DESTDIR="${pkgdir}" install

  install -Dm 644 libretro-common/audio/dsp_filters/*.{dsp,so} -t "${pkgdir}"/usr/lib/retroarch/filters/audio/
  install -Dm 644 gfx/video_filters/*.{filt,so} -t "${pkgdir}"/usr/lib/retroarch/filters/video/
}
