# TODO: This document needs to be formatted,
# some stuff needs cleaned up etc
name: eden-build

#on:
#  push:
#    branches: [ "master" ]

# TODO: combine build.yml into trigger_release.yml
jobs:
  source:
    if: ${{ !github.head_ref }}
    runs-on: source
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Pack
        run: ./.ci/source.sh

      - name: Upload
        uses: forgejo/upload-artifact@v4
        with:
          retention-days: 2
          name: source.zip
          path: artifacts/

  windows:
    runs-on: windows
    strategy:
      matrix:
        target: ["msvc"] # TODO: Add msys2
    defaults:
      run:
        shell: ${{ (matrix.target == 'msys2' && 'msys2') || 'bash' }} {0}
    env:
      CCACHE_DIR: ${{ runner.workspace }}/.cache/.ccache
      CCACHE_COMPILERCHECK: content
      CCACHE_SLOPPINESS: time_macros
      OS: windows
      TARGET: ${{ matrix.target }}
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
          fetch-tags: true

      - name: Set up vcpkg cache
        uses: actions/cache@v4
        with:
          path: |
            ${{ github.workspace }}/build/vcpkg_installed
            ${{ github.workspace }}/build/externals
            ${{ github.workspace }}/.vcpkg
          key: ${{ runner.os }}-${{ matrix.target }}-vcpkg-${{ hashFiles('**/CMakeLists.txt', '**/vcpkg.json') }}-${{ github.run_id }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.target }}-vcpkg-

      - name: Set up MSVC
        uses: https://github.com/ilammy/msvc-dev-cmd@v1
        if: ${{ matrix.target == 'msvc' }}

      - name: Cygwin with autoconf # NEEDED FOR LIBUSB
        shell: cmd
        run: ./.ci/windows/cygwin.bat

      - name: Configure & Build
        shell: bash
        run: |
          ./.ci/windows/qt-envvars.sh
          DEVEL=true WINDEPLOYQT="/c/Qt/6.9.0/msvc2022_64/bin/windeployqt6.exe" .ci/windows/build.sh -DCMAKE_PREFIX_PATH=C:/Qt/6.9.0/msvc2022_64/lib/cmake/Qt6

      - name: Package artifacts
        shell: bash
        run: |
          ./.ci/windows/qt-envvars.sh
          ./.ci/windows/package.sh

      - name: Upload Windows artifacts
        uses: forgejo/upload-artifact@v4
        with:
          retention-days: 2
          name: ${{ matrix.target }}.zip
          path: artifacts/*

  linux:
    runs-on: linux
    env:
      CCACHE_DIR: /home/runner/.cache/ccache
      CCACHE_COMPILERCHECK: content
      CCACHE_SLOPPINESS: time_macros
      OS: linux
      TARGET: fresh
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
          fetch-tags: true

      - name: Build
        run: TARGET=appimage DEVEL=true ./.ci/linux/build.sh

      - name: Package AppImage
        run: DEVEL=true ./.ci/linux/package.sh &> /dev/null

      - name: Upload Linux artifacts
        uses: forgejo/upload-artifact@v4
        with:
          retention-days: 3
          name: linux.zip
          path: ./*.AppImage

  android:
    runs-on: android

    env:
      OS: android
      TARGET: universal

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
          fetch-tags: true

      - name: Set tag name
        run: |
          if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
              echo "GIT_TAG_NAME=$GITHUB_REF_NAME" >> $GITHUB_ENV
          fi
          echo $GIT_TAG_NAME

      - name: Build
        run: ANDROID_HOME=/home/runner/sdk ./.ci/android/build.sh
        env:
          ANDROID_KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_B64 }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEYSTORE_PASS: ${{ secrets.ANDROID_KEYSTORE_PASS }}

      - name: Package Android artifacts
        run: ./.ci/android/package.sh

      - name: Upload Android artifacts
        uses: forgejo/upload-artifact@v4
        with:
          retention-days: 2
          name: android.zip
          path: artifacts/*
