#!/bin/bash
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

install_build_dependencies()
{
    if [ "${SKIP_APT_INSTALL:-0}" = "1" ]; then
        return
    fi

    if ! command -v apt-get >/dev/null 2>&1; then
        echo "apt-get not found, skip dependency installation."
        return
    fi

    local packages=(
        build-essential
        file
        gcovr
        lcov
        debhelper-compat
        pkgconf
        qt6-base-dev
        qtchooser
        libuchardet-dev
        libavcodec-dev
        libavformat-dev
        libavutil-dev
        libswscale-dev
        libtesseract-dev
        libleptonica-dev
        libminizip-dev
        cmake
        qt6-tools-dev
        libtag1-dev
    )

    local arch
    arch="$(dpkg --print-architecture 2>/dev/null || uname -m)"
    case "${arch}" in
        i386|i486|i586|i686)
            ;;
        *)
            packages+=(libpoppler-qt6-dev)
            ;;
    esac

    if [ "$(id -u)" -eq 0 ]; then
        apt-get update
        apt-get install -y "${packages[@]}"
    else
        sudo apt-get update
        sudo apt-get install -y "${packages[@]}"
    fi
}

install_build_dependencies
