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

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

# Argument definition
format=${1-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" | tr -d ';' | awk '{print $1"="$2}')
	echo '{
    "CONFIG": {
        "worker_processes": "'$worker_processes'",
        "worker_connections": "'$worker_connections'",
        "send_timeout": "'$send_timeout'",
        "proxy_connect_timeout": "'$proxy_connect_timeout'",
        "proxy_send_timeout": "'$proxy_send_timeout'",
        "proxy_read_timeout": "'$proxy_read_timeout'",
        "client_max_body_size": "'$client_max_body_size'",
        "gzip": "'$gzip'",
        "gzip_comp_level": "'$gzip_comp_level'",
        "charset": "'$charset'",
        "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" | awk '{print $2}' | tr -d ';' | tr '\n' ','
	echo
}

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Defining config path
config_path='/etc/nginx/nginx.conf'

# Defining keys
keys="worker_processes |worker_connections |send_timeout"
keys="$keys |proxy_connect_timeout |proxy_send_timeout"
keys="$keys |proxy_read_timeout |client_max_body_size"
keys="$keys |gzip |gzip_comp_level |charset "

# Reading nginx config
config=$(cat $config_path)

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

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

exit
