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

# Usage: update-modules-list [database]

database="$DB_NAME"
if [[ -n "$1" ]]; then
    database="$1"
fi

if check-database-initialized "$database"; then
    log-entrypoint "Update modules list on database $database."

    # Execute Odoo shell command to call base.module.update:update_module()
    (exec odoo shell "${DB_ARGS[@]}" --database "$database" --config "$ODOO_RC" --stop-after-init --no-http << 'EOF'
updater = env['base.module.update']
updater.update_module()
env.cr.commit()
print('Module list updated successfully.')
EOF
) || true
else
    log-entrypoint "Database $database is not initialized, skipping modules list update."
fi