name: Tagged release

on:
  push:
    tags:
      - "*"

env:
  CARGO_TERM_COLOR: always

jobs:
  prepare-release:
    runs-on: linux-arm64
    outputs:
      release_id: ${{ steps.release.outputs.release_id }}
    steps:
      - name: Check out the tag
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Test release-note generation
        run: python3 .gitea/scripts/release.py test

      - name: Create draft release
        id: release
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_SERVER_URL: ${{ gitea.server_url }}
          GITEA_SHA: ${{ gitea.sha }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: python3 .gitea/scripts/release.py prepare

  build-unix:
    needs: prepare-release
    runs-on: linux-arm64
    container: ghcr.io/rust-cross/cargo-zigbuild@sha256:82af75c41958c2af2787e8bedd912da7678a9438937e223e9d83d006d747b38b
    steps:
      - name: Check out the tag
        env:
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_SERVER_URL: ${{ gitea.server_url }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          set -eu
          git init
          git -c http.extraHeader="Authorization: token $GITEA_TOKEN" fetch --depth=1 \
            "$GITEA_SERVER_URL/$GITEA_REPOSITORY.git" "refs/tags/$GITEA_REF_NAME"
          git checkout --detach FETCH_HEAD

      - name: Build, package, and upload Unix releases
        env:
          CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS: -C link-arg=-Wl,-headerpad,0x1000
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: |
          set -eu
          version=$(printf '%s' "$GITEA_REF_NAME" | sed 's/[^A-Za-z0-9._-]/-/g')
          for build in \
            'aarch64-apple-darwin|aarch64-apple-darwin|darwin-arm64' \
            'x86_64-apple-darwin|x86_64-apple-darwin|darwin-x64' \
            'aarch64-unknown-linux-gnu|aarch64-unknown-linux-gnu.2.28|linux-arm64' \
            'x86_64-unknown-linux-gnu|x86_64-unknown-linux-gnu.2.28|linux-x64'
          do
            target=${build%%|*}
            rest=${build#*|}
            build_target=${rest%%|*}
            platform=${rest#*|}
            cargo zigbuild --release --locked --target "$build_target" \
              --package ironstorage-cli \
              --package ironstorage-tui \
              --package ironstorage-desktop

            cli="ironstorage-cli-${version}-${platform}"
            desktop="ironstorage-desktop-${version}-${platform}"
            mkdir -p "dist/$cli"
            cp "target/$target/release/ironstorage" "dist/$cli/"
            cp "target/$target/release/ironstorage-tui" "dist/$cli/"
            tar -C dist -czf "dist/${cli}.tar.gz" "$cli"
            rm -r "dist/$cli"

            case "$platform" in
              darwin-*)
                desktop_asset="dist/${desktop}.dmg"
                cargo run --release --locked --package ironstorage-macos-packager -- \
                  "$GITEA_REF_NAME" "target/$target/release" \
                  assets/icon-candidates/vault-classic@2x.png "$desktop_asset"
                ;;
              *)
                desktop_asset="dist/${desktop}.tar.gz"
                mkdir -p "dist/$desktop"
                cp "target/$target/release/ironstorage" "dist/$desktop/"
                cp "target/$target/release/ironstorage-tui" "dist/$desktop/"
                cp "target/$target/release/ironstorage-desktop" "dist/$desktop/"
                tar -C dist -czf "$desktop_asset" "$desktop"
                rm -r "dist/$desktop"
                ;;
            esac

            for asset in "dist/${cli}.tar.gz" "$desktop_asset"; do
              name=$(basename "$asset")
              curl --fail --silent --show-error --retry 3 \
                -H "Authorization: token $GITEA_TOKEN" \
                -H "Content-Type: application/octet-stream" \
                --data-binary "@$asset" \
                "$GITEA_API_URL/repos/$GITEA_REPOSITORY/releases/$RELEASE_ID/assets?name=$name"
            done
          done

  build-windows:
    needs:
      - prepare-release
      - build-unix
    runs-on: linux-arm64
    container: messense/cargo-xwin@sha256:4696dd4e79edf8569fa99c4b06bd99273e0501c7adc983aa61d57945f795bef0
    steps:
      - name: Check out the tag
        env:
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_SERVER_URL: ${{ gitea.server_url }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          set -eu
          git init
          git -c http.extraHeader="Authorization: token $GITEA_TOKEN" fetch --depth=1 \
            "$GITEA_SERVER_URL/$GITEA_REPOSITORY.git" "refs/tags/$GITEA_REF_NAME"
          git checkout --detach FETCH_HEAD

      - name: Install the repository Rust toolchain
        run: rustup toolchain install 1.92.0 --profile minimal --target x86_64-pc-windows-msvc

      - name: Cross-build release binaries
        run: >-
          cargo +1.92.0 xwin build --release --locked
          --target x86_64-pc-windows-msvc
          --package ironstorage-cli
          --package ironstorage-tui
          --package ironstorage-desktop

      - name: Package CLI, TUI, and desktop application
        env:
          TAG: ${{ gitea.ref_name }}
        run: |
          set -eu
          version=$(printf '%s' "$TAG" | sed 's/[^A-Za-z0-9._-]/-/g')
          cli="ironstorage-cli-${version}-windows-x64"
          desktop="ironstorage-desktop-${version}-windows-x64"
          release=target/x86_64-pc-windows-msvc/release
          mkdir -p "dist/$cli" "dist/$desktop"
          cp "$release/ironstorage.exe" "$release/ironstorage-tui.exe" "dist/$cli/"
          cp "$release/ironstorage.exe" "$release/ironstorage-tui.exe" \
            "$release/ironstorage-desktop.exe" "dist/$desktop/"
          tar -C dist -czf "dist/${cli}.tar.gz" "$cli"
          tar -C dist -czf "dist/${desktop}.tar.gz" "$desktop"
          rm -r "dist/$cli" "dist/$desktop"

      - name: Upload release packages
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: |
          set -eu
          for asset in dist/*.tar.gz; do
            name=$(basename "$asset")
            curl --fail --silent --show-error --retry 3 \
              -H "Authorization: token $GITEA_TOKEN" \
              -H "Content-Type: application/octet-stream" \
              --data-binary "@$asset" \
              "$GITEA_API_URL/repos/$GITEA_REPOSITORY/releases/$RELEASE_ID/assets?name=$name"
          done

  publish-release:
    needs:
      - prepare-release
      - build-unix
      - build-windows
    runs-on: linux-arm64
    steps:
      - name: Check out the tag
        uses: actions/checkout@v4

      - name: Publish complete release
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: python3 .gitea/scripts/release.py publish
