name: 'Linux'

on:
  push:
    branches:
      - 'master'
  pull_request:
    types: [ opened, synchronize ]
  workflow_dispatch:

concurrency: 
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  Build:
    strategy:
      fail-fast: false
      matrix:
        include:
        - BuildType: Release
          Platform: x64
          Backend: GLFW
          CC: gcc
          CXX: g++

        - BuildType: Release
          Platform: x64
          Backend: GLFW
          CC: clang
          CXX: clang++
          
        - BuildType: Release
          Platform: x64
          Backend: SDL2
          CC: gcc
          CXX: g++

        - BuildType: Release
          Platform: x64
          Backend: SDL2
          CC: clang
          CXX: clang++

    #runs-on: 'ubuntu-latest'
    runs-on: 'ubuntu-22.04'

    steps:
    - name: 'Checkout Repository'
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: 'Create Build Environment'
      run: |
        sudo apt update -y
        sudo apt install -y cmake curl g++ libgl1-mesa-dev libopenmpt-dev libcurl4-openssl-dev rpm

    - if: matrix.CC == 'clang'
      name: 'Configure Clang Compiler'
      uses: egor-tensin/setup-clang@v1
      with:
        version: 15
        
    - name: 'Configure CMake'
      run: |
        export CC=${{ matrix.CC }}
        export CXX=${{ matrix.CXX }}
        
        rm -f ./Content/Translations/*.po
        
        cmake -B ./_build/ -D CMAKE_BUILD_TYPE=${{ matrix.BuildType }} -D NCINE_STRIP_BINARIES=ON -D NCINE_PREFERRED_BACKEND=${{ matrix.Backend }} -D NCINE_ASSEMBLE_DEB=ON -D NCINE_ASSEMBLE_RPM=ON -D NCINE_WITH_GLEW=OFF

    - name: 'Build'
      run: |
        make -j $(nproc) -C ./_build/

    - name: 'Create Package'
      run: |
        make package -C ./_build/
        mkdir ./_package/
        cp -f ./_build/jazz2 ./_package/jazz2
        cp -f ./_build/*.deb ./_package/jazz2.deb
        cp -f ./_build/*.rpm ./_package/jazz2.rpm
        cp -f -r ./Content/ ./_package/Content/
        cp -f ./LICENSE ./_package/LICENSE
        
        case "${{ matrix.CC }}" in
          gcc ) artifactPath="Jazz2_Linux_${{ matrix.Platform }}_${{ matrix.Backend }}" ;;
          clang ) artifactPath="Jazz2_Linux_${{ matrix.Platform }}_${{ matrix.Backend }}_Clang" ;;
          * ) artifactPath="Jazz2_Linux_${{ matrix.Platform }}_${{ matrix.Backend }}_${{ matrix.CC }}" ;;
        esac
        echo "artifactPath=$artifactPath" >> $GITHUB_ENV
        
    - name: 'Upload Package'
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.artifactPath }}
        path: ./_package/
        
  Build-AppImage:
    strategy:
      fail-fast: false
      matrix:
        include:
        - BuildType: Release
          Platform: x64
          Backend: GLFW
          CC: gcc
          CXX: g++
        - BuildType: Release
          Platform: x64
          Backend: SDL2
          CC: gcc
          CXX: g++

    #runs-on: 'ubuntu-latest'
    runs-on: 'ubuntu-22.04'

    steps:
    - name: 'Checkout Repository'
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: 'Create Build Environment'
      run: |
        sudo apt-get update -y
        sudo apt-get install -y cmake curl g++ libgl1-mesa-dev libglew2.2 libglfw3 libsdl2-2.0-0 libopenal1 libvorbisfile3 libopenmpt-dev libcurl4-openssl-dev fuse
        
    - name: 'Configure CMake'
      run: |
        export CC=${{ matrix.CC }}
        export CXX=${{ matrix.CXX }}
        
        rm -f ./Content/Translations/*.po
        
        cmake -B ./_build/ -D CMAKE_BUILD_TYPE=${{ matrix.BuildType }} -D NCINE_STRIP_BINARIES=ON -D NCINE_PREFERRED_BACKEND=${{ matrix.Backend }} -D NCINE_WITH_GLEW=OFF -D NCINE_LINUX_PACKAGE=jazz2 -D NCINE_PACKAGED_CONTENT_PATH=ON -D CMAKE_INSTALL_PREFIX=/usr

    - name: 'Build'
      run: |
        DESTDIR=../_appdir/ make install -j $(nproc) -C ./_build/

    - name: 'Create AppImage'
      run: |
        if [ ! -f linuxdeploy-x86_64.AppImage ]; then
          curl -s -S -O --location 'https://github.com/darealshinji/linuxdeploy-plugin-checkrt/releases/download/continuous/linuxdeploy-plugin-checkrt.sh'
          curl -s -S -O --location 'https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage'
          chmod a+x linuxdeploy-x86_64.AppImage linuxdeploy-plugin-checkrt.sh
        fi
        
        mv -f ./_appdir/usr/share/jazz2/Content/ ./_appdir/usr/bin/Content/
        rm -f -r ./_appdir/usr/share/jazz2/
        rm -f -r ./_appdir/usr/share/doc/
        
        LDAI_OUTPUT="./_build/jazz2.AppImage" ARCH=x86_64 ./linuxdeploy-x86_64.AppImage --appdir=./_appdir/ --icon-filename=jazz2 --desktop-file=./_appdir/usr/share/applications/jazz2.desktop --executable=./_appdir/usr/bin/jazz2 --custom-apprun=./Sources/AppRun --output=appimage --plugin checkrt
        
        artifactPath="Jazz2_${{ matrix.Platform }}_${{ matrix.Backend }}.AppImage"
        echo "artifactPath=$artifactPath" >> $GITHUB_ENV
        
    - name: 'Upload Package'
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.artifactPath }}
        path: ./_build/jazz2.AppImage
        
  CodeQL:
    name: 'CodeQL Analyze'
    strategy:
      fail-fast: false
    
    runs-on: 'ubuntu-latest'
    
    permissions:
      security-events: write

    steps:
    - name: 'Checkout Repository'
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: 'Initialize CodeQL'
      uses: github/codeql-action/init@v3
      with:
        languages: cpp

    - name: 'Create Build Environment'
      run: |
        sudo apt-get update -y
        sudo apt-get install -y cmake curl g++ libgl1-mesa-dev libopenmpt-dev libcurl4-openssl-dev
    
    - name: 'Configure CMake and Build'
      run: |
        export CC=gcc
        export CXX=g++
        
        cmake -B ./_build/ -D CMAKE_BUILD_TYPE=Release -D NCINE_WITH_GLEW=OFF
        make -j $(nproc) -C ./_build/
        
    - name: 'Perform CodeQL Analysis'
      uses: github/codeql-action/analyze@v3


  # Testing builds with online multiplayer
  MultiplayerPreview:
    strategy:
      fail-fast: false
      matrix:
        include:
        - BuildType: Release
          Platform: x64
          Backend: SDL2
          CC: gcc
          CXX: g++

    #runs-on: 'ubuntu-latest'
    runs-on: 'ubuntu-22.04'

    steps:
    - name: 'Checkout Repository'
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: 'Create Build Environment'
      run: |
        sudo apt update -y
        sudo apt install -y cmake curl g++ libgl1-mesa-dev libopenmpt-dev libcurl4-openssl-dev rpm
        sudo apt install -y libdw-dev

    - if: matrix.CC == 'clang'
      name: 'Configure Clang Compiler'
      uses: egor-tensin/setup-clang@v1
      with:
        version: 15
        
    - name: 'Configure CMake'
      run: |
        export CC=${{ matrix.CC }}
        export CXX=${{ matrix.CXX }}
        
        rm -f ./Content/Translations/*.po
        
        cmake -B ./_build/ -D CMAKE_BUILD_TYPE=${{ matrix.BuildType }} -D NCINE_PREFERRED_BACKEND=${{ matrix.Backend }} -D NCINE_WITH_GLEW=OFF -D DEATH_DEBUG_SYMBOLS=ON -D WITH_MULTIPLAYER=ON

    - name: 'Build'
      run: |
        make -j $(nproc) -C ./_build/

    - name: 'Create Package'
      run: |
        make package -C ./_build/
        mkdir ./_package/
        cp -f ./_build/jazz2 ./_package/jazz2
        cp -f ./_build/jazz2.pdb ./_package/jazz2.pdb
        cp -f -r ./Content/ ./_package/Content/
        cp -f ./LICENSE ./_package/LICENSE
        cp -f ./Docs/Snippets/ServerConfiguration.json ./_package/Jazz2.Server.config
        
        case "${{ matrix.CC }}" in
          gcc ) artifactPath="Jazz2_Linux_MultiplayerPreview_${{ matrix.Platform }}_${{ matrix.Backend }}" ;;
          clang ) artifactPath="Jazz2_Linux_MultiplayerPreview_${{ matrix.Platform }}_${{ matrix.Backend }}_Clang" ;;
          * ) artifactPath="Jazz2_Linux_MultiplayerPreview_${{ matrix.Platform }}_${{ matrix.Backend }}_${{ matrix.CC }}" ;;
        esac
        echo "artifactPath=$artifactPath" >> $GITHUB_ENV
        
    - name: 'Upload Package'
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.artifactPath }}
        path: ./_package/