#!/bin/bash
# info: list system users
# options: [FORMAT]
#
# The function for obtainig the list of all server's users.


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

# Argument defenition
format=${1-shell}

# Json function
json_list_users() {
    echo '{'
    fileds_count=$(echo "$fields" | wc -w)
    for USER in $(grep @ /etc/passwd|cut -f 1 -d :); do
        source $VESTA/data/users/$USER/user.conf
        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
    done
    if [ -n "$data" ]; then
        echo -e '        }'
    fi
    echo -e '}'
}

# Shell function
shell_list_users() {
    if [  -z "$nohead" ]; then
        echo "${fields//$/}"
        for a in $fields; do
            echo -e "--------- \c"
        done
        echo
    fi

    for USER in $(grep @ /etc/passwd|cut -f 1 -d :); do
        source $VESTA/data/users/$USER/user.conf
        for field in $fields; do
            eval value=$field
            if [ -z "$value" ]; then
                value='NULL'
            fi
            echo -n "$value "
        done
        echo
    done
}


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

# Defining fileds to select
fields="\$USER \$FNAME \$LNAME \$PACKAGE \$WEB_TEMPLATE \$PROXY_TEMPLATE"
fields="$fields \$DNS_TEMPLATE \$WEB_DOMAINS \$WEB_ALIASES \$DNS_DOMAINS"
fields="$fields \$DNS_RECORDS \$MAIL_DOMAINS \$MAIL_ACCOUNTS \$DATABASES"
fields="$fields \$CRON_JOBS \$DISK_QUOTA \$BANDWIDTH \$NS \$SHELL \$BACKUPS"
fields="$fields \$CONTACT \$CRON_REPORTS \$RKEY \$SUSPENDED \$SUSPENDED_USERS"
fields="$fields \$SUSPENDED_WEB \$SUSPENDED_DNS \$SUSPENDED_MAIL"
fields="$fields \$SUSPENDED_DB \$SUSPENDED_CRON \$IP_AVAIL \$IP_OWNED"
fields="$fields \$U_USERS \$U_DISK \$U_DISK_DIRS \$U_DISK_WEB \$U_DISK_MAIL"
fields="$fields \$U_DISK_DB \$U_BANDWIDTH \$U_WEB_DOMAINS \$U_WEB_SSL"
fields="$fields \$U_WEB_ALIASES \$U_DNS_DOMAINS \$U_DNS_RECORDS"
fields="$fields \$U_MAIL_DOMAINS \$U_MAIL_DKIM \$U_MAIL_ACCOUNTS"
fields="$fields \$U_DATABASES \$U_CRON_JOBS \$U_BACKUPS \$LANGUAGE"
fields="$fields \$TIME \$DATE"

# Listing domains
case $format in 
    json)   json_list_users ;;
    plain)  nohead=1; shell_list_users ;;
    shell)  fields='$USER $PACKAGE $U_DISK $U_BANDWIDTH $SUSPENDED $DATE';
            shell_list_users | column -t ;;
esac


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

exit
