name: Build CCExtractor on Linux

on:
  workflow_dispatch:
  push:
    paths:
    - '.github/workflows/build_linux.yml'
    - '**.c'
    - '**.h'
    - '**CMakeLists.txt'
    - '**.cmake'
    - '**Makefile**'
    - 'linux/**'
    - 'package_creators/**'
    - 'src/rust/**'
  pull_request:
    types: [opened, synchronize, reopened]
    paths:
    - '.github/workflows/build_linux.yml'
    - '**.c'
    - '**.h'
    - '**CMakeLists.txt'
    - '**.cmake'
    - '**Makefile**'
    - 'linux/**'
    - 'package_creators/**'
    - 'src/rust/**'
jobs:
  build_shell:
    runs-on: ubuntu-latest
    steps:
    - name: Install dependencies
      run: sudo apt update && sudo apt-get install libgpac-dev libtesseract-dev libavcodec-dev libavdevice-dev libx11-dev libxcb1-dev libxcb-shm0-dev
    - uses: actions/checkout@v6
    - name: build
      run: ./build -hardsubx -min-rust
      working-directory: ./linux
    - name: Display version information
      run: ./ccextractor --version
      working-directory: ./linux
    - name: Prepare artifacts
      run: mkdir ./linux/artifacts
    - name: Copy release artifact
      run: cp ./linux/ccextractor ./linux/artifacts/
    # NOTE: The sample-platform test runner (CCExtractor/sample-platform)
    # matches artifact names exactly. Update Artifact_names in
    # mod_ci/controllers.py there if you rename this artifact.
    - uses: actions/upload-artifact@v7
      with:
        name: CCExtractor Linux build
        path: ./linux/artifacts
  build_shell_migrations:
    runs-on: ubuntu-latest
    steps:
    - name: Install dependencies
      run: sudo apt update && sudo apt-get install libgpac-dev libtesseract-dev libavcodec-dev libavdevice-dev libx11-dev libxcb1-dev libxcb-shm0-dev
    - uses: actions/checkout@v6
    - name: build
      run: ./build -hardsubx
      working-directory: ./linux
    - name: Display version information
      run: ./ccextractor --version
      working-directory: ./linux
    - name: Prepare artifacts
      run: mkdir ./linux/artifacts
    - name: Copy release artifact
      run: cp ./linux/ccextractor ./linux/artifacts/
    # NOTE: The sample-platform test runner (CCExtractor/sample-platform)
    # matches artifact names exactly. Update Artifact_names in
    # mod_ci/controllers.py there if you rename this artifact.
    - uses: actions/upload-artifact@v7
      with:
        name: CCExtractor Linux build (with migrations)
        path: ./linux/artifacts
  build_autoconf:
    runs-on: ubuntu-latest
    steps:
    - name: Install dependencies
      run: sudo apt update && sudo apt-get install libgpac-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev
    - uses: actions/checkout@v6
    - name: run autogen
      run: ./autogen.sh
      working-directory: ./linux
    - name: configure
      run: ./configure --enable-debug
      working-directory: ./linux
    - name: make
      run: make
      working-directory: ./linux
    - name: Display version information
      run: ./ccextractor --version
      working-directory: ./linux
  cmake:
    runs-on: ubuntu-latest
    steps:
    - name: Install dependencies
      run: sudo apt update && sudo apt-get install libgpac-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev
    - uses: actions/checkout@v6
    - name: cmake
      run: mkdir build && cd build && cmake ../src
    - name: build
      run: make -j$(nproc)
      working-directory: build
    - name: Display version information
      run: ./build/ccextractor --version
  cmake_ffmpeg_mp4:
    runs-on: ubuntu-latest
    steps:
    - name: Install dependencies
      run: sudo apt update && sudo apt-get install libgpac-dev libtesseract-dev libleptonica-dev libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libavdevice-dev libswresample-dev libswscale-dev
    - uses: actions/checkout@v6
    - name: cmake (default GPAC build)
      run: mkdir build && cd build && cmake ../src
    - name: build (default GPAC build)
      run: make -j$(nproc)
      working-directory: build
    - name: Display version information (GPAC build)
      run: ./build/ccextractor --version
    - name: cmake (FFmpeg MP4 build)
      run: mkdir build_ffmpeg && cd build_ffmpeg && cmake -DWITH_FFMPEG=ON -DWITH_OCR=ON -DWITH_HARDSUBX=ON ../src
    - name: build (FFmpeg MP4 build)
      run: make -j$(nproc)
      working-directory: build_ffmpeg
    - name: Display version information (FFmpeg MP4 build)
      run: ./build_ffmpeg/ccextractor --version
  cmake_ocr_hardsubx:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6
    - name: Install dependencies
      run: sudo apt update && sudo apt install libgpac-dev libtesseract-dev libavformat-dev libavdevice-dev libswscale-dev yasm
    - name: cmake
      run: |
        mkdir build && cd build
        cmake -DWITH_OCR=ON -DWITH_HARDSUBX=ON ../src
    - name: build
      run: |
        make -j$(nproc)
      working-directory: build
    - name: Display version information
      run: ./build/ccextractor --version
  build_rust:
    runs-on: ubuntu-latest
    steps:
      - name: Install dependencies
        run: sudo apt update && sudo apt-get install libgpac-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev
      - uses: actions/checkout@v6
      - name: cache 
        uses: actions/cache@v5
        with:
          path: |
            src/rust/.cargo/registry
            src/rust/.cargo/git
            src/rust/target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      - name: build
        run: cargo build 
        working-directory: ./src/rust
