#!/bin/bash
# info: list database host
# options: TYPE HOST [FORMAT]
#
# The function for obtaining database host parameters.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
type=$1
host=$2
format=${3-shell}

# Includes
source $VESTA/func/main.sh

# JSON list function
json_list() {
    echo '{'
    echo '    "'$HOST'": {
        "HOST": "'$HOST'",
        "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 "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$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,TYPE,CHARSETS,MAX_DB,U_SYS_USERS,"
    echo "U_DB_BASES,TPL,SUSPENDED,TIME,DATE'"
    echo -n "$HOST,$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_type_format_valid "$type"
is_object_valid "../../conf/$type" 'HOST' "$host"


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

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

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


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

exit
