# Maintainer:

## options
if [[ (-z "$_srcinfo" && -z "$_pkgver") ]]; then
  : ${_autoupdate:=true}
fi

#: ${_dotnet_type=-bin}
: ${_install_path:=usr/lib}

: ${DOTNET_SYSTEM_NET_DISABLEIPV6=1}
export DOTNET_SYSTEM_NET_DISABLEIPV6

_pkgname="ryujinx"
pkgname="$_pkgname-canary"
pkgver=1.3.308
pkgrel=1
pkgdesc="Experimental Nintendo Switch Emulator written in C#"
url="https://git.ryujinx.app/projects/Ryubing"
license=('MIT')
arch=('x86_64')

provides=("$_pkgname")
conflicts=("$_pkgname")

depends=(
  'alsa-lib'
  'fontconfig'
  'hicolor-icon-theme'
  'jack'
  'libpulse'
  'libx11'
  'wayland'
)
makedepends=(
  "dotnet-sdk${_dotnet_type:-}"
  'desktop-file-utils'
)

options=('!strip' '!debug')

_source_ryujinx() {
  _pkgsrc="ryubing"
  _pkgext="tar.gz"
  source=("${_pkgname}-canary-$_pkgver.$_pkgext"::"$url/archive/Canary-$_pkgver.$_pkgext")
  sha256sums=('SKIP')
}

prepare() {
  # ensure dotnet uses correct SDK
  cp "$_pkgsrc"/global.json global.json
}

pkgver() {
  echo "${_pkgver:?}"
}

build() (
  export DOTNET_CLI_HOME="$SRCDEST/nuget-home"
  export DOTNET_CLI_TELEMETRY_OPTOUT=1

  local _runtime="linux-x64"
  local _args=(
    -c Release
    -r "$_runtime"
    --disable-build-servers
    --nologo
    --self-contained true
    -p:DebugType=none
    -p:ExtraDefineConstants=DISABLE_UPDATER
    -p:PublishSingleFile=true
    -p:Version="$pkgver"
    -p:RuntimeIdentifiers="$_runtime"
  )

  echo "Building AVA Interface..."
  dotnet publish "${_args[@]}" -o publish_ava "$_pkgsrc/src/Ryujinx"

  echo "Shutting down dotnet build server in background."
  (timeout -k 45 30 dotnet build-server shutdown) > /dev/null 2>&1 &
)

package() {
  # program
  mkdir -pm755 "$pkgdir/$_install_path/$_pkgname"
  cp -a publish_ava/* "$pkgdir/$_install_path/$_pkgname/"

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

  # launcher
  local _launcher="$pkgdir/usr/share/applications/$_pkgname.desktop"
  install -Dm644 "$_pkgsrc"/distribution/linux/app.ryujinx.Ryujinx.desktop "$_launcher"

  desktop-file-edit --remove-key="Version" "$_launcher"
  desktop-file-edit --set-key="Exec" --set-value="$_pkgname %f" "$_launcher"
  desktop-file-edit --set-icon="$_pkgname" "$_launcher"

  # icon
  install -Dm644 "$_pkgsrc"/distribution/misc/Logo.png "$pkgdir/usr/share/icons/hicolor/256x256/apps/$_pkgname.png"

  # mimetype
  install -Dm644 "$_pkgsrc"/distribution/linux/mime/Ryujinx.xml "$pkgdir/usr/share/mime/packages/$_pkgname.xml"

  # license
  install -Dm644 "$_pkgsrc"/LICENSE.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  # permissions
  find "$pkgdir" -type d -exec chmod 755 {} \;
  find "$pkgdir" -type f -exec chmod 644 {} \;
  chmod 755 "$pkgdir/$_install_path/$_pkgname/Ryujinx"
  chmod 755 "$pkgdir/$_install_path/$_pkgname/Ryujinx.sh"
}

_update_version() {
  : ${_pkgver:=$pkgver}

  if [[ "${_autoupdate::1}" != "t" ]]; then
    return
  fi

  local _response _pkgver_new
  _response=$(curl -Ssf -L --max-redirs 3 "$url/tags.atom")
  _tag=$(
    printf '%s' "$_response" \
      | grep -Po '/tag/\KCanary-([0-9\.]+)' \
      | sort -rV | head -1
  )
  _pkgver_new="${_tag#Canary-}"

  if [ "$_pkgver" != "${_pkgver_new:?}" ]; then
    _pkgver="${_pkgver_new:?}"
  fi
}

_update_version
_source_ryujinx
