name: Publish

on:
  push:
    tags:
      - '*'

jobs:
  publish:
    name: ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            use-cross: false

          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            use-cross: false

          - os: ubuntu-latest
            target: arm-unknown-linux-gnueabihf
            use-cross: true

          - os: windows-latest
            target: x86_64-pc-windows-gnu
            use-cross: false

          - os: windows-latest
            target: x86_64-pc-windows-msvc
            use-cross: false

          - os: macos-latest
            target: x86_64-apple-darwin
            use-cross: false

          - os: macos-latest
            target: aarch64-apple-darwin
            use-cross: false

          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            use-cross: true

          - os: ubuntu-latest
            target: armv7-unknown-linux-gnueabihf
            use-cross: true

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 1

    - name: Set the version
      shell: bash
      if: env.SD_VERSION == ''
      run: |
        echo "SD_VERSION=$GITHUB_REF_NAME" >> $GITHUB_ENV
        echo "version is: ${{ env.SD_VERSION }}"

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

    - name: Setup native compilation
      if: ${{ matrix.use-cross == false }}
      shell: bash
      run: |
        echo "CARGO=cargo" >> $GITHUB_ENV

    - name: Setup cross compilation
      if: ${{ matrix.use-cross == true }}
      shell: bash
      run: |
        dir="$RUNNER_TEMP/cross-download"
        mkdir "$dir"
        echo "$dir" >> $GITHUB_PATH
        cd "$dir"
        curl -LO "https://github.com/cross-rs/cross/releases/download/v0.2.5/cross-x86_64-unknown-linux-musl.tar.gz"
        tar xf cross-x86_64-unknown-linux-musl.tar.gz
        echo "CARGO=cross" >> $GITHUB_ENV
        echo "RUSTFLAGS=--cfg sd_cross_compile" >> $GITHUB_ENV
        echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV

    - name: Build
      shell: bash
      run: |
        $CARGO --version
        $CARGO build --release --locked --target ${{ matrix.target }}
        # Handle windows being an oddity
        if [ "${{ matrix.os }}" = "windows-latest" ]; then
          echo "BIN_NAME=sd.exe" >> $GITHUB_ENV
        else
          echo "BIN_NAME=sd" >> $GITHUB_ENV
        fi

    - name: Setup archive
      shell: bash
      run: |
        staging="sd-${{ env.SD_VERSION }}-${{ matrix.target }}"
        mkdir -p "$staging"

        cp -r {README.md,LICENSE,CHANGELOG.md,gen/*} "$staging"
        if [ "${{ matrix.os }}" = "windows-latest" ]; then
          cp "target/${{ matrix.target }}/release/sd.exe" "$staging/"
          7z a "$staging.zip" "$staging"
          echo "ASSET=$staging.zip" >> $GITHUB_ENV
        else
          cp "target/${{ matrix.target }}/release/sd" "$staging/"
          tar czf "$staging.tar.gz" "$staging"
          echo "ASSET=$staging.tar.gz" >> $GITHUB_ENV
        fi

    - name: Upload binaries to release
      uses: svenstaro/upload-release-action@2.7.0
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: ${{ env.ASSET }}
        asset_name: ${{ env.ASSET }}
        tag: ${{ github.ref }}
