#!/bin/bash
# info: list system IP
# options: IP [FORMAT]
#
# example: v-list-sys-ip 203.0.113.1
#
# This function for getting the list of system IP parameters.

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

# Argument definition
ip="$1"
format="${2-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
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	echo '{'
	echo '    "'$ip'": {
        "OWNER": "'$OWNER'",
        "STATUS": "'$STATUS'",
        "NAME": "'$NAME'",
        "U_SYS_USERS": "'$U_SYS_USERS'",
        "U_WEB_DOMAINS": "'$U_WEB_DOMAINS'",
        "INTERFACE": "'$INTERFACE'",
        "NETMASK": "'$NETMASK'",
        "NAT": "'$NAT'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
        }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "IP:             $ip"
	echo "NETMASK:        $NETMASK"
	echo "INTERFACE:      $INTERFACE"
	echo "NAT:            $NAT"
	echo "OWNER:          $OWNER"
	echo "STATUS:         $STATUS"
	echo "NAME:           $NAME"
	echo "USERS:          $U_SYS_USERS"
	echo "DOMAINS:        $U_WEB_DOMAINS"
	echo "TIME:           $TIME"
	echo "DATE:           $DATE"
}

# PLAIN list function
plain_list() {
	echo -ne "$ip\t$OWNER\t$STATUS\t$NAME\t$U_SYS_USERS\t$U_WEB_DOMAINS\t"
	echo -e "$INTERFACE\t$NETMASK\t$NAT\t$TIME\t$DATE"
}

# CSV list function
csv_list() {
	echo -n "IP,OWNER,STATUS,NAME,U_SYS_USERS,U_WEB_DOMAINS,INTERFACE"
	echo "NETMASK,NAT,TIME,DATE"
	echo -n "$ip,$OWNER,$STATUS,$NAME,\"$U_SYS_USERS\",$U_WEB_DOMAINS,"
	echo "$INTERFACE, $NETMASK,$NAT,$TIME,$DATE"
}

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

check_args '1' "$#" 'IP [FORMAT]'
is_format_valid 'ip'
if [ ! -e "$HESTIA/data/ips/$ip" ]; then
	check_result $E_NOTEXIST "IP $ip doesn't exist"
fi

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

# Parsing IP
source_conf "$HESTIA/data/ips/$ip"

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

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

exit
