#!/bin/bash
# info: list php config parameters
# options: [VERSION] [FORMAT]
#
# example: v-list-sys-php-config
#
# This function for obtaining the list of php config parameters.

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

# Argument definition
php_ver=${1-default}
format=${2-shell}

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	parse_object_kv_list $(echo "$config" | egrep "$keys" \
		| sed -e "s/[ ]*=/=/" -e "s/=[ ]*/=\'/" -e "s/$/'/")
	echo '{
    "CONFIG": {
        "memory_limit": "'$memory_limit'",
        "max_execution_time": "'$max_execution_time'",
        "max_input_time": "'$max_input_time'",
        "upload_max_filesize": "'$upload_max_filesize'",
        "post_max_size": "'$post_max_size'",
        "display_errors": "'$display_errors'",
        "error_reporting": "'$error_reporting'",
        "config_path": "'$config_path'"
        }
}'
}

# SHELL list function
shell_list() {
	echo "$config" | egrep "$keys" | tr -d '='
	echo "config_path $config_path"
}

# PLAIN list function
plain_list() {
	echo "$config" | egrep "$keys" | tr -d '='
	echo "config_path $config_path"
}

# CSV list function
csv_list() {
	echo "$keys" | sed "s/ |/,/g"
	echo "$config" | egrep "$keys" | tr -d '=' | awk '{print $2}' | tr '\n' ','
	echo
}

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#
declare -a versions
if [ "$php_ver" == "default" ]; then
	# List through /etc/php
	if [ "$LOCAL_PHP" == "yes" ]; then
		for version in /opt/brepo/php*/etc/php-fpm.d/www.conf; do
			ver=$(echo "$version" | awk -F"/" '{ print $4 }' | sed  "s/php\([[:digit:]]\+\)/\1/g")
			if [ "$ver" != "php*" ]; then
				versions+=("$ver")
			fi
		done
	else
		for version in /etc/opt/remi/php*/php-fpm.d/www.conf; do
			ver=$(echo "$version" | awk -F"/" '{ print $5 }' | sed  "s/php\([[:digit:]]\+\)/\1/g")
			if [ "$ver" != "php*" ]; then
				versions+=("$ver")
			fi
		done
	fi
else
	ver=$(echo "$php_ver" | sed  "s/php\([[:digit:]]\+\)/\1/g")
	versions+=("$ver")
fi

if [ ${#versions[@]} -eq 0 ]; then
    php_ver="82"
else
    php_ver="${versions[0]}"
fi

# Defining config path
if [ "$LOCAL_PHP" == "yes" ]; then
	config_path=$(find /opt/brepo/php${php_ver}/* -name php.ini)
else
	config_path=$(find /etc/opt/remi/php${php_ver}/* -name php.ini)
fi

config_count=$(echo "$config_path" | wc -l)
if [ "$config_count" -gt 1 ]; then
	if [ "$LOCAL_PHP" == "yes" ]; then
		multiphp_versions=$(ls -d /opt/brepo/php*/etc/php-fpm.d 2> /dev/null | wc -l)
	else
		multiphp_versions=$(ls -d /etc/opt/remi/php*/php-fpm.d 2> /dev/null | wc -l)
	fi
	if [ "$WEB_BACKEND" = 'php-fpm' ] || [ "$multiphp_versions" -gt 0 ]; then
		config_path=$(echo "$config_path" | grep "php")
	else
		config_path=$(echo "$config_path" | grep httpd)
	fi
fi

# Defining keys
keys="memory_limit |max_execution_time |max_input_time"
keys="$keys |upload_max_filesize |post_max_size"
keys="$keys |display_errors |error_reporting "

# Reading config
config=$(cat "$config_path" | grep -v "^;")

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list | column -t ;;
esac

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit