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


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

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

# Includes
source $VESTA/func/main.sh

# Json function
json_list_package() {
    i=1
    fileds_count=$(echo "$fields" | wc -w)
    pkg_data=$(cat $VESTA/data/packages/$PACKAGE.pkg)
    echo '{'
    eval $pkg_data
    for field in $fields; do
        eval value=$field
        if [ $i -eq 1 ]; then
            echo -e "\t\"$value\": {"
        else
            if [ $fileds_count -eq $i ]; then
                echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
            else
                echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
            fi
        fi
    (( ++i))
    done
    if [ -n "$value" ]; then
        echo -e '        }'
    fi
    echo -e '}'
}

# Shell function
shell_list_package() {
    line=$(cat $VESTA/data/packages/$PACKAGE.pkg)
    eval $line
    for field in $fields; do
        eval key="$field"
        if [ -z "$key" ]; then
            key='NULL'
        fi
        echo "${field//$/}: $key "
    done
}


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

# Checking args
check_args '1' "$#" 'PACKAGE [FORMAT]'
validate_format 'package'
is_package_valid


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

# Defining fileds to select
fields='$PACKAGE $WEB_TEMPLATE $PROXY_TEMPLATE $DNS_TEMPLATE $WEB_DOMAINS
    $WEB_ALIASES $DNS_DOMAINS $DNS_RECORDS $MAIL_DOMAINS $MAIL_ACCOUNTS
    $DATABASES $CRON_JOBS $DISK_QUOTA $BANDWIDTH $NS $SHELL $BACKUPS $TIME
    $DATE'

# Listing ip
case $format in 
    json)   json_list_package ;;
    plain)  shell_list_package ;;
    shell)  shell_list_package | column -t ;;
    *)      check_args '1' '0' 'IP [FORMAT]'
esac


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

exit
