kind: pipeline
type: docker
name: ci

trigger:
  event:
    - push
    - pull_request

steps:
  - name: test
    image: rust:latest
    commands:
      - cargo fmt --check
      - cargo clippy -- -D warnings
      - cargo test

  - name: coverage
    image: xd009642/tarpaulin
    privileged: true
    environment:
      GITEA_TOKEN:
        from_secret: gitea_token
    commands:
      - |
        apt-get update -qq && apt-get install -y -qq jq curl
        PCT=$(cargo tarpaulin 2>&1 | awk '/coverage,/{print int($1)}')
        [ -z "$PCT" ] && PCT=0
        if [ "$PCT" -ge 80 ]; then FILL="#4c1"
        elif [ "$PCT" -ge 60 ]; then FILL="#dfb317"
        else FILL="#e05d44"; fi
        echo "PCT=$PCT FILL=$FILL"
        printf '<svg xmlns="http://www.w3.org/2000/svg" width="120" height="20"><rect width="76" height="20" fill="#555"/><rect x="76" width="44" height="20" fill="%s"/><g fill="#fff" text-anchor="middle" font-family="sans-serif" font-size="11"><text x="38" y="14">coverage</text><text x="98" y="14">%s%%</text></g></svg>' "$FILL" "$PCT" > coverage.svg
        CONTENT=$(base64 coverage.svg | tr -d '\n')
        SHA=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
          "https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/contents/coverage.svg?ref=badges" \
          | jq -r '.sha')
        jq -n --arg msg "ci: update coverage badge [CI SKIP]" \
          --arg content "$CONTENT" --arg sha "$SHA" --arg branch "badges" \
          '{message: $msg, content: $content, sha: $sha, branch: $branch}' \
        | curl -fsSL -X PUT \
          -H "Authorization: token $GITEA_TOKEN" \
          -H "Content-Type: application/json" \
          "https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/contents/coverage.svg" \
          -d @-

---
kind: pipeline
type: docker
name: release

trigger:
  event:
    - tag
  ref:
    - refs/tags/[0-9]*

steps:
  - name: build-x86_64
    image: messense/rust-musl-cross:x86_64-musl
    commands:
      - cargo build --release --target x86_64-unknown-linux-musl
      - cp target/x86_64-unknown-linux-musl/release/tmuxido tmuxido-x86_64-linux

  - name: build-aarch64
    image: messense/rust-musl-cross:aarch64-musl
    commands:
      - cargo build --release --target aarch64-unknown-linux-musl
      - cp target/aarch64-unknown-linux-musl/release/tmuxido tmuxido-aarch64-linux

  - name: publish
    image: alpine
    environment:
      GITEA_TOKEN:
        from_secret: gitea_token
    commands:
      - apk add --no-cache curl jq
      - |
        # Delete existing release for this tag if present (handles retag scenarios)
        EXISTING_ID=$(curl -fsSL \
          -H "Authorization: token $GITEA_TOKEN" \
          "https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/releases/tags/$DRONE_TAG" \
          | jq -r '.id // empty')
        if [ -n "$EXISTING_ID" ]; then
          curl -fsSL -X DELETE \
            -H "Authorization: token $GITEA_TOKEN" \
            "https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/releases/$EXISTING_ID"
        fi
        # Read DRONE_TAG via ENVIRON inside awk to avoid Drone's ${VAR} substitution
        # which would replace ${TAG} with an empty string before the shell runs.
        BODY=$(awk '
          BEGIN { tag = ENVIRON["DRONE_TAG"] }
          /^## \[/ { in_section = (index($0, "[" tag "]") > 0); next }
          in_section && /^## \[/ { exit }
          in_section { print }
        ' CHANGELOG.md)
        RELEASE_ID=$(curl -fsSL -X POST \
          -H "Authorization: token $GITEA_TOKEN" \
          -H "Content-Type: application/json" \
          "https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/releases" \
          -d "{\"tag_name\":\"$DRONE_TAG\",\"name\":\"$DRONE_TAG\",\"body\":$(echo "$BODY" | jq -Rs .)}" \
          | jq -r .id)
        for ASSET in tmuxido-x86_64-linux tmuxido-aarch64-linux; do
          curl -fsSL -X POST \
            -H "Authorization: token $GITEA_TOKEN" \
            "https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/releases/$RELEASE_ID/assets" \
            -F "attachment=@$ASSET"
        done
    depends_on:
      - build-x86_64
      - build-aarch64

  - name: publish-github
    image: alpine
    environment:
      GITHUB_TOKEN:
        from_secret: GITHUB_TOKEN
      GITHUB_REPO: cinco/tmuxido
    commands:
      - apk add --no-cache curl jq
      - TAG=${DRONE_TAG}
      - |
        NOTES=$(awk "/^## \[$TAG\]/{found=1; next} found && /^## \[/{exit} found{print}" CHANGELOG.md)
      - |
        EXISTING=$(curl -fsSL \
          -H "Authorization: token $GITHUB_TOKEN" \
          -H "Accept: application/vnd.github.v3+json" \
          "https://api.github.com/repos/$GITHUB_REPO/releases/tags/$TAG" | jq -r '.id // empty')
        if [ -n "$EXISTING" ]; then
          curl -s -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
            "https://api.github.com/repos/$GITHUB_REPO/releases/$EXISTING"
        fi
      - |
        RELEASE_ID=$(curl -fsSL -X POST \
          -H "Authorization: token $GITHUB_TOKEN" \
          -H "Accept: application/vnd.github.v3+json" \
          -H "Content-Type: application/json" \
          "https://api.github.com/repos/$GITHUB_REPO/releases" \
          -d "{\"tag_name\":\"$TAG\",\"name\":\"$TAG\",\"body\":$(echo "$NOTES" | jq -Rs .)}" \
          | jq -r '.id')
      - |
        for ARCH in x86_64 aarch64; do
          curl -fsSL -X POST \
            -H "Authorization: token $GITHUB_TOKEN" \
            -H "Content-Type: application/octet-stream" \
            "https://uploads.github.com/repos/$GITHUB_REPO/releases/$RELEASE_ID/assets?name=tmuxido-${ARCH}-linux" \
            --data-binary @"tmuxido-${ARCH}-linux"
        done
    depends_on:
      - build-x86_64
      - build-aarch64
