name: Emu third-party dependencies (Linux)

on:
  workflow_call:
    # needed since it allows this to become a reusable workflow
  workflow_dispatch:
    # allows manual trigger

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:
  deps-build:
    runs-on: ubuntu-22.04
    if: ${{ !cancelled() }}

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

    # we need branch because it has build scripts
    - name: Checkout branch
      if: steps.emu-deps-cache-step.outputs.cache-hit != 'true'
      uses: actions/checkout@v4

    - name: Clone third-party deps (deps/linux)
      if: steps.emu-deps-cache-step.outputs.cache-hit != 'true'
      uses: actions/checkout@v4
      with:
        ref: 'third-party/deps/linux'
        path: "${{env.THIRD_PARTY_BASE_DIR}}/deps/linux"

    - name: Clone third-party deps (deps/common)
      if: steps.emu-deps-cache-step.outputs.cache-hit != 'true'
      uses: actions/checkout@v4
      with:
        ref: 'third-party/deps/common'
        path: "${{env.THIRD_PARTY_BASE_DIR}}/deps/common"

### fix folder permissions! not sure why this fails
    # nested subdirs "build/linux/release" cause permission problems
    - name: Give all permissions to repo folder
      if: steps.emu-deps-cache-step.outputs.cache-hit != 'true'
      shell: bash
      working-directory: ${{ github.workspace }}
      run:  sudo chmod -R 777 "${{ github.workspace }}"

    - name: Build deps
      if: steps.emu-deps-cache-step.outputs.cache-hit != 'true'
      shell: bash
      run: sudo chmod 777 build_linux_deps.sh && sudo ./build_linux_deps.sh -verbose
