#!/usr/bin/env bash

version=$1
sha256sum=$2

if [[ -z "$version" || -z "$sha256sum" ]]; then
    echo "Usage: $0 <version> <sha256sum>" >&2
    exit 1
fi

cat <<EOF
# Maintainer: Valentin Batz <valentin.batz+archlinux@posteo.de>
pkgname=mdns-browser
pkgver=$version
pkgrel=1
pkgdesc="A cross platform mDNS-Browser app written in Rust using tauri and leptos "
arch=('x86_64')
url="https://github.com/hrzlgnm/mdns-browser"
license=('MIT')
depends=('cairo' 'desktop-file-utils' 'gdk-pixbuf2' 'glib2' 'gtk3' 'hicolor-icon-theme' 'libsoup' 'pango' 'webkit2gtk-4.1' 'openssl')
conflicts=('mdns-browser-bin')
makedepends=('cargo' 'git' 'file' 'appmenu-gtk-module' 'libappindicator-gtk3' 'librsvg' 'base-devel' 'curl' 'wget' 'rustup' 'webkit2gtk-4.1') options=('!strip' '!emptydirs')
source=("\$pkgname-v\$pkgver.tar.gz::https://github.com/hrzlgnm/\$pkgname/archive/refs/tags/\$pkgname-v\$pkgver.tar.gz")
sha256sums=('$sha256sum')
_builddir="\$pkgname-\$pkgname-v\$pkgver"
prepare() {
    cd "\$srcdir/\$_builddir" || exit 1
    export RUSTUP_TOOLCHAIN=stable
    cargo --locked install trunk@0.21.14 --no-default-features --features rustls
    cargo --locked install tauri-cli@2.8.4
    cargo --locked install cargo-auditable@0.7.0
    rustup toolchain install \$RUSTUP_TOOLCHAIN --target wasm32-unknown-unknown --profile minimal --no-self-update
    cargo fetch --locked --target "\$(rustc -vV | sed -n 's/host: //p')"
    cargo fetch --locked --target wasm32-unknown-unknown
}
build() {
    cd "\$srcdir/\$_builddir" || exit 1
    # unfortunately LTOFLAGS -flto=auto set by /etc/makepkg.conf break linking as those are added to CFLAGS automatically
    # building will bail out with something like: undefined reference to 'ring_core_0_17_8_OPENSSL_ia32cap_P' when -flto=auto is set
    export CFLAGS="\${CFLAGS//-flto=auto//}"
    cargo --locked --frozen auditable tauri build -b deb || true
}
check() {
    cd "\$srcdir/\$_builddir" || exit 1
    export RUSTUP_TOOLCHAIN=stable
    cargo test --locked --frozen --package=models
}
package() {
   # use the intermediate artifacts generated by the bundler for creating the .deb
    cd "\${srcdir}/\${_builddir}/target/release/bundle/deb/\${pkgname}_\${pkgver}_amd64/data" || exit 1
    install -Dm755 usr/bin/mdns-browser "\$pkgdir"/usr/bin/mdns-browser
    install -Dm644 usr/share/applications/mdns-browser.desktop "\$pkgdir"/usr/share/applications/mdns-browser.desktop
    install -Dm644 usr/share/icons/hicolor/128x128/apps/mdns-browser.png "\$pkgdir"/usr/share/icons/hicolor/128x128/apps/mdns-browser.png
    install -Dm644 usr/share/icons/hicolor/256x256@2/apps/mdns-browser.png "\$pkgdir"/usr/share/icons/hicolor/256x256@2/apps/mdns-browser.png
    install -Dm644 usr/share/icons/hicolor/32x32/apps/mdns-browser.png "\$pkgdir"/usr/share/icons/hicolor/32x32/apps/mdns-browser.png
    install -Dm644 usr/share/licenses/mdns-browser/LICENSE "\$pkgdir"/usr/share/licenses/mdns-browser/LICENSE
}
EOF
