name: Continuous integration
#on: [pull_request, push]
on:
  push:
  pull_request:
  workflow_dispatch:
    inputs:
      triggerIos:
        description: 'Run iOS tests'
        required: true
        default: 'yes'
      triggerMacos:
        description: 'Run macOS tests'
        required: true
        default: 'yes'
      trigger3DS:
        description: 'Run 3DS tests'
        required: true
        default: 'yes'
jobs:
  fxcg:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install build-essential cmake libfreetype-dev -y
      - name: Get latest gint commit hash
        run: |
          LATEST_COMMIT_HASH=$(curl --silent https://git.planet-casio.com/api/v1/repos/Lephenixnoir/gint/branches/master | jq -r .commit.id)
          echo "Latest commit hash is: $LATEST_COMMIT_HASH"
          echo "LATEST_COMMIT_HASH=$LATEST_COMMIT_HASH" >> $GITHUB_OUTPUT
        id: get-latest-commit-hash
      - name: Cache gint/fxsdk installation
        id: cache-gint
        uses: actions/cache@v4
        with:
          path: |
            ~/.local/*/*
            !~/.local/share/containers
          key: ${{ runner.os }}-gint-${{ steps.get-latest-commit-hash.outputs.LATEST_COMMIT_HASH }}
      - name: Install gint/fxsdk
        if: steps.cache-gint.outputs.cache-hit != 'true'
        env:
          URL: "https://git.planet-casio.com/Lephenixnoir/GiteaPC/archive/master.tar.gz"
        run: |
          export PATH="~/.local/bin:$PATH"
          cd "$(mktemp -d)"
          curl "$URL" -o giteapc-master.tar.gz
          tar -xzf giteapc-master.tar.gz
          cd giteapc
          python3 giteapc.py install Lephenixnoir/GiteaPC -y
          sudo apt-get install python3-pil libusb-1.0-0-dev libudev-dev libsdl2-dev libpng-dev libudisks2-dev libglib2.0-dev libmpfr-dev libmpc-dev libppl-dev -y
          giteapc install Lephenixnoir/fxsdk:noudisks2 Lephenixnoir/sh-elf-binutils Lephenixnoir/sh-elf-gcc -y
          giteapc install Lephenixnoir/OpenLibm Vhex-Kernel-Core/fxlibc Lephenixnoir/sh-elf-gcc -y
          giteapc install Lephenixnoir/gint -y
      - name: Add fxsdk to PATH
        run: echo "~/.local/bin" >> $GITHUB_PATH
      - run: make -j2 PLATFORM=simulator TARGET=fxcg
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-directory'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'output/release/simulator/fxcg/epsilon.g3a'
          destination: 'upsilon-binfiles.appspot.com/dev/simulator/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon.g3a
          path: output/release/simulator/fxcg/epsilon.g3a
  nintendo_3ds:
    if: github.event.inputs.trigger3DS == 'yes' || github.event.inputs.trigger3DS == ''
    runs-on: ubuntu-latest
    container: devkitpro/devkitarm:latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - run: sudo apt-get update
      - run: sudo apt-get install build-essential imagemagick libfreetype6-dev libjpeg-dev libpng-dev pkg-config -y
      - run: yes | sudo dkp-pacman -S --needed devkitARM 3dstools libctru
      - run: wget https://github.com/3DSGuy/Project_CTR/releases/download/makerom-v0.18.3/makerom-v0.18.3-ubuntu_x86_64.zip
      - run: unzip makerom-v0.18.3-ubuntu_x86_64.zip
      - run: rm makerom-v0.18.3-ubuntu_x86_64.zip
      - run: chmod +x ./makerom
      - run: echo "PATH=.:$PATH" >> $GITHUB_ENV
      - run: make -j2 PLATFORM=simulator TARGET=3ds
      - run: make -j2 PLATFORM=simulator TARGET=3ds epsilon.cia
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-3ds.3dsx
          path: output/release/simulator/3ds/epsilon.3dsx
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-3ds.cia
          path: output/release/simulator/3ds/epsilon.cia
  android:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: |
          sudo apt-get update
          sudo apt-get install imagemagick libfreetype-dev libpng-dev
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'
      - run: wget -nv https://dl.google.com/android/repository/android-ndk-r21e-linux-x86_64.zip
      - run: unzip -q android-ndk-r21e-linux-x86_64.zip
      - run: make -j2 PLATFORM=simulator TARGET=android NDK_PATH=./android-ndk-r21e
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-file'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'output/release/simulator/android/epsilon.apk'
          destination: 'upsilon-binfiles.appspot.com/dev/simulator/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-android.apk
          path: output/release/simulator/android/epsilon.apk
  n0100:
    runs-on: ubuntu-latest
    steps:
      - run: |
          sudo apt-get update
          sudo apt-get install build-essential imagemagick libfreetype6-dev libjpeg-dev libpng-dev pkg-config
      - uses: numworks/setup-arm-toolchain@2020-q2
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: mkdir final-output
      - run: make -j2 MODEL=n0100 EPSILON_I18N=en output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.en.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 EPSILON_I18N=fr output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.fr.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 EPSILON_I18N=nl output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.nl.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 EPSILON_I18N=pt output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.pt.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 EPSILON_I18N=it output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.it.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 EPSILON_I18N=de output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.de.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 EPSILON_I18N=es output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.es.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 EPSILON_I18N=hu output/release/device/n0100/epsilon.onboarding.two_binaries
      - run: mv output/release/device/n0100/epsilon.onboarding.internal.bin final-output/epsilon.onboarding.internal.hu.bin
      - run: rm output/release/device/n0100/apps/i18n.o output/release/device/n0100/apps/i18n.cpp
      - run: make -j2 MODEL=n0100 output/release/device/n0100/flasher.light.bin
      - run: mv output/release/device/n0100/flasher.light.bin final-output/flasher.light.bin
      - run: find final-output/ -type f -exec bash -c "shasum -a 256 -b {} > {}.sha256" \;
      - run: tar cvfz binpack-n0100.tgz final-output/*
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon'}}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-directory'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon'}}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'final-output/'
          destination: 'upsilon-binfiles.appspot.com/dev/n100/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-binpack-n0100.tgz
          path: binpack-n0100.tgz
  n0110:
    runs-on: ubuntu-latest
    steps:
      - run: |
          sudo apt-get update
          sudo apt-get install build-essential imagemagick libfreetype6-dev libjpeg-dev libpng-dev pkg-config
      - uses: numworks/setup-arm-toolchain@2020-q2
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: make -j2 MODEL=n0110 epsilon.dfu
      - run: make -j2 MODEL=n0110 epsilon.onboarding.dfu
      - run: make -j2 MODEL=n0110 epsilon.onboarding.update.dfu
      - run: make -j2 MODEL=n0110 epsilon.onboarding.beta.dfu
      - run: make -j2 MODEL=n0110 flasher.light.dfu
      - run: make -j2 MODEL=n0110 flasher.verbose.dfu
      # We  don't need bench as it is used only in factory
      # - run: make -j2 bench.ram.dfu
      # - run: make -j2 bench.flash.dfu
      - run: make -j2 MODEL=n0110 binpack
      - run: cp output/release/device/n0110/binpack-n0110-`git rev-parse HEAD | head -c 7`.tgz output/release/device/n0110/binpack-n0110.tgz
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-directory'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'output/release/device/n0110/binpack/'
          destination: 'upsilon-binfiles.appspot.com/dev/n110/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-binpack-n0110.tgz
          path: output/release/device/n0110/binpack-n0110.tgz
  bootloader:
    runs-on: ubuntu-latest
    steps:
      - run: |
          sudo apt-get update
          sudo apt-get install build-essential imagemagick libfreetype6-dev libjpeg-dev libpng-dev pkg-config
      - uses: numworks/setup-arm-toolchain@2022-08
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: make -j2 MODEL=n0110 bootloader
      - run: make -j2 epsilon.A.dfu epsilon.B.dfu
      - run: make -j2 epsilon.onboarding.A.dfu
      - run: make -j2 epsilon.onboarding.B.dfu
      - run: make -j2 epsilon.onboarding.update.A.dfu
      - run: make -j2 epsilon.onboarding.update.B.dfu
      - run: make -j2 epsilon.onboarding.beta.A.dfu
      - run: make -j2 epsilon.onboarding.beta.B.dfu
      - run: make -j2 binpack
      - run: cp output/release/device/bootloader/binpack-bootloader-`git rev-parse HEAD | head -c 7`.tgz output/release/device/bootloader/binpack-bootloader.tgz
      - run: cp output/release/device/n0110/bootloader.bin output/release/device/bootloader/binpack/
      - run: cp output/release/device/n0110/bootloader.bin output/release/device/bootloader/
      - run: cd output/release/device/bootloader && for binary in *.bin; do shasum -a 256 -b binpack/${binary} > binpack/${binary}.sha256;done
      - run: cd output/release/device/bootloader && tar cvfz binpack-bootloader.tgz binpack/*
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-directory'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'output/release/device/bootloader/binpack/'
          destination: 'upsilon-binfiles.appspot.com/dev/n110/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-binpack-bootloader.tgz
          path: output/release/device/bootloader/binpack-bootloader.tgz
  windows:
    runs-on: windows-latest
    defaults:
       run:
         shell: msys2 {0}
    steps:
      - uses: msys2/setup-msys2@v2
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: pacman -S --noconfirm mingw-w64-x86_64-gcc mingw-w64-x86_64-freetype mingw-w64-x86_64-pkg-config make mingw-w64-x86_64-python3 mingw-w64-x86_64-libjpeg-turbo mingw-w64-x86_64-libpng
      - run: make -j2 PLATFORM=simulator
      - run: make -j2 PLATFORM=simulator test.exe
      - run: cmd /c output\release\simulator\windows\test.exe --headless
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-file'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'output/release/simulator/windows/epsilon.exe'
          destination: 'upsilon-binfiles.appspot.com/dev/simulator/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-windows.exe
          path: output/release/simulator/windows/epsilon.exe
  web:
    runs-on: ubuntu-latest
    steps:
      - uses: numworks/setup-emscripten@master
        with:
          sdk: latest
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      # Install the Linux toolchin to avoid dependancies issues, but maybe only libpng is needed
      - run: |
          sudo apt-get update
          sudo apt-get install build-essential imagemagick libfreetype6-dev libjpeg-dev libpng-dev pkg-config libxtst-dev
      - run: make -j2 PLATFORM=simulator TARGET=web
      - run: make -j2 PLATFORM=simulator TARGET=web test.js
      - run: node output/release/simulator/web/test.js --headless
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-file'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'output/release/simulator/web/epsilon.js'
          destination: 'upsilon-binfiles.appspot.com/dev/simulator/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-web.zip
          path: output/release/simulator/web/epsilon.zip
  linux:
    runs-on: ubuntu-latest
    steps:
      - run: |
          sudo apt-get update
          sudo apt-get install build-essential imagemagick libfreetype6-dev libjpeg-dev libpng-dev pkg-config libxtst-dev
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: make -j2 PLATFORM=simulator
      - run: make -j2 PLATFORM=simulator test.bin
      - run: output/release/simulator/linux/test.bin --headless
      - id: 'auth'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{secrets.GOOGLE_CREDENTIALS}}'
      - id: 'upload-file'
        if: ${{ github.event_name == 'push' && github.ref_name == 'upsilon-dev' && github.repository == 'UpsilonNumworks/Upsilon' }}
        uses: 'google-github-actions/upload-cloud-storage@v0'
        with:
          path: 'output/release/simulator/linux/epsilon.bin'
          destination: 'upsilon-binfiles.appspot.com/dev/simulator/'
          parent: false
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-linux.bin
          path: output/release/simulator/linux/epsilon.bin
  macos:
    if: github.event.inputs.triggerMacos == 'yes' || github.event.inputs.triggerMacos == ''
    runs-on: macOS-latest
    steps:
      - run: brew install python-setuptools
      - run: brew install numworks/tap/epsilon-sdk
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: make -j2 PLATFORM=simulator
      - run: make -j2 PLATFORM=simulator ARCH=x86_64 test.bin
      - run: output/release/simulator/macos/x86_64/test.bin --headless
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-macos.zip
          path: output/release/simulator/macos/epsilon.app
  ios:
    if: github.event.inputs.triggerIos == 'yes' || github.event.inputs.triggerIos == ''
    runs-on: macOS-latest
    steps:
      - run: brew install python-setuptools
      - run: brew install numworks/tap/epsilon-sdk
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - run: make -j2 PLATFORM=simulator TARGET=ios EPSILON_TELEMETRY=0
      - run: make -j2 PLATFORM=simulator TARGET=ios EPSILON_TELEMETRY=0 APPLE_PLATFORM=ios-simulator
      - uses: actions/upload-artifact@v4
        with:
          name: epsilon-ios.ipa
          path: output/release/simulator/ios/epsilon.ipa

env:
  ACCEPT_OFFICIAL_TOS: 1
