name: GBDK Build and Package

on:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
  # Triggers the workflow on push or pull request events but only for the develop branch
  # push:
  #   branches: [ develop ]
  # pull_request:
  #   branches: [ develop ]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-20.04
            name: Linux-x64
          - os: macos-13
            name: MacOS-x64
          - os: macos-14
            name: MacOS-arm64
          - os: windows-2019
            name: Windows-x64
          - os: windows-2019
            name: Windows-x32
    steps:

      # ==== OS Specific Dependencies ====
      - name: Linux Depends
        if: matrix.name == 'Linux-x64'
        run: |
          curl -Lo sdcc.tar.gz https://github.com/gbdk-2020/gbdk-2020-sdcc/releases/download/sdcc-patched-gbdk-4.3.0/sdcc-14650-Linux-x64.tar.gz
          tar xvfz sdcc.tar.gz

      - name: MacOS Depends
        if: matrix.name == 'MacOS-x64'
        run: |
          curl -Lo sdcc.tar.gz https://github.com/gbdk-2020/gbdk-2020-sdcc/releases/download/sdcc-patched-gbdk-4.3.0/sdcc-14650-MacOS-x64.tar.gz
          tar xvfz sdcc.tar.gz

      - name: MacOS ARM Depends
        if: matrix.name == 'MacOS-arm64'
        run: |
          curl -Lo sdcc.tar.gz https://github.com/gbdk-2020/gbdk-2020-sdcc/releases/download/sdcc-patched-gbdk-4.3.0/sdcc-14650-MacOS-arm64.tar.gz
          tar xvfz sdcc.tar.gz

      - name: Windows-x64 Depends
        if: matrix.name == 'Windows-x64'
        run: |
          curl -Lo sdcc.tar.gz https://github.com/gbdk-2020/gbdk-2020-sdcc/releases/download/sdcc-patched-gbdk-4.3.0/sdcc-14650-Win64-On-Linux.tar.gz
          7z x sdcc.tar.gz
          7z x sdcc.tar

      - name: Windows-x32 Depends
        if: matrix.name == 'Windows-x32'
        run: |
          curl -Lo sdcc.tar.gz https://github.com/gbdk-2020/gbdk-2020-sdcc/releases/download/sdcc-patched-gbdk-4.3.0/sdcc-14650-Win32-On-Linux.tar.gz
          7z x sdcc.tar.gz
          7z x sdcc.tar

      - name: Windows Depends MSYS2/MinGW64
        if: matrix.name == 'Windows-x64'
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          path-type: minimal #strict
          release: false
          update: false
          install: >-
            base-devel
            mingw-w64-x86_64-toolchain

      - name: Windows Depends MSYS2/MinGW32
        if: matrix.name == 'Windows-x32'
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW32
          path-type: minimal #strict
          release: false
          update: false
          install: >-
            base-devel
            mingw-w64-i686-toolchain

      # ==== Build Steps ====
      - name: Check out GBDK-2020
        uses: actions/checkout@v4
        with:
          path: gbdk-2020
          submodules: false

      # ==== Pre-Build: Set environment vars ====
      # Needs to be in a separate step than build so that setting the environment var takes effect
      #
      - name: Pre-Build Linux/MacOS
        if: (matrix.name == 'Linux-x64')
        shell: bash
        run: |
          echo "BUILD_PACKAGE_FILENAME=gbdk-linux64.tar.gz" >> $GITHUB_ENV

      - name: Pre-Build Linux/MacOS
        if: (matrix.name == 'MacOS-x64')
        shell: bash
        run: |
          echo "BUILD_PACKAGE_FILENAME=gbdk-macos.tar.gz" >> $GITHUB_ENV

      - name: Pre-Build Linux/MacOS
        if: (matrix.name == 'MacOS-arm64')
        shell: bash
        run: |
          echo "BUILD_PACKAGE_FILENAME=gbdk-macos-arm64.tar.gz" >> $GITHUB_ENV

      - name: Pre-Build Windows
        if: (matrix.name == 'Windows-x64')
        shell: bash
        run: |
          echo "BUILD_PACKAGE_FILENAME=gbdk-win64.zip" >> $GITHUB_ENV

      - name: Pre-Build Windows
        if: (matrix.name == 'Windows-x32')
        shell: bash
        run: |
          echo "BUILD_PACKAGE_FILENAME=gbdk-win32.zip" >> $GITHUB_ENV

      # ==== Builds ====
      - name: Build GBDK-2020 Linux/MacOS
        if: (matrix.name == 'Linux-x64') || (matrix.name == 'MacOS-x64') || (matrix.name == 'MacOS-arm64')
        shell: bash
        run: |
          export SDCCDIR=`pwd`/sdcc
          cd gbdk-2020
          make
          cd ..

      - name: Build GBDK-2020 Windows
        if: (matrix.name == 'Windows-x64') || (matrix.name == 'Windows-x32')
        shell: cmd
        run: |
          set SDCCDIR=%CD%\sdcc
          cd gbdk-2020
          msys2 -c 'make'
          cd ..

      # ==== Packaging ====
      - name: Package build Linux/MacOS
        if: (matrix.name == 'Linux-x64') || (matrix.name == 'MacOS-x64') || (matrix.name == 'MacOS-arm64')
        shell: bash
        run: |
          cd gbdk-2020
          mkdir package
          cd build
          tar czf ../package/${{ env.BUILD_PACKAGE_FILENAME }} gbdk
          cd ..
          cd ..

      - name: Package build Windows
        if: (matrix.name == 'Windows-x64') || (matrix.name == 'Windows-x32')
        shell: cmd
        run: |
          cd gbdk-2020
          mkdir package
          cd build
          7z a ../package/${{ env.BUILD_PACKAGE_FILENAME }} gbdk
          cd ..
          cd ..

      - name: Store build
        if: (matrix.name == 'Linux-x64') || (matrix.name == 'MacOS-x64') || (matrix.name == 'MacOS-arm64') || ('Windows-x64') || ('Windows-x32')
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.name }} build
          path: gbdk-2020/package/${{ env.BUILD_PACKAGE_FILENAME }}
          # retention-days: 14
