#!/bin/bash
# shellcheck shell=bash
set -e

# Source environment variables for cron context
# shellcheck disable=SC1091
if [ -f /etc/environment ]; then set -a; . /etc/environment; set +a; fi
# shellcheck disable=SC1091
if [ -f /.env ]; then set -a; . /.env; set +a; fi

# Use persisted value instead of calling bashio::config (www-data may lack API access)
CONFIGSOURCE="${CONFIG_LOCATION}"

(
echo "[INFO] Running update according to defined schedule. Files located in $CONFIGSOURCE/import_files will be imported."
if [ "${SILENT_MODE}" = "true" ]; then
    echo "[WARNING] Silent mode activated. Only errors will be shown. Please disable in addon options if you need to debug"
    sudo -Eu www-data /usr/local/bin/php /var/www/html/artisan importer:auto-import "$CONFIGSOURCE/import_files" >/dev/null
else
    sudo -Eu www-data /usr/local/bin/php /var/www/html/artisan importer:auto-import "$CONFIGSOURCE/import_files"
fi
) &>/proc/1/fd/1
