#!/bin/bash
# info: list backup host
# options: [FORMAT]
#
# The function for obtaining the list of back fto host.


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

# Argument defenition
format=${1-shell}

# Includes
source $VESTA/func/main.sh

# Json function
json_list_ftp_host() {
    i=1
    fileds_count=$(echo "$fields" | wc -w)
    ip_data=$(cat $VESTA/conf/ftp.backup.conf)
    echo '{'
    eval $ip_data
    for field in $fields; do
        eval value=$field
        if [ $i -eq 1 ]; then
            echo -e "\t\"$value\": {"
        else
            if [ $fileds_count -eq $i ]; then
                echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
            else
                echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
            fi
        fi
    (( ++i))
    done
    if [ -n "$value" ]; then
        echo -e '        }'
    fi
    echo -e '}'
}

# Shell function
shell_list_ftp_host() {
    line=$(cat $VESTA/conf/ftp.backup.conf)
    eval $line
    for field in $fields; do
        eval key="$field"
        if [ -z "$key" ]; then
            key='NULL'
        fi
        echo "${field//$/}: $key "
    done
}


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


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

if [ ! -e "$VESTA/conf/ftp.backup.conf" ]; then
    exit
fi

# Defining fileds to select
fields='$HOST $USERNAME $PORT $TIME $DATE'

# Listing database
case $format in 
    json)   json_list_ftp_host ;;
    plain)  nohead=1; shell_list_ftp_host;;
    shell)  shell_list_ftp_host | column -t ;;
    *)      check_args '2' '0' '[FORMAT]'
esac


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

exit
