name: Release upload
"on":
  release:
    types:
    - published

jobs:
  package-xgo-build:
    runs-on: ubuntu-latest
    steps:
    - name: Check out code into the Go module directory
      uses: actions/checkout@v4
    - name: Install Go
      if: success()
      uses: actions/setup-go@v4
      with:
        go-version: "1.21"
    - name: Setup XGO
      run: go install github.com/crazy-max/xgo@latest
    - name: Run dist
      run: make dist
    - name: Set environment variables
      shell: bash
      run: |
        echo "GOPATH=${{ runner.workspace }}" >> $GITHUB_ENV
        echo "${{ runner.workspace }}/bin" >> $GITHUB_PATH
    - name: Upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: xgo-build-zip
        path: dist/*.zip

  package-zig-build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - { GOOS: linux,   GOARCH: amd64, ZIPOS: linux_old, ZIG_TARGET: x86_64-linux-gnu.2.18 }
    steps:
    - name: Check out code into the Go module directory
      uses: actions/checkout@v4
    - name: Install Go
      if: success()
      uses: actions/setup-go@v4
      with:
        go-version: 'stable'
    - name: Setup Zig
      uses: goto-bus-stop/setup-zig@v2
    - name: make package
      run: |
        export "CC=zig cc -target ${{ matrix.ZIG_TARGET }}"
        make pkg GOOS=${{ matrix.GOOS }} GOARCH=${{ matrix.GOARCH }} ZIPOS=${{ matrix.ZIPOS }}
    - name: Upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: trdsql-${{ matrix.GOOS }}-${{ matrix.GOARCH }}
        path: dist/*.zip

  package-macos:
    runs-on: macos-latest
    strategy:
      matrix:
        include:
          - { GOOS: darwin, GOARCH: amd64}
          - { GOOS: darwin, GOARCH: arm64}
      fail-fast: false
    steps:
    - name: Check out code into the Go module directory
      uses: actions/checkout@v4
    - name: Install Go
      if: success()
      uses: actions/setup-go@v4
      with:
        go-version: 'stable'
    - name:  make package
      run: make pkg GOOS=${{ matrix.GOOS }} GOARCH=${{ matrix.GOARCH }}
    - name: Upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: trdsql-${{ matrix.GOOS }}-${{ matrix.GOARCH }}
        path: dist/*.zip

  release-upload:
    runs-on: ubuntu-latest
    needs: [package-xgo-build, package-zig-build, package-macos]
    steps:
    -  uses: actions/checkout@v4

    - { uses: actions/download-artifact@v3, with: { name: xgo-build-zip,        path: dist/ } }
    - { uses: actions/download-artifact@v3, with: { name: trdsql-linux-amd64,   path: dist/ } }
    - { uses: actions/download-artifact@v3, with: { name: trdsql-darwin-amd64,  path: dist/ } }
    - { uses: actions/download-artifact@v3, with: { name: trdsql-darwin-arm64,  path: dist/ } }

    - name: Upload Asset to Release with a wildcard
      uses: softprops/action-gh-release@v1
      if: startsWith(github.ref, 'refs/tags/')
      with:
        files: dist/*.zip
    - name: update homebrew
      run: |
        bash .github/update-homebrew-tap.sh
    - name: Pushes homebrew-tap
      uses: dmnemec/copy_file_to_another_repo_action@main
      env:
        API_TOKEN_GITHUB: ${{ secrets.TAP_GITHUB_TOKEN }}
      with:
        source_file: '.github/trdsql.rb'
        destination_repo: 'noborus/homebrew-tap'
        destination_branch: 'master'
        user_email: 'noborusai@gmail.com'
        user_name: 'Noboru Saito'
        commit_message: 'Brew formula update for trdsql'
