#!/usr/bin/with-contenv bashio

    ####################
    # Initialization   #
    ####################

if bashio::config.has_value 'trusted_domains'; then
LAUNCHER="sudo -u abc php /data/config/www/nextcloud/occ" || bashio::log.info "/data/config/www/nextcloud/occ not found"
if ! bashio::fs.file_exists '/data/config/www/nextcloud/occ'; then
LAUNCHER=`find / -name "occ" -print -quit`
fi || bashio::log.info "occ not found"

bashio::log.info "Currently set trusted domains :"
$LAUNCHER config:system:get trusted_domains || bashio::log.info "No trusted domain set yet. The first one will be set when doing initial configuration"

    bashio::log.info "Trusted domains set in the configuration. Refreshing domains."  && \
    ###################################
    # Remove previous trusted domains #
    ###################################
    bashio::log.info "... removing previously added trusted domain (except for first one created)"
    i=2
    until [ $i -gt 5 ]
    do
      $LAUNCHER config:system:delete trusted_domains $i && \
      ((i=i+1)) || exit
    done

    ###########################
    # Add new trusted domains #
    ###########################
    TRUSTEDDOMAINS=$(bashio::config 'trusted_domains')
    bashio::log.info "... alignement with trusted domains list : ${TRUSTEDDOMAINS}"
    for domain in ${TRUSTEDDOMAINS//,/ } # Comma separated values
    do
        bashio::log.info "... adding ${domain}"
        $LAUNCHER config:system:set trusted_domains $i --value=${domain}
        i=$((i+1))
    done

bashio::log.info "Remaining configurated trusted domains :"
bashio::log.info "$($LAUNCHER config:system:get trusted_domains)" || exit

fi

