name: Linux (Ubuntu)

on:
  push:
    branches: master
    tags: 'v*.*.*'
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'LICENSE'
      - '.gitignore'
  pull_request:
    branches: master
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'LICENSE'
      - '.gitignore'

defaults:
  run:
    shell: bash

env:
  BUILD_TARGET: linux
  DONT_PACKAGE: true
  MAKE: make
  BT_INSTALLBASE: ${{ github.workspace }}/target/
  QMAKE_EXTRA_ARGUMENTS: CONFIG+=use_pulse CONFIG+=use_jack

jobs:
  build:
    runs-on: ubuntu-20.04

    steps:
      - name: Identify build type.
        id: identify-build
        run: |
          case ${GITHUB_REF} in
            refs/tags/* )
              TAG=${GITHUB_REF#refs/tags/}
              echo "Release ${TAG}"
              echo "build-tag=${TAG}" >> $GITHUB_OUTPUT
              echo "release=true" >> $GITHUB_OUTPUT
              ;;
            refs/pull/*)
              PR=$(echo ${GITHUB_REF} | cut -d/ -f3)
              echo "Test PR #${PR}"
              echo "build-tag=pr-${PR}" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              ;;
            refs/heads/* )
              BRANCH=${GITHUB_REF#refs/heads/}
              echo "Test ${BRANCH}"
              echo "build-tag=${BRANCH}" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              ;;
            * )
              echo "Test (unknown)"
              echo "build-tag=unknown" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              ;;
          esac

      - name: Checking out repository.
        uses: actions/checkout@v4
        with:
          submodules: recursive

      ## Ubuntu-specific steps

      - name: Installing dependencies.
        run: |
          sudo apt update
          sudo apt install qt5-default qttools5-dev-tools \
            libasound2-dev libpulse-dev libjack-jackd2-dev

      ## End Ubuntu-specific steps

      - name: Configuring.
        run: |
          lupdate Project.pro
          qmake Project.pro PREFIX=${BT_INSTALLBASE} \
            CONFIG+=release CONFIG-=debug ${QMAKE_EXTRA_ARGUMENTS}
          ${MAKE} qmake_all

      - name: Building.
        run: |
          ${MAKE} -j2

      - name: Installing.
        run: |
          ${MAKE} -j2 install

      - name: Test packaging.
        if: env.DONT_PACKAGE != 'true'
        run: |
          pushd ${BT_INSTALLBASE}
          bash ${GITHUB_WORKSPACE}/scripts/package_${SCRIPT_NAME:-${BUILD_TARGET%%-*}}.sh
          popd

      - name: Finalize packaging.
        id: packaging
        if: steps.identify-build.outputs.release == 'true' && env.DONT_PACKAGE != 'true'
        run: |
          export package_name="BambooTracker-${{ steps.identify-build.outputs.build-tag }}-${BUILD_TARGET}"
          echo "package-name=${package_name}" >> $GITHUB_OUTPUT
          mv -v ${BT_INSTALLBASE} ${package_name}
          7z a -tzip ${package_name}{.zip,}

      - name: Upload release package.
        if: steps.identify-build.outputs.release == 'true' && env.DONT_PACKAGE != 'true'
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ${{ steps.packaging.outputs.package-name }}.zip
          asset_name: ${{ steps.packaging.outputs.package-name }}.zip
          tag: ${{ github.ref }}
