#!/bin/bash
# info: list database host
# options: TYPE HOST [FORMAT]
#
# example: v-list-database-host mysql localhost
#
# This function for obtaining database host parameters.

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

# Argument definition
type=$1
host=$2
format=${3-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
# shellcheck source=/usr/local/hestia/func/db.sh
source $HESTIA/func/db.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	echo '{'
	echo '    "'$HOST'": {
        "HOST": "'$HOST'",
        "PORT": "'$PORT'",
        "TYPE": "'$type'",
        "CHARSETS": "'$CHARSETS'",
        "MAX_DB": "'$MAX_DB'",
        "U_SYS_USERS": "'$U_SYS_USERS'",
        "U_DB_BASES": "'$U_DB_BASES'",
        "TPL": "'$TPL'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "HOST:           $HOST"
	echo "PORT:           $PORT"
	echo "TYPE:           $type"
	echo "CHARSETS:       $CHARSETS"
	echo "MAX_DB:         $MAX_DB"
	echo "U_DB_BASES:     $U_DB_BASES"
	echo "U_SYS_USERS:    $U_SYS_USERS"
	echo "TPL:            $TPL"
	echo "SUSPENDED:      $SUSPENDED"
	echo "TIME:           $TIME"
	echo "DATE:           $DATE"
}

# PLAIN list function
plain_list() {
	echo -ne "$HOST\t$PORT\t$type\t$CHARSETS\t$MAX_DB\t$U_SYS_USERS\t"
	echo -e "$U_DB_BASES\t$TPL\t$SUSPENDED\t$TIME\t$DATE"
}

# CSV list function
csv_list() {
	echo -n "HOST,PORT,TYPE,CHARSETS,MAX_DB,U_SYS_USERS,"
	echo "U_DB_BASES,TPL,SUSPENDED,TIME,DATE'"
	echo -n "$HOST,$PORT,$type,\"$CHARSETS\",$MAX_DB,\"$U_SYS_USERS\","
	echo "$U_DB_BASES,$TPL,$SUSPENDED,$TIME,$DATE"
}

# Type format validator
is_type_format_valid() {
	exclude="[!|#|$|^|&|(|)|+|=|{|}|:|@|<|>|?|/|\|\"|'|;|%|\`| ]|\."
	if [[ "$1" =~ $exclude ]]; then
		check_result "$E_INVALID" "invalid type extention format :: $1"
	fi
}

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

check_args '2' "$#" 'TYPE HOST [FORMAT]'
is_format_valid 'host'
is_type_format_valid "$type"
is_object_valid "../../conf/$type" 'HOST' "$host"

# Set default port values if they don't exist in host configuration file
database_set_default_ports

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

# Parsing hosts
parse_object_kv_list $(grep "HOST='$host'" $HESTIA/conf/$type.conf)

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

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

exit
