name: Build libsm64

on:
  push:
    branches:
      - "**"
    tags-ignore:
      - "**"
  pull_request:

jobs:
  Build:
    name: Build libsm64 for ${{ matrix.identifier }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - identifier: linux
            os: ubuntu-latest
          - identifier: windows
            os: ubuntu-latest
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout project
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'

      - name: (Windows) Install MinGW
        if: ${{ startsWith(matrix.identifier, 'windows') }}
        run: |
          sudo apt-get update -y -qq
          sudo apt-get install -y mingw-w64
          sudo update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
          sudo update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix

      - name: (Windows) Prepare cross compilation
        if: ${{ startsWith(matrix.identifier, 'windows') }}
        shell: sh
        run: |
          echo "OS=Windows_NT" >> $GITHUB_ENV

      - name: (Linux) Install dependencies
        if: ${{ startsWith(matrix.identifier, 'linux') }}
        run: |
          sudo add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu `lsb_release -sc` main universe restricted multiverse"
          sudo apt-get update -y -qq
          sudo apt-get install -y libsdl2-dev libglew-dev

      - name: Build shared library
        shell: bash
        run: |
          if [[ "${{ matrix.identifier }}" == "windows" ]]; then
              make lib -j $(nproc 2>/dev/null || sysctl -n hw.ncpu) \
                CC=x86_64-w64-mingw32-gcc \
                CXX=x86_64-w64-mingw32-g++
          else
              make lib -j $(nproc 2>/dev/null || sysctl -n hw.ncpu)
          fi

      - name: (Linux) Build test executable
        if: ${{ startsWith(matrix.identifier, 'linux') }}
        run: |
          make test -j`nproc`

      - name: Upload build
        uses: actions/upload-artifact@v3
        with:
          name: libsm64_${{ matrix.identifier }}
          path: |
            ./README.md
            ./dist
            ./run-test

  Release:
    name: Snapshot Release
    runs-on: ubuntu-latest
    needs: Build

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v3

      - name: Create release
        id: create_release
        shell: bash
        run: |
          RESPONSE=$(curl -s -X POST \
            -H "Authorization: token ${{ secrets.LIBSM64_TOKEN }}" \
            -H "Content-Type: application/json" \
            "https://git.nepu.men/api/v1/repos/NepuShiro/libsm64/releases" \
            -d '{
              "tag_name": "latest",
              "name": "Snapshot Build",
              "prerelease": true
            }')

          echo "$RESPONSE"

          RELEASE_ID=$(echo "$RESPONSE" | grep -o '"id":[0-9]*' | head -n 1 | grep -o '[0-9]*')
          echo "release_id=$RELEASE_ID" >> $GITHUB_OUTPUT

      - name: Upload Linux artifact
        shell: bash
        run: |
          curl -X POST \
            -H "Authorization: token ${{ secrets.LIBSM64_TOKEN }}" \
            -F "attachment=@libsm64_linux/run-test" \
            "https://git.nepu.men/api/v1/repos/NepuShiro/libsm64/releases/${{ steps.create_release.outputs.release_id }}/assets?name=run-test-linux"

      - name: Upload Windows artifact
        shell: bash
        run: |
          tar -czf windows.tar.gz libsm64_windows
          curl -X POST \
            -H "Authorization: token ${{ secrets.LIBSM64_TOKEN }}" \
            -F "attachment=@windows.tar.gz" \
            "https://git.nepu.men/api/v1/repos/NepuShiro/libsm64/releases/${{ steps.create_release.outputs.release_id }}/assets?name=libsm64-windows.tar.gz"

      - name: Upload README
        shell: bash
        run: |
          curl -X POST \
            -H "Authorization: token ${{ secrets.LIBSM64_TOKEN }}" \
            -F "attachment=@README.md" \
            "https://git.nepu.men/api/v1/repos/NepuShiro/libsm64/releases/${{ steps.create_release.outputs.release_id }}/assets?name=README.md"