#!/bin/bash
# info: list user ips
# options: USER [FORMAT]
#
# The function for obtainig the list of available ip addresses.


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

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

# Includes
source $VESTA/func/main.sh

# Json function
json_list_user_ips() {
    echo '{'
    fileds_count=$(echo "$fields" | wc -w)
    for IP in $(ls $VESTA/data/ips/); do
        source $VESTA/data/ips/$IP
        if [ "$OWNER" = "$user" ]; then
            if [ -n "$data" ]; then
                echo -e '        },'
            fi
            i=1
            for field in $fields; do
                eval value=$field
                if [ $i -eq 1 ]; then
                    (( ++i))
                    echo -e "\t\"$value\": {"
                else
                    if [ $i -lt $fileds_count ]; then
                        (( ++i))
                        echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
                    else
                        echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
                        data=1
                    fi
                fi
            done
        else
            if  [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
                if [ -n "$data" ]; then
                    echo -e '        },'
                fi
                i=1

                for field in $fields; do
                    eval value=$field
                    if [ $i -eq 1 ]; then
                        (( ++i))
                        echo -e "\t\"$value\": {"
                    else
                        if [ $i -lt $fileds_count ]; then
                            (( ++i))
                            echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
                        else
                            echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
                            data=1
                        fi
                    fi
                done
            fi
        fi
    done
    if [ -n "$data" ]; then
        echo -e '        }'
    fi
    echo -e '}'

}

# Shell function
shell_list_user_ips() {
    for IP in $(ls $VESTA/data/ips/); do
        source $VESTA/data/ips/$IP
        if [ "$OWNER" = "$user" ]; then
            for field in $fields; do
                eval value=$field
                if [ -z "$value" ]; then
                    value='NULL'
                fi
                echo -n "$value "
            done
            echo
        else
            if  [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
                for field in $fields; do
                    eval value=$field
                    if [ -z "$value" ]; then
                        value='NULL'
                    fi
                    echo -n "$value "
                done
                echo
            fi
        fi
    done
}


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

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


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

# Defining fileds to select
fields='$IP $OWNER $STATUS $NAME $NAT'

# Listing ips
case $format in
    json)   json_list_user_ips ;;
    plain)  nohead=1; shell_list_user_ips ;;
    shell)  shell_list_user_ips | column -t ;;
    *)      check_args '1' '0' 'USER [FORMAT]' ;;
esac


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

exit
