name: Build

on: [push, pull_request]

env:
  CMAKE_BUILD_TYPE: ${{ github.event_name == 'pull_request' && 'Debug' || 'Release' }}

jobs:
  windows:
    name: Windows
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Configure
        run: cmake -B build
        shell: cmd
      - name: Build
        run: cmake --build build --config %CMAKE_BUILD_TYPE%
        shell: cmd
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: lovr.exe
          path: |
            build/Release/lovr.exe
            build/Release/lovrc.bat
            build/Release/*.dll

  linux:
    name: Linux
    runs-on: ubuntu-20.04
    steps:
      - name: Update Packages
        run: sudo apt update
      - name: Install Packages
        run: sudo apt install -y xorg-dev libxcb-glx0-dev libx11-xcb-dev libfuse2 libcurl4-openssl-dev libssl-dev
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Init
        run: cmake -B build -D LOVR_BUILD_BUNDLE=ON
      - name: Build
        run: cmake --build build
      - name: AppImage
        run: >
          curl -sOL https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage &&
          chmod +x ./appimagetool-x86_64.AppImage &&
          ./appimagetool-x86_64.AppImage build/bin &&
          mv LÖVR-x86_64.AppImage lovr-x86_64.AppImage
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: lovr.appimage
          path: lovr-x86_64.AppImage

  android:
    name: Android
    runs-on: ubuntu-22.04
    steps:
      - name: Update Packages
        run: sudo apt update
      - name: Install Packages
        run: sudo apt install -y glslang-tools
      - name: Install Android 29
        run: ${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager --install "platforms;android-29"
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Init
        run: >
          mkdir build &&
          cd build &&
          keytool
          -genkey
          -dname 'cn=Unknown, ou=Unknown, o=Unknown, l=Unknown, st=Unknown, c=Unknown'
          -keystore key.keystore
          -keypass hunter2
          -storepass hunter2
          -alias key
          -keyalg RSA
          -keysize 2048
          -validity 10000 &&
          cmake ..
          -D CMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake
          -D ANDROID_SDK=$ANDROID_HOME
          -D ANDROID_ABI=arm64-v8a
          -D ANDROID_NATIVE_API_LEVEL=29
          -D ANDROID_BUILD_TOOLS_VERSION=34.0.0
          -D ANDROID_KEYSTORE=key.keystore
          -D ANDROID_KEYSTORE_PASS=pass:hunter2
      - name: Build
        run: cmake --build build
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: lovr.apk
          path: build/lovr.apk

  macos:
    name: macOS
    runs-on: macos-latest
    steps:
      - name: Install Wulkan
        env:
          VKV: 1.3.275.0
        run: |
          curl -sOL https://sdk.lunarg.com/sdk/download/$VKV/mac/vulkansdk-macos-$VKV.dmg?Human=true
          hdiutil attach vulkansdk-macos-$VKV.dmg
          cp -R /Volumes/vulkansdk-macos-$VKV .
          hdiutil detach /Volumes/vulkansdk-macos-$VKV
          export VULKAN_SDK=${{ runner.temp }}/vulkan && echo "VULKAN_SDK=$VULKAN_SDK/macOS" >> $GITHUB_ENV
          sudo vulkansdk-macos-$VKV/InstallVulkan.app/Contents/MacOS/InstallVulkan --root $VULKAN_SDK --accept-licenses --default-answer --confirm-command install
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Init
        run: cmake -B build -D LOVR_BUILD_BUNDLE=ON
      - name: Build
        run: cmake --build build
      - name: Package
        run: >
          cp $VULKAN_SDK/lib/libvulkan.1.3.275.dylib build/lovr.app/Contents/MacOS/libvulkan.1.dylib &&
          cp $VULKAN_SDK/lib/libMoltenVK.dylib build/lovr.app/Contents/MacOS &&
          mkdir -p build/lovr.app/Contents/Resources/vulkan/icd.d &&
          cp $VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json build/lovr.app/Contents/Resources/vulkan/icd.d &&
          sed -i '' 's|../../../lib/libMoltenVK.dylib|../../../MacOS/libMoltenVK.dylib|' build/lovr.app/Contents/Resources/vulkan/icd.d/MoltenVK_icd.json &&
          ditto -c -k --keepParent build/lovr.app lovr.zip
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: lovr.app
          path: lovr.zip
