name: Build binaries

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v3

      - name: Cache
        id: cache-1
        uses: actions/cache@v3
        with:
          path: cache
          key: ${{ runner.os }}-cache-1

      - name: Download devkitPPC r42.2-1, libogc 2.4.0 and required tools
        if: steps.cache-1.outputs.cache-hit != 'true'
        # general-tools is needed for bin2s and gamecube-tools is needed for elf2dol
        run: | 
          mkdir cache && cd cache
          wget "https://wii.leseratte10.de/devkitPro/file.php/devkitPPC-r42.2-1-linux_x86_64.pkg.tar.zst"
          wget "https://wii.leseratte10.de/devkitPro/file.php/devkitppc-rules-1.1.1-1-any.pkg.tar.xz"
          wget "https://wii.leseratte10.de/devkitPro/file.php/libogc-2.4.0-1-any.pkg.tar.zst"
          wget "https://wii.leseratte10.de/devkitPro/file.php/general-tools-1.2.0-2-linux_x86_64.pkg.tar.xz"
          wget "https://wii.leseratte10.de/devkitPro/file.php/gamecube-tools-1.0.3-1-linux_x86_64.pkg.tar.xz"
          cd ..

      - name: Extract downloaded files
        # general-tools is needed for bin2s and gamecube-tools is needed for elf2dol
        run: | 
          tar -xf cache/devkitPPC-r42.2-1-linux_x86_64.pkg.tar.zst --strip-components=1
          tar -xf cache/devkitppc-rules-1.1.1-1-any.pkg.tar.xz --strip-components=1
          tar -xf cache/libogc-2.4.0-1-any.pkg.tar.zst --strip-components=1
          tar -xf cache/general-tools-1.2.0-2-linux_x86_64.pkg.tar.xz --strip-components=1
          tar -xf cache/gamecube-tools-1.0.3-1-linux_x86_64.pkg.tar.xz --strip-components=1

      - name: Compile
        run: |
          PATH=$(pwd)/devkitpro/devkitPPC/bin:$PATH DEVKITPPC=$(pwd)/devkitpro/devkitPPC DEVKITPRO=$(pwd)/devkitpro make

      - name: Package
        run: |
          cp -r wii/apps apps
          mkdir -p apps/wiiflow/
          cp -r out/bins apps/wiiflow/bins
          cp -r out/imgs apps/wiiflow/imgs
          cp out/boot.dol apps/wiiflow/
          mkdir -p wiiflow
          cp -r wii/wiiflow/Languages wiiflow/languages
          cp -r wii/wiiflow/themes_lite wiiflow/themes_lite
          echo "sha=$(git rev-parse --short=7 HEAD)" >> $GITHUB_ENV

      - name: Upload binaries
        uses: actions/upload-artifact@v3
        with: 
          name: wiiflow_lite_${{ env.sha }}
          path: |
            apps
            wiiflow
