#!/bin/bash
# info: list web domains
# options: USER [FORMAT]
#
# The function to obtain the list of all user web domains.


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

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

# Includes
source $HESTIA/func/main.sh

# JSON list function
json_list() {
    IFS=$'\n'
    i=1
    objects=$(grep DOMAIN $USER_DATA/web.conf |wc -l)
    echo "{"
    while read str; do
        parse_object_kv_list "$str"
        echo -n '    "'$DOMAIN'": {
        "IP": "'$IP'",
        "IP6": "'$IP6'",
        "U_DISK": "'$U_DISK'",
        "U_BANDWIDTH": "'$U_BANDWIDTH'",
        "TPL": "'$TPL'",
        "ALIAS": "'$ALIAS'",
        "STATS": "'$STATS'",
        "STATS_USER": "'$STATS_USER'",
        "SSL": "'$SSL'",
        "SSL_HOME": "'$SSL_HOME'",
        "LETSENCRYPT": "'$LETSENCRYPT'",
        "FTP_USER": "'$FTP_USER'",
        "FTP_PATH": "'$FTP_PATH'",
        "AUTH_USER": "'$AUTH_USER'",
        "BACKEND": "'$BACKEND'",
        "PROXY": "'$PROXY'",
        "PROXY_EXT": "'$PROXY_EXT'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
        if [ "$i" -lt "$objects" ]; then
            echo ','
        else
            echo
        fi
        ((i++))
    done < <(cat $USER_DATA/web.conf)
    echo '}'
}

# SHELL list function
shell_list() {
    IFS=$'\n'
    echo "DOMAIN   IP   TPL   SSL   DISK   BW   SPND   DATE"
    echo "------   --   ---   ---   ----   --   ----   -----"
    while read str; do
        parse_object_kv_list "$str"
        echo "$DOMAIN $IP $TPL $SSL $U_DISK $U_BANDWIDTH $SUSPENDED $DATE"
    done < <(cat $USER_DATA/web.conf)
}

# PLAIN list function
plain_list() {
    IFS=$'\n'
    while read str; do
        parse_object_kv_list "$str"
        echo -ne "$DOMAIN\t$IP\t$IP6\t$U_DISK\t$U_BANDWIDTH\t$TPL\t"
        echo -ne "$ALIAS\t$STATS\t$STATS_USER\t$SSL\t$SSL_HOME\t$LETSENCRYPT\t"
        echo -ne "$FTP_USER\t$FTP_PATH\t$AUTH_USER\t$BACKEND\t$PROXY\t"
        echo -e "$PROXY_EXT\t$SUSPENDED\t$TIME\t$DATE"
    done < <(cat $USER_DATA/web.conf)
}

# CSV list function
csv_list() {
    IFS=$'\n'
    echo -n "DOMAIN,IP,IP6,U_DISK,U_BANDWIDTH,TPL,ALIAS,STATS,STATS_USER,"
    echo -n "SSL,SSL_HOME,LETSENCRYPT,FTP_USER,FTP_PATH,AUTH_USER,BACKEND,PROXY,"
    echo "PROXY_EXT,SUSPENDED,TIME,DATE"
    while read str; do
        parse_object_kv_list "$str"
        echo -n "$DOMAIN,$IP,$IP6,$U_DISK,$U_BANDWIDTH,$TPL,"
        echo -n "\"$ALIAS\",$STATS,\"$STATS_USER\",$SSL,$SSL_HOME,$LETSENCRYPT,"
        echo -n "\"$FTP_USER\",\"$FTP_PATH\",\"$AUTH_USER\",$BACKEND,$PROXY,"
        echo "\"$PROXY_EXT\",$SUSPENDED,$TIME,$DATE"
    done < <(cat $USER_DATA/web.conf)
}


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

check_args '1' "$#" 'USER [FORMAT]'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"


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

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


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

exit
