on: [push, pull_request]

name: CI

env:
  DESTDIR: "${{ github.workspace }}/dist"
  MESON: "${{ github.workspace }}/repo/meson.sh"

jobs:
  "Meson_Compile_Naev":

    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-20.04
            shell: bash
            config: linux.ini

          - os: windows-latest
            shell: msys2 {0}
            config: windows.ini
            
    defaults:
      run:
        shell: ${{ matrix.shell }}

    runs-on: ${{ matrix.os }}

    steps:
    - name: Update APT Cache
      if: ${{ runner.os == 'Linux'}}
      run: |
        sudo apt-get update

    - name: Install Additional Build Dependencies
      if: ${{ runner.os == 'Linux'}}
      run: |
        sudo apt-get install \
          automake \
          autopoint \
          autoconf-archive \
          build-essential \
          binutils-dev \
          gettext \
          intltool \
          libsdl2-dev \
          libsdl2-mixer-dev \
          libsdl2-image-dev \
          libgl1-mesa-dev \
          libgl1-mesa-dri \
          libxml2-dev \
          libfreetype6-dev \
          libpng-dev \
          libopenal-dev \
          libvorbis-dev \
          libiberty-dev \
          llvm \
          ninja-build \
          xvfb

    - name: Install MinGW Packages
      if: ${{ runner.os == 'Windows'}}
      uses: msys2/setup-msys2@v2
      with:
        msystem: MINGW64
        update: true
        path-type: strict
        install: git tar mingw-w64-x86_64-clang mingw-w64-x86_64-freetype mingw-w64-x86_64-libvorbis mingw-w64-x86_64-libxml2 mingw-w64-x86_64-make mingw-w64-x86_64-mesa mingw-w64-x86_64-meson mingw-w64-x86_64-ninja mingw-w64-x86_64-openal mingw-w64-x86_64-pkg-config mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_mixer

    - name: Checkout Naev Repository
      uses: actions/checkout@v2
      with:
        path: repo
        fetch-depth: 0

    - name:  Package Meson Dist
      run: |
        sh $MESON setup dist repo -Dexecutable=disabled -Ddocs_c=disabled -Ddocs_lua=disabled
        sh $MESON dist -C dist --no-tests --include-subprojects
        
    - name: Upload Meson Dist Artifact
      uses: actions/upload-artifact@v2
      if: ${{ runner.os == 'Linux' }}
      with:
        name: naev-meson-dist-${{ github.sha }}
        path: ${{ github.workspace }}/dist/meson-dist/*
        if-no-files-found: error
        
    - name: Extract Source
      run: |
        mkdir source
        tar -xf dist/meson-dist/naev-*.tar.xz -C source --strip 1
      working-directory: "${{ github.workspace }}"

    - name: Meson Setup
      id: setup
      run: |
        sh $MESON setup build source \
            --native-file='source/utils/build/${{ matrix.config }}' \
            --buildtype=debug \
            -Db_lto=true \
            -Dauto_features=enabled \
            -Ddocs_c=disabled \
            -Ddocs_lua=disabled

    - name: Upload Setup Log
      uses: actions/upload-artifact@v2
      if: ${{ success() || steps.setup.outcome == 'failure' }}
      with:
        name: ${{ matrix.os }}-meson-${{ github.sha }}-setup-log
        path: ${{ github.workspace }}/build/meson-logs/meson-log.txt
        if-no-files-found: ignore

    - name: Meson Compile
      id: compile
      run: |
        sh $MESON compile -C build

    - name: Upload Compile Log
      uses: actions/upload-artifact@v2
      if: ${{ success() || steps.compile.outcome == 'failure' }}
      with:
        name: ${{ matrix.os }}-meson-${{ github.sha }}-compile-log
        path: ${{ github.workspace }}/build/meson-logs/meson-log.txt
        if-no-files-found: ignore

    - name: Meson Test
      id: tests
      run: |
        sh $MESON test -C build --print-errorlogs

    - name: Upload Test Log
      uses: actions/upload-artifact@v2
      if: ${{ success() || steps.tests.outcome == 'failure' }}
      with:
        name: ${{ matrix.os }}-meson-${{ github.sha }}-test-log
        path: ${{ github.workspace }}/build/meson-logs/testlog.txt
        if-no-files-found: ignore

    - name: Test Meson Install
      run: |
        sh $MESON install -C build

    - name: Upload Install Log
      uses: actions/upload-artifact@v2
      if: ${{ success() || steps.compile.outcome == 'failure' }}
      with:
        name: ${{ matrix.os }}-meson-${{ github.sha }}-install-log
        path: ${{ github.workspace }}/build/meson-logs/meson-log.txt
        if-no-files-found: ignore

  "Autotools_Compile_Naev":

    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-20.04
            shell: bash
            make: make

          - os: windows-latest
            shell: msys2 {0}
            make: mingw32-make

    defaults:
      run:
        shell: ${{ matrix.shell }}

    runs-on: ${{ matrix.os }}

    steps:
    - name: Update APT Cache
      if: ${{ runner.os == 'Linux'}}
      run: |
        sudo apt-get update

    - name: Install Additional Build Dependencies
      if: ${{ runner.os == 'Linux'}}
      run: |
        sudo apt-get install \
          build-essential \
          automake \
          autopoint \
          autoconf-archive \
          binutils-dev \
          gettext \
          intltool \
          libsdl2-dev \
          libsdl2-mixer-dev \
          libsdl2-image-dev \
          libgl1-mesa-dev \
          libglu1-mesa-dev \
          libxml2-dev \
          libfreetype6-dev \
          libpng-dev \
          libopenal-dev \
          libvorbis-dev \
          libiberty-dev 

    - name: Install MinGW Packages
      if: ${{ runner.os == 'Windows'}}
      uses: msys2/setup-msys2@v2
      with:
        msystem: MINGW64
        update: true
        path-type: strict
        install: mingw-w64-x86_64-libtool mingw-w64-x86_64-toolchain mingw-w64-x86_64-clang mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_image mingw-w64-x86_64-libxml2 mingw-w64-x86_64-libpng mingw-w64-x86_64-openal mingw-w64-x86_64-libvorbis mingw-w64-x86_64-binutils mingw-w64-x86_64-itstool mingw-w64-x86_64-freetype mingw-w64-x86_64-gettext mingw-w64-x86_64-make mingw-w64-x86_64-pkg-config autoconf autoconf-archive automake-wrapper git intltool tar

    - name: Checkout Naev Repository
      uses: actions/checkout@v2
      with:
        path: repo
        fetch-depth: 0

    - name: Autotools Setup
      run: |
        ./autogen.sh
        ./configure
      working-directory: repo
      env:
        MAKE: "${{ matrix.make }}"

    - name: Package Autotools Dist
      run: |
          $MAKE dist-gzip
      working-directory: "repo"
      env:
        MAKE: "${{ matrix.make }}"

    - name: Upload Autotools Dist Artifact
      uses: actions/upload-artifact@v2
      if: ${{ runner.os == 'Linux' }}
      with:
        name: naev-autotools-dist-${{ github.sha }}
        path: ${{ github.workspace }}/repo/naev-*.tar.gz
        if-no-files-found: error

    - name: Extract Source
      run: |
        mkdir source
        tar -xf repo/naev-*.tar.gz -C source --strip 1
      working-directory: ${{ github.workspace }}

    - name: Autotools Setup
      id: setup
      run: ./configure
      working-directory: source
      env:
        MAKE: "${{ matrix.make }}"

    - name: Upload Configure Log
      uses: actions/upload-artifact@v2
      if: ${{ success() || steps.setup.outcome == 'failure' }}
      with:
        name: ${{ matrix.os }}-autotools-${{ github.sha }}-configure-log
        path: ${{ github.workspace }}/source/config.log
        if-no-files-found: ignore

    - name: Autotools Compile
      run: $MAKE -j"$(nproc)"
      working-directory: "source"
      env:
        MAKE: "${{ matrix.make }}"

    - name: Autotools Install
      run: $MAKE install
      working-directory: "source"
      env:
        MAKE: "${{ matrix.make }}"

  "AppImage_Compile_Naev":
    needs: [Meson_Compile_Naev, Autotools_Compile_Naev]
    strategy:
      fail-fast: false
      matrix:
        include:
          - buildtype: meson
          - buildtype: autotools

    runs-on: ubuntu-16.04

    steps:
    - name: Update APT Cache
      run: |
        sudo apt-get update

    - name: Install Additional Build Dependencies
      run: |
        sudo apt-get install \
          autopoint \
          automake \
          autoconf-archive \
          binutils-dev \
          build-essential \
          gettext \
          intltool \
          libfreetype6-dev \
          libgl1-mesa-dev \
          libiberty-dev \
          libopenal-dev \
          libpng-dev \
          libsdl2-dev \
          libsdl2-image-dev \
          libsdl2-mixer-dev \
          libvorbis-dev \
          libxml2-dev \
          ninja-build=1.7.1-1~ubuntu16.04.1
        sudo update-alternatives --install /usr/bin/llvm-ar llvm-ar /usr/bin/llvm-ar-9 90

    - name: Checkout Naev Repository
      uses: actions/checkout@v2
      with:
        path: repo
        fetch-depth: 0

    - name: Compile AppImage Autotools
      if: ${{ matrix.buildtype == 'autotools' }}
      id: autotoolsCompile
      run: |
        ./repo/utils/buildAppImage.sh -c -s "repo" -b "repo" -o "${{ env.DESTDIR }}" |& tee -a appImageBuildLog.txt

    - name: Compile AppImage Meson
      if: ${{ matrix.buildtype == 'meson' }}
      id: mesonCompile
      run: |
        ./repo/utils/buildAppImage.sh -c -m -s "repo" -b "build" -o "${{ env.DESTDIR }}" |& tee -a appImageBuildLog.txt

    - name: Upload Compile Log
      uses: actions/upload-artifact@v2
      if: ${{ (success() || steps.mesonCompile.outcome == 'failure' || steps.autotoolsCompile.outcome == 'failure') }}
      with:
        name: naev-${{ github.sha }}-${{ matrix.buildtype }}AppImageBuild-log
        path: ${{ github.workspace }}/appImageBuildLog.txt
        if-no-files-found: ignore

    - name: Upload Artifact
      uses: actions/upload-artifact@v2
      with:
        name: naev-${{ matrix.buildtype }}AppImage-${{ github.sha }}
        path: "${{ env.DESTDIR }}/out/*.AppImage"
        if-no-files-found: error

  "Documentation":
    needs: [AppImage_Compile_Naev, Autotools_Compile_Naev, Meson_Compile_Naev]
    runs-on: ubuntu-20.04

    steps:
    - name: Update APT Cache
      run: |
        sudo apt-get update
        
    - name: Install Additional Build Dependencies
      run: |
        sudo apt-get install \
          lua-ldoc \
          graphviz \
          doxygen \
          ninja-build
          
    - name: Checkout Naev Repository
      uses: actions/checkout@v2
      with:
        path: repo
        fetch-depth: 0
        
    - name: Meson Build
      run: |
        sh $MESON setup build repo \
            -Dexecutable=disabled
        
    - name: Trigger API Site Update
      if: ${{ github.event_name == 'push' && github.repository == 'naev/naev' }}
      run: |
        curl -X POST https://api.github.com/repos/naev/naev.github.io/dispatches \
        -H 'Accept: application/vnd.github.everest-preview+json' \
        -u ${{ secrets.WEBSITE_ACCESS_TOKEN }} \
        --data '{"event_type": "api", "client_payload": { "repository": "'"$GITHUB_REPOSITORY"'" }}'
