#!/bin/bash
# info: list web domain parameters
# options: USER DOMAIN [FORMAT]
#
# example: v-list-web-domain admin example.com
#
# This function to obtain web domain parameters.

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

# Argument definition
user=$1
domain=$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
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	echo '{'
	echo '    "'$DOMAIN'": {
        "IP": "'$IP'",
        "IP6": "'$IP6'",
        "DOCUMENT_ROOT": "'$DOCROOT'",
        "U_DISK": "'$U_DISK'",
        "U_BANDWIDTH": "'$U_BANDWIDTH'",
        "TPL": "'$TPL'",
        "ALIAS": "'$ALIAS'",
        "STATS": "'$STATS'",
        "STATS_USER": "'$STATS_USER'",
        "SSL": "'$SSL'",
        "SSL_FORCE": "'$SSL_FORCE'",
        "SSL_HSTS": "'$SSL_HSTS'",
        "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'",
        "FASTCGI_CACHE": "'$FASTCGI_CACHE'",
        "FASTCGI_DURATION": "'$FASTCGI_DURATION'",
        "REDIRECT": "'$REDIRECT'",
        "REDIRECT_CODE": "'$REDIRECT_CODE'",
        "CUSTOM_DOCROOT": "'$CUSTOM_DOCROOT'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'",
		"PROXY_PORT_INTERNAL": "'$PROXY_PORT_INTERNAL'"
    }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "DOMAIN:         $DOMAIN"
	echo "ALIAS:          ${ALIAS//,/ }"
	echo "IP:             $IP"
	echo "DOCUMENT_ROOT:  $DOCROOT"
	if [ -n "$IP6" ]; then
		echo "IP6:            $IP6"
	fi
	if [ -n "$SSL" ] && [ "$SSL" != 'no' ]; then
		echo "SSL:            $SSL / $SSL_HOME"
		if [ -n "$LETSENCRYPT" ] && [ "$LETSENCRYPT" != 'no' ]; then
			echo "LETSENCRYPT:    $LETSENCRYPT"
		fi
		if [ -n "$SSL_FORCE" ]; then
			echo "SSL_FORCE:       $SSL_FORCE"
		fi
		if [ -n "$SSL_HSTS" ]; then
			echo "SSL_HSTS:       $SSL_HSTS"
		fi
	fi
	echo "TEMPLATE:       $TPL"
	if [ -n "$WEB_BACKEND" ]; then
		echo "BACKEND:        $BACKEND"
	fi
	if [ -n "$PROXY_SYSTEM" ]; then
		echo "PROXY:          $PROXY"
		echo "PROXY_PORT_INTERNAL:     $PROXY_PORT_INTERNAL"
		echo "PROXY EXT:      ${PROXY_EXT//,/ }"
	fi
	if [ -n "$STATS" ]; then
		echo "STATS:          $STATS"
	fi
	if [ -n "$FTP_USER" ]; then
		echo "FTP_USER:       ${FTP_USER//,/, }"
	fi
	if [ -n "$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$DOCROOT\t$U_DISK\t$U_BANDWIDTH\t$TPL\t"
	echo -ne "$ALIAS\t$STATS\t$STATS_USER\t$SSL\t$SSL_FORCE\t$SSL_HSTS\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\t$PROXY_PORT_INTERNAL"
}

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

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

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

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

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

if [ -n "$CUSTOM_DOCROOT" ]; then
	DOCROOT="$CUSTOM_DOCROOT"
else
	DOCROOT="$HOMEDIR/$user/web/$DOMAIN/public_html/"
fi

if [ -z "$PROXY_PORT_INTERNAL" ];then
	PROXY_PORT_INTERNAL="0"
fi

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

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

exit
