name: Linux

on: [push, pull_request]

env:
  # build Configuration, i.e. Debug, Release, etc.
  PRODUCT_BUILD_TYPE: Release

  # build Configuration, i.e. Debug, Release, etc.
  Configuration: Release

  # Required for Github Action. Unit test TestProcess.testKillAndTerminate fails to start /bin/nano with the following error: "Error opening terminal: unknown."
  TERM: xterm

jobs:
  build:
    # For a list of available runner types, refer to
    # https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Configure GIT
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: |
        git config --local core.autocrlf true
        git config --local advice.detachedHead false
        git submodule update --init --recursive

    - name: Setup python
      uses: actions/setup-python@v3
      with:
        python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
        architecture: 'x64' # optional x64 or x86. Defaults to x64 if not specified

    - name: Create new environment variables
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: |
        python -c "import os; print('GIT_REPOSITORY=' + os.path.split(os.getenv('GITHUB_REPOSITORY'))[1]);" >> $GITHUB_ENV
        python -c "import os; print('GIT_BRANCH=' + os.path.split(os.getenv('GITHUB_REF'))[1]);" >> $GITHUB_ENV
        echo GITHUB_WORKFLOW=$GITHUB_WORKFLOW>> $GITHUB_ENV

    - name: List environment variables for debugging
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: |
        env

    - name: Configure user profile and directories
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: |
        mkdir ~/Documents   #required for TestUser.testFoldersExisting() because /home/travis/Documents does not exists!
        mkdir ~/Desktop     #required for TestUser.testFoldersExisting() because /home/travis/Desktop   does not exists!
        
    - name: Deploy 'tests not available' badge before building
      uses: exuanbo/actions-deploy-gist@v1
      with:
        token: ${{ secrets.BADGES }}
        gist_id: 58cf6c72c08e706335337d5ef9ca48e8
        gist_file_name: ${{env.GIT_REPOSITORY}}.${{env.GIT_BRANCH}}.${{env.GITHUB_WORKFLOW}}.json
        file_path: ./ci/github/tests_not_available.badge.json
    
    - name: Install Arduino CLI
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/install_arduinocli.sh

    - name: Install Arduino library dependnecies
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_install_libraries.sh

    - name: Install this arduino library
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/install_this.sh

    - name: Install Google Test
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/install_googletest.sh

    - name: Install RapidAssist
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/install_rapidassist.sh

    - name: Install win32arduino
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/install_win32arduino.sh

    - name: Build library
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/build_library.sh

    - name: Build Arduino sketch - Basic
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh Basic

    - name: Build Arduino sketch - BlockingProgramMemoryRtttl
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh BlockingProgramMemoryRtttl

    - name: Build Arduino sketch - BlockingRtttl
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh BlockingRtttl

    - name: Build Arduino sketch - BlockingWithNonBlocking
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh BlockingWithNonBlocking

    - name: Build Arduino sketch - NonBlockingProgramMemoryRtttl
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh NonBlockingProgramMemoryRtttl

    - name: Build Arduino sketch - NonBlockingRtttl
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh NonBlockingRtttl

    - name: Build Arduino sketch - NonBlockingStopBeforeEnd
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh NonBlockingStopBeforeEnd

    - name: Build Arduino sketch - Play10Bits
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh Play10Bits

    - name: Build Arduino sketch - Play16Bits
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh Play16Bits

    - name: Build Arduino sketch - Rtttl2Code
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/arduino_build_sketch.sh Rtttl2Code

    - name: Run unit tests
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: ./ci/github/test_script.sh

    - name: Search unit test report file
      working-directory: ${{env.GITHUB_WORKSPACE}}
      shell: bash
      run: |
        UNITTEST_REPORT_PATH=$(find . -name 'anyrtttl_unittest*.xml')
        echo UNITTEST_REPORT_PATH=$UNITTEST_REPORT_PATH
        echo UNITTEST_REPORT_PATH=$UNITTEST_REPORT_PATH >> $GITHUB_ENV
      
    - name: Create test badge
      working-directory: ${{env.GITHUB_WORKSPACE}}
      run: python ci/github/maketestbadge.py ${{env.UNITTEST_REPORT_PATH}}

    - name: Deploy test badge to gist
      uses: exuanbo/actions-deploy-gist@v1
      with:
        token: ${{ secrets.BADGES }}
        gist_id: 58cf6c72c08e706335337d5ef9ca48e8
        gist_file_name: ${{env.GIT_REPOSITORY}}.${{env.GIT_BRANCH}}.${{env.GITHUB_WORKFLOW}}.json
        file_path: ./badge.json
    
    - name: Archive test results
      uses: actions/upload-artifact@v3
      with:
        name: unit-test-results
        path: build/bin/anyrtttl_unittest*.xml
