#!/bin/bash
# info: list web domain parameters
# options: USER DOMAIN [FORMAT]
#
# The function to obtain web domain parameters.


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

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

# Includes
source $VESTA/func/main.sh

# JSON list function
json_list() {
    echo '{'
    echo '    "'$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'",
        "DOCUMENT_ROOT": "'$HOMEDIR/$user/web/$domain/public_html'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
    echo '}'
}

# SHELL list function
shell_list() {
    source $VESTA/conf/vesta.conf
    echo "DOMAIN:         $DOMAIN"
    echo "ALIAS:          ${ALIAS//,/ }"
    echo "IP:             $IP"
    if [ ! -z "$IP6" ]; then
        echo "IP6:            $IP6"
    fi
    if [ ! -z "$SSL" ] && [ "$SSL" != 'no' ]; then
        echo "SSL:            $SSL / $SSL_HOME"
        if [ ! -z "$LETSENCRYPT" ] && [ "$LETSENCRYPT" != 'no' ]; then
         echo "LETSENCRYPT:    $LETSENCRYPT"
        fi
    fi
    echo "TEMPLATE:       $TPL"
    if [ ! -z "$WEB_BACKEND" ]; then
        echo "BACKEND:        $BACKEND"
    fi
    if [ ! -z "$PROXY_SYSTEM" ]; then
        echo "PROXY:          $PROXY"
        echo "PROXY EXT:      ${PROXY_EXT//,/ }"
    fi
    if [ ! -z "$STATS" ]; then
        echo "STATS:          $STATS"
    fi
    if [ ! -z "$FTP_USER" ]; then
        echo "FTP_USER:       ${FTP_USER//,/, }"
    fi
    if [ ! -z "$AUTH_USER" ]; then
        echo "HTTP_AUTH:      $AUTH_USER"
    fi
    echo "DISK:           $U_DISK"
    echo "BW:             $U_BANDWIDTH"
    echo "SUSPENDED:      $SUSPENDED"
    echo "TIME:           $TIME"
    echo "DATE:           $DATE"
}

# PLAIN list function
plain_list() {
    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"
    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"
}

# CSV list function
csv_list() {
    echo -n "DOMAIN,IP,IP6,U_DISK,U_BANDWIDTH,TPL,ALIAS,STATS,STATS_USER,SSL,"
    echo -n "SSL_HOME,LETSENCRYPT,FTP_USER,FTP_PATH,AUTH_USER,BACKEND,PROXY,PROXY_EXT,"
    echo "SUSPENDED,TIME,DATE"
    echo -n "$DOMAIN,$IP,$IP6,$U_DISK,$U_BANDWIDTH,$TPL,\"$ALIAS\",$STATS"
    echo -n "\"$STATS_USER\",$SSL,$SSL_HOME,$LETSENCRYPT,\"$FTP_USER\",\"$FTP_PATH\","
    echo -n "\"$AUTH_USER\",$BACKEND,$PROXY,\"$PROXY_EXT\",$SUSPENDED,$TIME,"
    echo  "$DATE"
}


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

check_args '2' "$#" 'USER DOMAIN [FORMAT]'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"


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

# Parsing domain
eval $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)

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


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

exit
