#  Copyright (C) 2020-2021 Jacob Burroughs <maths22@gmail.com>
#                2020-2021 A. Semphris <semphris@protonmail.com>
#
#  Released under the Creative Commons Zero (CC0) license, available at:
#    Legal code: https://creativecommons.org/publicdomain/zero/1.0/legalcode
#    Information: https://creativecommons.org/share-your-work/public-domain/cc0/



# Note: Parts of this code were taken from the SuperTux project.
#         ~ Semphris (responsible for transfering and adapting the file)

name: linux
on:
  push:
    branches:
      - master
    tags:
      - '*'
  pull_request: {}
  workflow_dispatch:

jobs:
  build:

    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest]
        compiler: [gcc, clang]
        build_type: [Debug, Release]
        server_only: [ON, OFF]
        exclude:
          - os: macos-latest
            compiler: gcc
          - os: macos-latest
            build_type: Debug

    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1
          submodules: true
      - name: Configure packaging name for git master branch
        if: ${{ matrix.build_type == 'Release' && matrix.server_only == 'OFF' &&
            github.ref == 'refs/heads/master' }}
        run: |
          echo "release_tag=git`date +%Y%m%d`" >> $GITHUB_ENV
          echo "release_name=preview" >> $GITHUB_ENV
      - name: Configure packaging name for tag
        if: ${{ matrix.build_type == 'Release' && matrix.server_only == 'OFF' &&
            startsWith(github.ref, 'refs/tags/') }}
        run: |
          echo "release_tag=`basename $GITHUB_REF`" >> $GITHUB_ENV
          echo "release_name=`basename $GITHUB_REF`" >> $GITHUB_ENV
      - name: Configure packaging name for non-releasing branch
        if: ${{ matrix.build_type != 'Release' || matrix.server_only != 'OFF' ||
            !(github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')) ||
            github.repository_owner != 'supertuxkart' }}
        run: |
          echo "release_tag=" >> $GITHUB_ENV
          echo "release_name=" >> $GITHUB_ENV
      - name: Check for prerelease
        if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'rc') || contains(github.ref, 'beta') }}
        run: |
          echo "release_pre=true" >> $GITHUB_ENV
      - name: Check for non-prerelease
        if: ${{ github.ref != 'refs/heads/master' && !contains(github.ref, 'rc') && !contains(github.ref, 'beta') }}
        run: |
          echo "release_pre=false" >> $GITHUB_ENV
      - name: Show packaging name
        run : |
          echo "${{ env.release_tag }}"
          echo "${{ env.release_name }}"
          echo "${{ env.release_pre }}"
      - name: Install linux dependencies
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run: |
          sudo apt-get update 
          sudo apt install -y build-essential cmake libbluetooth-dev libcurl4-gnutls-dev \
          libfreetype6-dev libharfbuzz-dev libjpeg-dev libogg-dev libopenal-dev \
          libpng-dev libsdl2-dev libvorbis-dev pkg-config zlib1g-dev clang
      - name: Install dylibbundler for packaging osx binary
        if: ${{ env.release_tag != '' && matrix.os == 'macos-latest' && matrix.build_type == 'Release' && matrix.server_only == 'OFF' }}
        run: |
          HOMEBREW_NO_AUTO_UPDATE=1 brew install dylibbundler
      - name: Install macos dependencies
        if: ${{ matrix.os == 'macos-latest' }}
        run: |
          # Something funky happens with freetype if mono is left
          sudo mv /Library/Frameworks/Mono.framework /Library/Frameworks/Mono.framework-disabled
          wget https://github.com/supertuxkart/dependencies/releases/download/preview/dependencies-mac.tar.xz
          # Remove any existing installation to avoid conflict with bundled dependencies
          rm -rf /usr/local/include/*
          rm -rf /usr/local/opt/openssl@1.1/include
          rm -rf /usr/local/opt/freetype
          rm -rf /usr/local/opt/harfbuzz
          tar xf dependencies-mac.tar.xz -C /usr/local
          rm dependencies-mac.tar.xz
      - name: Set compiler (gcc)
        if: ${{ matrix.os == 'ubuntu-latest' && matrix.compiler == 'gcc' }}
        run: |
          echo "CXX=g++" >> $GITHUB_ENV
          echo "CC=gcc" >> $GITHUB_ENV
      - name: Set compiler (clang)
        if: ${{ matrix.os == 'ubuntu-latest' && matrix.compiler == 'clang' }}
        run: |
          echo "CXX=clang++" >> $GITHUB_ENV
          echo "CC=clang" >> $GITHUB_ENV
      - name: Set compiler (macos)
        if: ${{ matrix.os == 'macos-latest' }}
        run: |
          # This ensures for now we use clang11
          # Clang12 runs into a bunch of fun with `include location '/usr/local/include' is unsafe for cross-compilation`
          # that we don't care about for now
          echo "CXX=clang++" >> $GITHUB_ENV
          echo "CC=clang" >> $GITHUB_ENV
      - name: Configure bulid (linux)
        if: ${{ matrix.os != 'macos-latest' }}
        env:
          BUILD_TYPE: ${{ matrix.build_type }}
          SERVER_ONLY: ${{ matrix.server_only }}
        run: |
          cmake --version
          $CXX --version
          mkdir "build"
          cd "build"
          cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSERVER_ONLY=$SERVER_ONLY -DCHECK_ASSETS=off -DBUILD_RECORDER=off -DNO_SHADERC=on;
      - name: Configure bulid (macos)
        if: ${{ matrix.os == 'macos-latest' }}
        env:
          BUILD_TYPE: ${{ matrix.build_type }}
          SERVER_ONLY: ${{ matrix.server_only }}
        run: |
          cmake --version
          $CXX --version
          mkdir "build"
          cd "build"
          CFLAGS="-mmacosx-version-min=10.9" CXXFLAGS="-mmacosx-version-min=10.9" LINKFLAGS="-mmacosx-version-min=10.9" LDFLAGS="-mmacosx-version-min=10.9" /usr/local/opt/cmake/bin/cmake .. -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9 -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSERVER_ONLY=$SERVER_ONLY -DCHECK_ASSETS=off -DBUILD_RECORDER=off;
      - name: Build and install
        working-directory: build
        run: |
          make -j3 VERBOSE=1
          make install DESTDIR="/tmp/stk" VERBOSE=1
      - name: Packaging (macos)
        if: ${{ env.release_tag != '' && matrix.os == 'macos-latest' && matrix.build_type == 'Release' && matrix.server_only == 'OFF' }}
        working-directory: build
        run: |
          /usr/local/opt/dylibbundler/bin/dylibbundler -od -b -x ./bin/SuperTuxKart.app/Contents/MacOS/supertuxkart -d ./bin/SuperTuxKart.app/Contents/libs/ -p @executable_path/../libs/
          wget https://github.com/supertuxkart/stk-assets-mobile/releases/download/git/stk-assets-full.zip
          unzip stk-assets-full.zip -d ../data
          rm stk-assets-full.zip
          cd bin
          # Fix the name on case insensitive filesystem
          mv supertuxkart.app stk.app
          mv stk.app SuperTuxKart.app
          zip -r ../SuperTuxKart-${{ env.release_tag }}-mac.zip .
      - name: Create Release (macos)
        if: ${{ env.release_tag != '' && matrix.os == 'macos-latest' && matrix.build_type == 'Release' && matrix.server_only == 'OFF' }}
        uses: ncipollo/release-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: "build/SuperTuxKart-${{ env.release_tag }}-mac.zip"
          tag: ${{ env.release_name }}
          omitBodyDuringUpdate: true
          omitNameDuringUpdate: true
          allowUpdates: true
          prerelease: ${{ env.release_pre }}
