#!/bin/bash
# info: list system ip
# options: IP [FORMAT]
#
# The function for getting the list of system ip parameters.


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

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

# Includes
source $VESTA/func/main.sh
source $VESTA/func/ip.sh

# 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 "$VESTA/data/ips/$ip" ]; then
    check_result $E_NOTEXIST "ip $ip doesn't exist"
fi


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

# Parsing ip
source $VESTA/data/ips/$ip

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


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

exit
