name: Build emu variant (Linux)

on:
  workflow_call:
    inputs:
      emu-variant:
        description: |
          Which variant of the emu to build:
            regular: build the regular version of the emu
            exp: build the experimental version of the emu
            tools: build the tools only
        required: true
        type: string

      x32:
        description: |
          build architecture
            true: x32
            false: x64
        required: true
        type: boolean
    
      debug:
        description: |
          build mode
            true: build in debug mode
            false: build in release mode
        required: true
        type: boolean

permissions:
  contents: write

env:
  DEPS_CACHE_KEY: emu-deps-linux
  DEPS_CACHE_DIR: build/deps/linux

  PACKAGE_BASE_DIR: "build/package/linux"
  THIRD_PARTY_BASE_DIR: 'third-party'
  
jobs:
  build:
    runs-on: ubuntu-22.04
    if: ${{ !cancelled() }}

    steps:
### setup build vars
    - name: Setup build vars
      shell: bash
      run: |
        echo "env file = '$GITHUB_ENV'"
        echo "workspace = '${{ github.workspace }}'"
        
        arch=""
        if [[ "${{ inputs.x32 }}" = "true" ]]; then
          arch="32"
        else
          arch="64"
        fi
        echo "build_arch=$arch" >>"$GITHUB_ENV"

        build_switches=""
        if [[ "${{ inputs.emu-variant }}" = "regular" ]]; then
          build_switches="+lib-$arch +client-$arch"
        elif [[ "${{ inputs.emu-variant }}" = "exp" ]]; then
          build_switches="+exp-lib-$arch +exp-client-$arch"
        elif [[ "${{ inputs.emu-variant }}" = "tools" ]]; then
          build_switches="+tool-clientldr +tool-itf-$arch +tool-lobby-$arch"
        else
          echo "[X] invalid emu variant '${{ inputs.emu-variant }}'" >&2
          exit 1
        fi
        echo "build_switches=$build_switches" >>"$GITHUB_ENV"

        build_mode=""
        if [[ "${{ inputs.debug }}" = "true" ]]; then
          build_mode="debug"
        else
          build_mode="release"
        fi
        echo "build_mode=$build_mode" >>"$GITHUB_ENV"

    - name: Checkout branch
      uses: actions/checkout@v4

### deps
    - name: Restore deps
      id: emu-deps-cache-step
      uses: actions/cache@v4
      with:
        key: ${{ env.DEPS_CACHE_KEY }}
        path: ${{ env.DEPS_CACHE_DIR }}

## mandatory Linux packages, installed via sudo apt install ...
    - name: Install required packages
      shell: bash
      run: sudo chmod 777 build_linux_deps.sh && sudo ./build_linux_deps.sh -verbose -packages_only

## extra helpers/tools, these are not built inside the deps build dir
    - name: Clone third-party build helpers (build/linux)
      uses: actions/checkout@v4
      with:
        ref: 'third-party/build/linux'
        path: "${{env.THIRD_PARTY_BASE_DIR}}/build/linux"

### fix folder permissions! not sure why this fails
    # nested subdirs "build/linux/release" cause permission problems
    - name: Give all permissions to repo folder
      shell: bash
      working-directory: ${{ github.workspace }}
      run:  sudo chmod -R 777 "${{ github.workspace }}"

### build target(s)
    - name: Build target(s)
      shell: bash
      working-directory: ${{ github.workspace }}
      run: "sudo chmod 777 build_linux.sh && ./build_linux.sh -j 3 -verbose ${{ env.build_mode }} clean +build_str ${{ github.sha }} ${{ env.build_switches }}"

### upload artifact/package to github Actions (for targets)
    - name: Upload build package (for targets)
      uses: actions/upload-artifact@v4
      with:
        name: "emu-linux-${{ inputs.emu-variant }}-${{ env.build_arch }}-${{ env.build_mode }}-${{ github.sha }}"
        path: "build/linux/"
        if-no-files-found: 'error'
        compression-level: 9
        retention-days: 1
