#!/usr/bin/env bash
set -e

log-entrypoint 'Install python packages required for testing.'
uv pip install --no-cache-dir coverage websocket-client

log-entrypoint "Collect requirements.txt files in $TEST_ADDONS_DIR."
find "$TEST_ADDONS_DIR" -type f -name "requirements.txt" -exec uv pip install -r {} \;

log-entrypoint "Collect test-requirements.txt files in $TEST_ADDONS_DIR."
find "$TEST_ADDONS_DIR" -type f -name "test-requirements.txt" -exec uv pip install -r {} \;

if [[ -n "$TEST_INCLUDE" ]]; then
    test_depends_modules=$(manifestoo --select-include "$TEST_INCLUDE" --select-exclude "$TEST_EXCLUDE" list-depends --separator=,)
else
    test_depends_modules=$(manifestoo --select-addons-dir "$TEST_ADDONS_DIR" --select-exclude "$TEST_EXCLUDE" list-depends --separator=,)
fi

log-entrypoint "Install depends modules for testing: $test_depends_modules"
odoo_init_lang=${ODOO_INIT_LANG:="en_US"}
without_demo=${WITHOUT_DEMO:=True}
odoo_init_param=""
if [[ "$without_demo" = "True" ]]; then
    odoo_init_param="--without-demo=all"
fi
unbuffer /opt/odoo-venv/bin/odoo \
    -d "$ODOO_DATABASE" \
    -i "${test_depends_modules:-base}" \
    --load-language=$odoo_init_lang \
    --stop-after-init \
    --http-port=8068 \
    $odoo_init_param
