#!/bin/bash -ex

FORCE_ARG=""
if [[ "$1" == "--force" ]]; then
    FORCE_ARG="--force"
fi

if [[ ! -S /var/run/middleware/middlewared.sock ]]; then
    FORCE_ARG="--force"
fi

PACKAGES=(
    # need to make things work
    make
    # open-iscsi is used to test authorized networks for iscsi
    open-iscsi
    # Integration and unit test utils, python package management
    python3-cryptography
    python3-pip
    python3-pyfakefs
    python3-pyotp
    python3-pytest
    python3-pytest-asyncio
    python3-pytest-dependency
    python3-pytest-rerunfailures
    python3-pytest-timeout
    # `snmpwalk` to test `snmp-agent`
    snmp
    # Used by the integration test runner
    sshpass
    # Unpack core dumps (https://jira.ixsystems.com/browse/NAS-113855)
    zstd
)
PIP_PACKAGES=()

if [ -f /usr/local/libexec/disable-rootfs-protection ]; then
    # Running on SCALE
    /usr/local/libexec/disable-rootfs-protection $FORCE_ARG
    if [ $? -ne 0 ]; then
        return 1
    fi
fi

apt update
apt install -y "${PACKAGES[@]}"
if [ "${#PIP_PACKAGES[@]}" -gt 0 ]; then
    pip install --break-system-packages "${PIP_PACKAGES[@]}"
fi
