name: GenerateBuilds

on:
  push:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  generate-port-o2r:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true
    - name: Install dependencies
      run: sudo apt-get install gcc g++ git cmake ninja-build lsb-release
    - name: ccache
      uses: hendrikmuhs/ccache-action@v1.2.13
      with:
        key: ${{ runner.os }}-o2r-ccache-${{ github.ref }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-o2r-ccache-${{ github.ref }}
          ${{ runner.os }}-o2r-ccache-
    - name: Cache build folders
      uses: actions/cache@v4
      with:
        key: ${{ runner.os }}-o2r-build-${{ github.ref }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-o2r-build-${{ github.ref }}
          ${{ runner.os }}-o2r-build-
        path: |
          tools/Torch/cmake-build-release
    - name: Generate starship.o2r
      run: |
        export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
        make -C tools/Torch type=release -j3
        tools/Torch/cmake-build-release/torch pack port starship.o2r o2r
    - uses: actions/upload-artifact@v4
      with:
        name: starship.o2r
        path: starship.o2r
        retention-days: 1

  build-windows:
    needs: generate-port-o2r
    runs-on: windows-2022
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Build
        run: |
          cmake -S . -B "build/x64" -G "Visual Studio 17 2022" -T v143 -A x64 -DCMAKE_BUILD_TYPE=Release
          cmake --build ./build/x64 --config Release --parallel 10
      - name: Download starship.o2r
        uses: actions/download-artifact@v4
        with:
          name: starship.o2r
          path: ./build/x64/Release
      - name: Create Package
        run: |
          mkdir starship-release
          mv build/x64/Release/Starship.exe starship-release/
          mv build/x64/Release/starship.o2r starship-release/
          mv config.yml starship-release/
          mv assets starship-release/
          Invoke-WebRequest -Uri "https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/master/gamecontrollerdb.txt" -OutFile "starship-release/gamecontrollerdb.txt"
      - name: Upload build
        uses: actions/upload-artifact@v4
        with:
          name: starship-windows
          path: starship-release

  build-macos:
    needs: generate-port-o2r
    runs-on: macOS-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Install dependencies
        run: brew install sdl2 libpng glew ninja cmake libzip nlohmann-json tinyxml2 spdlog vorbis-tools
      - name: Build
        run: |
          cmake -H. -Bbuild-cmake -GNinja -DCMAKE_BUILD_TYPE=Release
          cmake --build build-cmake --config Release -j3
      - name: Download starship.o2r
        uses: actions/download-artifact@v4
        with:
          name: starship.o2r
          path: ./build-cmake
      - name: Create Package
        run: |
          mkdir starship-release
          mv build-cmake/Starship starship-release/
          mv build-cmake/starship.o2r starship-release/
          mv config.yml starship-release/
          mv assets starship-release/
          curl -o starship-release/gamecontrollerdb.txt -sSL 'https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/refs/heads/master/gamecontrollerdb.txt'
      - name: Publish packaged artifacts
        uses: actions/upload-artifact@v4
        with:
            name: starship-mac-x64
            path: starship-release

  build-linux:
    needs: generate-port-o2r
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Update machine
        run: sudo apt update
      - name: Install dependencies
        run: sudo apt-get install gcc g++ git cmake ninja-build lsb-release libsdl2-dev libpng-dev libsdl2-net-dev libzip-dev zipcmp zipmerge ziptool nlohmann-json3-dev libtinyxml2-dev libspdlog-dev libboost-dev libopengl-dev libogg-dev libvorbis-dev
      - name: ccache
        uses: hendrikmuhs/ccache-action@v1.2.14
        with:
          key: linux-ccache-${{ github.ref }}-${{ github.sha }}
          restore-keys: |
            linux-ccache-${{ github.ref }}
            linux-ccache-
      - name: Cache build folders
        uses: actions/cache@v4
        with:
          key: linux-build-${{ github.ref }}-${{ github.sha }}
          restore-keys: |
            linux-build-${{ github.ref }}
            linux-build-
          path: |
            SDL2-2.30.3
            tinyxml2-10.0.0
            libzip-1.10.1
      - name: Install latest SDL
        run: |
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          if [ ! -d "SDL2-2.30.3" ]; then
            wget https://www.libsdl.org/release/SDL2-2.30.3.tar.gz
            tar -xzf SDL2-2.30.3.tar.gz
          fi
          cd SDL2-2.30.3
          ./configure --enable-hidapi-libusb
          make -j 10
          sudo make install
          sudo cp -av /usr/local/lib/libSDL* /lib/x86_64-linux-gnu/
      - name: Install latest tinyxml2
        run: |
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          wget https://github.com/leethomason/tinyxml2/archive/refs/tags/10.0.0.tar.gz
          tar -xzf 10.0.0.tar.gz
          cd tinyxml2-10.0.0
          mkdir -p build
          cd build
          cmake ..
          make
          sudo make install
      - name: Install libzip without crypto
        run: |
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          if [ ! -d "libzip-1.10.1" ]; then
            wget https://github.com/nih-at/libzip/releases/download/v1.10.1/libzip-1.10.1.tar.gz
            tar -xzf libzip-1.10.1.tar.gz
          fi
          cd libzip-1.10.1
          mkdir -p build
          cd build
          cmake .. -DENABLE_COMMONCRYPTO=OFF -DENABLE_GNUTLS=OFF -DENABLE_MBEDTLS=OFF -DENABLE_OPENSSL=OFF
          make
          sudo make install
          sudo cp -av /usr/local/lib/libzip* /lib/x86_64-linux-gnu/
      - name: Download starship.o2r
        uses: actions/download-artifact@v4
        with:
          name: starship.o2r
          path: ./build-cmake
      - name: Build
        run: |
          cmake -H. -Bbuild-cmake -GNinja -DCMAKE_BUILD_TYPE=Release
          cmake --build build-cmake --config Release -j3
          (cd build-cmake && cpack -G External)
          wget -O gamecontrollerdb.txt https://github.com/mdqinc/SDL_GameControllerDB/blob/master/gamecontrollerdb.txt
          mv README.md readme.txt
          mv build-cmake/*.appimage starship.appimage
      - name: Upload build
        uses: actions/upload-artifact@v4
        with:
          name: Starship-linux
          path: |
            starship.appimage
            config.yml
            assets
            gamecontrollerdb.txt

  build-switch:
    needs: generate-port-o2r
    runs-on: ubuntu-latest
    container:
      image: devkitpro/devkita64:20241023
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y ninja-build
          sudo apt-get remove -y cmake
          git -C libultraship remote add nx https://github.com/Net64DD/libultraship.git
          git -C libultraship fetch nx
          git -C libultraship checkout nx/main-nx
          wget https://github.com/Kitware/CMake/releases/download/v3.28.3/cmake-3.28.3-linux-x86_64.sh -O /tmp/cmake.sh
          sudo sh /tmp/cmake.sh --prefix=/usr/local/ --exclude-subdir
          cmake -H. -Bbuild-switch -GNinja -DCMAKE_TOOLCHAIN_FILE=/opt/devkitpro/cmake/Switch.cmake
          cmake --build build-switch --config Release -j3
          wget -O gamecontrollerdb.txt https://github.com/mdqinc/SDL_GameControllerDB/blob/master/gamecontrollerdb.txt
          mv README.md readme.txt
          mv build-switch/*.nro Starship.nro
      - name: Upload build
        uses: actions/upload-artifact@v4
        with:
          name: Starship-switch
          path: |
            Starship.nro
            config.yml
            assets
            gamecontrollerdb.txt
