#!/bin/bash
# info: change sysconfig value
# options: KEY VALUE
#
# example: v-change-sys-config-value VERSION 1.0
#
# This function is for changing main config settings such as COMPANY_NAME or
# COMPANY_EMAIL and so on.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
key=$(echo "$1" | tr '[:lower:]' '[:upper:]')
value=$2

# Includes
# shellcheck source=/etc/tuliocp/tulio.conf
source /etc/tuliocp/tulio.conf
# shellcheck source=/usr/local/tulio/func/main.sh
source $TULIO/func/main.sh
# shellcheck source=/usr/local/tulio/func/sysconfig.sh
source $TULIO/func/sysconfig.sh
# load config file
source_conf "$TULIO/conf/tulio.conf"

# Perform verification if read-only mode is enabled
check_tulio_demo_mode

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '2' "$#" 'KEY VALUE'
is_common_format_valid "$key" 'key'
format_no_quotes "$value" 'value'
#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# The value is rendered into a temporary file beside the configuration, checked
# and only then renamed over it. The previous version of this wrote the sorted
# result to a fixed path in /tmp and moved it in, which any local user could
# have replaced or symlinked in the window between the two, and which left the
# panel without a configuration file at all if the move failed halfway.
if ! sysconfig_write "$key" "$value" "$TULIO/conf/tulio.conf"; then
	check_result "$E_UPDATE" "unable to set $key in $TULIO/conf/tulio.conf"
fi

# Check if the variable "$key" has the value "APP_NAME"
if [ "$key" == "APP_NAME" ]; then
	# Path to the file manager configuration file where the change will be made.
	config_file="$TULIO/web/fm/configuration.php"
	new_app_name="File Manager - $value"

	# Verify if configuration.php exists and is writable
	if [ -f "$config_file" ] && [ -w "$config_file" ]; then
		# Sed replaces only the value after "File Manager -"
		sed -i "s|\(\$dist_config\[\"frontend_config\"\]\[\"app_name\"\] = \"File Manager - \).*\";|\1${value}\";|" "$config_file"
	fi
fi

#----------------------------------------------------------#
#                       Tulio                             #
#----------------------------------------------------------#

# Logging
$BIN/v-log-action "system" "Info" "System" "System configuration value changed (Key: $key, Value: $value)."
log_event "$OK" "$ARGUMENTS"

# Stated rather than inherited from log_event, whose result is the result of
# appending a line to the system log. Callers check this one with check_result
# - v-change-sys-api does it four times - so a log that could not be written
# used to be reported to them as a configuration change that had not landed,
# when it had. Failures above exit through check_result with their own code.
exit "$OK"
