# Temperlux release pipeline — tag → build binary + publish to crates.io + GitHub Release
when:
  - event: tag
    ref: refs/tags/v*

steps:
  - name: release
    image: rust:1.96-slim
    commands:
      - |
        apt-get update -qq && apt-get install -qq -y libddcutil-dev libgtk-4-dev libadwaita-1-dev pkg-config wget git > /dev/null 2>&1

        echo "=== Building temperlux ${CI_COMMIT_TAG} ==="
        cargo build --release --features gui 2>&1

        # Package binaries
        mkdir -p dist
        cp target/release/temperlux dist/
        cp target/release/temperlux-gui dist/
        cp contrib/temperlux.desktop dist/
        cp LICENSE NOTICE README.md CHANGELOG.md dist/
        cp Makefile dist/

        # Create tarball
        ARCHIVE="temperlux-${CI_COMMIT_TAG}-x86_64-linux.tar.gz"
        tar -czf "$ARCHIVE" -C dist .
        echo "Created ${ARCHIVE} ($(du -h "$ARCHIVE" | cut -f1))"

        # Install gh CLI
        wget -q -O /tmp/gh.tar.gz https://github.com/cli/cli/releases/download/v2.74.0/gh_2.74.0_linux_amd64.tar.gz
        tar -xzf /tmp/gh.tar.gz -C /tmp && cp /tmp/gh_*/bin/gh /usr/local/bin/gh

        # Create GitHub Release with assets (delete existing draft if any)
        export GH_TOKEN="$GPR_TOKEN"
        BODY=$(git log $(git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo "")..HEAD --pretty=format:"- %s" 2>/dev/null || git log -5 --pretty=format:"- %s")
        gh release delete "$CI_COMMIT_TAG" --yes 2>/dev/null || true
        gh release create "$CI_COMMIT_TAG" "$ARCHIVE" \
          --title "Temperlux ${CI_COMMIT_TAG}" \
          --notes "$BODY"

        # Publish to crates.io (no GUI deps needed — publishes default features only)
        echo "=== Publishing to crates.io ==="
        cargo publish --no-verify --allow-dirty --token "$CARGO_REGISTRY_TOKEN" 2>&1
