#!/bin/bash
# info: list user nameservers
# options: USER [FORMAT]
#
# Function for obtainig the list of user's DNS servers.


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

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

# Includes
source $VESTA/func/main.sh

# Json function
json_list_ns() {
    ns=$(grep "^NS='" $USER_DATA/user.conf |cut -f 2 -d \')
    echo '['
    i=1
    nslistc=$(echo -e "${ns//,/\n}"|wc -l)
    for nameserver in ${ns//,/ };do
        if [ "$i" -ne "$nslistc" ]; then
            echo -e  "\t\"$nameserver\","
        else
            echo -e  "\t\"$nameserver\""
        fi
        (( ++i))
    done
    echo "]"
}

# Shell function
shell_list_ns() {
    ns=$(grep "^NS='" $USER_DATA/user.conf |cut -f 2 -d \')
    if [ -z "$nohead" ]; then
        echo "NAMESERVER"
        echo "----------"
    fi
    for nameserver in ${ns//,/ };do
        echo "$nameserver"
    done
}


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

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


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

# Listing nameservers
case $format in 
    json)   json_list_ns ;;
    plain)  nohead=1; shell_list_ns ;;
    shell)  shell_list_ns ;;
    *)      check_args '1' '0' 'USER [FORMAT]'
esac


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

exit
