# Maintainer: Peter Jackson <pete@peteonrails.com>
# Local development PKGBUILD - builds from local source tree

pkgname=voxtype
pkgver=0.1.0
pkgrel=1
pkgdesc="Push-to-talk voice-to-text for Linux (optimized for Wayland, works on X11)"
arch=('x86_64' 'aarch64')
url="https://github.com/peteonrails/voxtype"
license=('MIT')
depends=(
    'alsa-lib'
    'gcc-libs'
    'glibc'
)
makedepends=(
    'cargo'
    'clang'
    'cmake'
    'pkgconf'
    # Uncomment for GPU acceleration:
    # 'vulkan-headers'  # for Vulkan (AMD, NVIDIA, Intel)
    # 'cuda'            # for CUDA (NVIDIA only)
)
optdepends=(
    'wtype: keyboard simulation for Wayland (recommended, best CJK support)'
    'ydotool: keyboard simulation fallback (X11/TTY support)'
    'wl-clipboard: clipboard support'
    'libnotify: desktop notifications'
    'pipewire: audio capture (recommended)'
    'pulseaudio: audio capture (alternative)'
    'vulkan-icd-loader: GPU acceleration via Vulkan (requires rebuild with gpu-vulkan feature)'
    'cuda: GPU acceleration for NVIDIA (requires rebuild with gpu-cuda feature)'
)
backup=('etc/voxtype/config.toml')
options=(!lto)

# Build from local source - no download needed
source=()
sha256sums=()

# Point to the project root (two levels up from packaging/arch/)
_srcdir="${PWD%/*/*}"

pkgver() {
    cd "$_srcdir"
    grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)"/\1/'
}

build() {
    cd "$_srcdir"
    export RUSTUP_TOOLCHAIN=stable
    export CARGO_TARGET_DIR=target

    # GPU Acceleration (optional) - uncomment ONE of these lines:
    # local _features="--features gpu-vulkan"   # AMD, NVIDIA, Intel (recommended for AMD)
    # local _features="--features gpu-cuda"     # NVIDIA only
    # local _features="--features gpu-hipblas"  # AMD ROCm

    cargo build --release ${_features:-}
}

check() {
    cd "$_srcdir"
    export RUSTUP_TOOLCHAIN=stable
    cargo test --release
}

package() {
    cd "$_srcdir"

    # Install binary
    install -Dm755 "target/release/voxtype" "$pkgdir/usr/bin/voxtype"

    # Install default configuration
    install -Dm644 "config/default.toml" "$pkgdir/etc/voxtype/config.toml"

    # Install systemd user service
    install -Dm644 "packaging/systemd/voxtype.service" \
        "$pkgdir/usr/lib/systemd/user/voxtype.service"

    # Install documentation
    install -Dm644 "README.md" "$pkgdir/usr/share/doc/$pkgname/README.md"
    install -Dm644 "docs/INSTALL.md" "$pkgdir/usr/share/doc/$pkgname/INSTALL.md"
    install -Dm644 "docs/USER_MANUAL.md" "$pkgdir/usr/share/doc/$pkgname/USER_MANUAL.md"
    install -Dm644 "docs/CONFIGURATION.md" "$pkgdir/usr/share/doc/$pkgname/CONFIGURATION.md"

    # Install license
    install -Dm644 "LICENSE" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

    # Install shell completions
    install -Dm644 "packaging/completions/voxtype.bash" \
        "$pkgdir/usr/share/bash-completion/completions/voxtype"
    install -Dm644 "packaging/completions/voxtype.zsh" \
        "$pkgdir/usr/share/zsh/site-functions/_voxtype"
    install -Dm644 "packaging/completions/voxtype.fish" \
        "$pkgdir/usr/share/fish/vendor_completions.d/voxtype.fish"
}
