# .github/workflows/release.yml

name: Build Release Binaries

on:
  workflow_dispatch:
    inputs:
      release_tag:
        description: 'Release tag (e.g., v1.0.0)'
        required: true
        default: 'v0.0.0-snapshot'

env:
  CARGO_TERM_COLOR: always
  MAIN_BINARY_NAME: serval
  CHECK_BINARY_NAME: serval-check
  XMP_EXTRACT_BINARY_NAME: serval-xmp-extract
  RELEASE_PROFILE_NAME: release-lto

jobs:
  build_release:
    name: Build for ${{ matrix.os }} (${{ matrix.artifact_suffix }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_suffix: linux-amd64
            binary_ext: ""
          - os: macos-latest
            target: x86_64-apple-darwin 
            artifact_suffix: macos-amd64
            binary_ext: ""
          - os: macos-latest
            target: aarch64-apple-darwin 
            artifact_suffix: macos-arm64
            binary_ext: ""
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_suffix: windows-amd64
            binary_ext: ".exe"

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }} 

      - name: Cache Cargo dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ env.RELEASE_PROFILE_NAME }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.target }}-cargo-${{ env.RELEASE_PROFILE_NAME }}-

      - name: Build all binaries in release mode
        run: cargo build --profile ${{ env.RELEASE_PROFILE_NAME }} --bins --target ${{ matrix.target }}

      - name: Prepare artifacts for ${{ matrix.os }} (${{ matrix.artifact_suffix }})
        shell: bash 
        run: |
          # The output directory for a custom profile is target/<target_triple>/<profile_name>
          SOURCE_RELEASE_DIR="target/${{ matrix.target }}/${{ env.RELEASE_PROFILE_NAME }}"
          STAGING_DIR="staging/${{ env.MAIN_BINARY_NAME }}-${{ github.event.inputs.release_tag }}-${{ matrix.artifact_suffix }}"
          mkdir -p "${STAGING_DIR}"

          echo "Source release directory: ${SOURCE_RELEASE_DIR}"
          echo "Staging directory: ${STAGING_DIR}"
          echo "Binary extension: ${{ matrix.binary_ext }}"
          
          cp "${SOURCE_RELEASE_DIR}/${{ env.MAIN_BINARY_NAME }}${{ matrix.binary_ext }}" "${STAGING_DIR}/"
          cp "${SOURCE_RELEASE_DIR}/${{ env.CHECK_BINARY_NAME }}${{ matrix.binary_ext }}" "${STAGING_DIR}/"
          cp "${SOURCE_RELEASE_DIR}/${{ env.XMP_EXTRACT_BINARY_NAME }}${{ matrix.binary_ext }}" "${STAGING_DIR}/"
          
          ls -R "${STAGING_DIR}"

          ARCHIVE_BASENAME="${{ env.MAIN_BINARY_NAME }}-${{ github.event.inputs.release_tag }}-${{ matrix.artifact_suffix }}"
          if [ "${{ matrix.os }}" == "windows-latest" ]; then
            7z a "${ARCHIVE_BASENAME}.zip" "./${STAGING_DIR}/*" 
            echo "ARCHIVE_NAME=${ARCHIVE_BASENAME}.zip" >> $GITHUB_ENV
          else
            tar czvf "${ARCHIVE_BASENAME}.tar.gz" -C "./staging" "${ARCHIVE_BASENAME}"
            echo "ARCHIVE_NAME=${ARCHIVE_BASENAME}.tar.gz" >> $GITHUB_ENV
          fi
          
          echo "Archive created: ${{ env.ARCHIVE_NAME }}"

      - name: Upload artifact for ${{ matrix.os }} (${{ matrix.artifact_suffix }})
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.MAIN_BINARY_NAME }}-${{ github.event.inputs.release_tag }}-${{ matrix.artifact_suffix }}
          path: ${{ env.ARCHIVE_NAME }} 
          if-no-files-found: error 
