# Owlry Plugins build and release automation

default:
    @just --list

# === Build ===

build:
    cargo build --workspace

release:
    cargo build --workspace --release

plugin name:
    cargo build -p owlry-plugin-{{name}} --release

plugins:
    cargo build --workspace --release

# === Quality ===

check:
    cargo check --workspace
    cargo clippy --workspace

test:
    cargo test --workspace

fmt:
    cargo fmt --all

# === Install ===

install-local:
    just plugins
    @for f in target/release/libowlry_plugin_*.so; do \
        sudo install -Dm755 "$$f" /usr/lib/owlry/plugins/$$(basename "$$f"); \
    done
    @echo "Installed all plugins"

# === Version Management ===

show-versions:
    #!/usr/bin/env bash
    for dir in crates/owlry-plugin-*; do
        name=$(basename "$dir")
        ver=$(grep '^version' "$dir/Cargo.toml" | head -1 | cut -d'"' -f2)
        printf "  %-35s %s\n" "$name" "$ver"
    done

# Bump a single plugin version, update Cargo.lock, commit
bump-crate crate new_version:
    #!/usr/bin/env bash
    set -euo pipefail
    toml="crates/{{crate}}/Cargo.toml"
    [ -f "$toml" ] || { echo "Error: $toml not found"; exit 1; }

    old=$(grep '^version' "$toml" | head -1 | sed 's/.*"\(.*\)"/\1/')
    [ "$old" = "{{new_version}}" ] && { echo "{{crate}} already at {{new_version}}"; exit 0; }

    echo "Bumping {{crate}} from $old to {{new_version}}"
    sed -i 's/^version = ".*"/version = "{{new_version}}"/' "$toml"
    cargo check -p {{crate}}
    git add "$toml" Cargo.lock
    git commit -m "chore({{crate}}): bump version to {{new_version}}"
    echo "{{crate}} bumped to {{new_version}}"

# Bump all plugin crates to same version
bump-all new_version:
    #!/usr/bin/env bash
    set -euo pipefail
    for dir in crates/owlry-plugin-*; do
        crate=$(basename "$dir")
        old=$(grep '^version' "$dir/Cargo.toml" | head -1 | sed 's/.*"\(.*\)"/\1/')
        [ "$old" = "{{new_version}}" ] && continue
        echo "Bumping $crate from $old to {{new_version}}"
        sed -i 's/^version = ".*"/version = "{{new_version}}"/' "$dir/Cargo.toml"
    done
    cargo check --workspace
    git add crates/*/Cargo.toml Cargo.lock
    git commit -m "chore: bump all plugins to {{new_version}}"
    echo "All plugins bumped to {{new_version}}"

# === Tagging ===

# Tag a specific plugin (format: {crate}-v{version})
tag-crate crate:
    #!/usr/bin/env bash
    set -euo pipefail
    ver=$(grep '^version' "crates/{{crate}}/Cargo.toml" | head -1 | sed 's/.*"\(.*\)"/\1/')
    tag="{{crate}}-v$ver"
    if git rev-parse "$tag" >/dev/null 2>&1; then
        echo "Tag $tag already exists"
        exit 0
    fi
    git tag -a "$tag" -m "{{crate}} v$ver"
    echo "Created tag $tag"

push-tags:
    git push --tags

# === AUR Package Management ===

# Stage AUR files into git index (handles embedded .git dirs)
aur-stage pkg:
    #!/usr/bin/env bash
    set -euo pipefail
    dir="aur/{{pkg}}"
    [ -d "$dir" ] || { echo "Error: $dir not found"; exit 1; }

    # Build list of files to stage
    files=("$dir/PKGBUILD" "$dir/.SRCINFO")
    for f in "$dir"/*.install; do
        [ -f "$f" ] && files+=("$f")
    done

    if [ -d "$dir/.git" ]; then
        mv "$dir/.git" "$dir/.git.bak"
        git add "${files[@]}"
        mv "$dir/.git.bak" "$dir/.git"
    else
        git add "${files[@]}"
    fi

# Update a specific plugin AUR package PKGBUILD
aur-update-pkg pkg:
    #!/usr/bin/env bash
    set -euo pipefail
    aur_dir="aur/{{pkg}}"
    [ -d "$aur_dir" ] || { echo "Error: $aur_dir not found"; exit 1; }

    crate_dir="crates/{{pkg}}"
    [ -d "$crate_dir" ] || { echo "Error: $crate_dir not found"; exit 1; }

    ver=$(grep '^version' "$crate_dir/Cargo.toml" | head -1 | sed 's/.*"\(.*\)"/\1/')
    tag="{{pkg}}-v$ver"
    url="https://somegit.dev/Owlibou/owlry-plugins/archive/$tag.tar.gz"

    echo "Updating {{pkg}} to $ver (tag: $tag)"
    sed -i "s/^pkgver=.*/pkgver=$ver/" "$aur_dir/PKGBUILD"
    sed -i 's/^pkgrel=.*/pkgrel=1/' "$aur_dir/PKGBUILD"

    echo "Downloading tarball and computing checksum..."
    hash=$(curl -sL "$url" | b2sum | cut -d' ' -f1)
    if [ -z "$hash" ] || [ ${#hash} -lt 64 ]; then
        echo "Error: failed to download or hash $url"
        exit 1
    fi
    sed -i "s|^b2sums=.*|b2sums=('$hash')|" "$aur_dir/PKGBUILD"

    (cd "$aur_dir" && makepkg --printsrcinfo > .SRCINFO)
    echo "{{pkg}} PKGBUILD updated to $ver"

# Publish a specific plugin to aur.archlinux.org
aur-publish-pkg pkg:
    #!/usr/bin/env bash
    set -euo pipefail
    aur_dir="aur/{{pkg}}"
    [ -d "$aur_dir/.git" ] || { echo "Error: $aur_dir has no AUR git repo"; exit 1; }

    cd "$aur_dir"
    ver=$(grep '^pkgver=' PKGBUILD | sed 's/pkgver=//')
    git add -A
    git commit -m "Update to v$ver" || { echo "Nothing to commit"; exit 0; }
    git push origin master
    echo "{{pkg}} v$ver published to AUR!"

# Update ALL plugin AUR packages
aur-update-all:
    #!/usr/bin/env bash
    set -euo pipefail
    for dir in aur/owlry-plugin-*/; do
        pkg=$(basename "$dir")
        [ -f "$dir/PKGBUILD" ] || continue
        echo "=== $pkg ==="
        just aur-update-pkg "$pkg"
        echo ""
    done
    echo "All updated. Run 'just aur-publish-all' to publish."

# Publish ALL plugin AUR packages
aur-publish-all:
    #!/usr/bin/env bash
    set -euo pipefail
    for dir in aur/owlry-plugin-*/; do
        pkg=$(basename "$dir")
        [ -d "$dir/.git" ] || continue
        echo "=== $pkg ==="
        just aur-publish-pkg "$pkg"
        echo ""
    done
    echo "All published!"

# Commit AUR file changes to the plugins repo
aur-commit msg="chore(aur): update PKGBUILDs":
    #!/usr/bin/env bash
    set -euo pipefail
    for dir in aur/*/; do
        pkg=$(basename "$dir")
        [ -f "$dir/PKGBUILD" ] || continue
        just aur-stage "$pkg"
    done
    git diff --cached --quiet && { echo "No AUR changes to commit"; exit 0; }
    git commit -m "{{msg}}"

# Show AUR package status
aur-status:
    #!/usr/bin/env bash
    echo "=== AUR Package Status ==="
    for dir in aur/*/; do
        pkg=$(basename "$dir")
        [ -f "$dir/PKGBUILD" ] || continue
        ver=$(grep '^pkgver=' "$dir/PKGBUILD" | sed 's/pkgver=//')
        if [ -d "$dir/.git" ]; then
            printf "  ✓ %-35s %s\n" "$pkg" "$ver"
        else
            printf "  ✗ %-35s %s (no AUR repo)\n" "$pkg" "$ver"
        fi
    done

# === Release Workflows ===

# Release a single plugin: bump → push → tag → update AUR → publish AUR
release-plugin crate new_version:
    #!/usr/bin/env bash
    set -euo pipefail

    just bump-crate {{crate}} {{new_version}}
    git push

    just tag-crate {{crate}}
    just push-tags

    echo "Waiting for tag to propagate..."
    sleep 3

    just aur-update-pkg {{crate}}
    just aur-commit "chore(aur): update {{crate}} to {{new_version}}"
    git push

    just aur-publish-pkg {{crate}}
    echo ""
    echo "{{crate}} v{{new_version}} released and published to AUR!"

# === Testing ===

aur-test-pkg pkg:
    #!/usr/bin/env bash
    set -euo pipefail
    cd "aur/{{pkg}}"
    echo "Testing {{pkg}} PKGBUILD..."
    makepkg -sf
    echo "Package built successfully!"
    ls -lh *.pkg.tar.zst
