#!/bin/bash
# info: list users
# options: [FORMAT]
#
# The function to obtain the list of all system users.


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

# Argument definition
format=${1-shell}

# JSON list function
json_list() {
    echo '{'
    i=1
    while read USER; do
        source $HESTIA/data/users/$USER/user.conf
        if [ ! -f "$HESTIA/data/users/$USER/user.conf" ]; then
            continue;
        fi
        if [ $i -gt 1 ]; then
            echo ","
        fi
        source $HESTIA/data/users/$USER/user.conf
        echo -n '    "'$USER'": {
        "NAME": "'$NAME'",
        "PACKAGE": "'$PACKAGE'",
        "WEB_TEMPLATE": "'$WEB_TEMPLATE'",
        "BACKEND_TEMPLATE": "'$BACKEND_TEMPLATE'",
        "PROXY_TEMPLATE": "'$PROXY_TEMPLATE'",
        "DNS_TEMPLATE": "'$DNS_TEMPLATE'",
        "WEB_DOMAINS": "'$WEB_DOMAINS'",
        "WEB_ALIASES": "'$WEB_ALIASES'",
        "DNS_DOMAINS": "'$DNS_DOMAINS'",
        "DNS_RECORDS": "'$DNS_RECORDS'",
        "MAIL_DOMAINS": "'$MAIL_DOMAINS'",
        "MAIL_ACCOUNTS": "'$MAIL_ACCOUNTS'",
        "DATABASES": "'$DATABASES'",
        "CRON_JOBS": "'$CRON_JOBS'",
        "DISK_QUOTA": "'$DISK_QUOTA'",
        "BANDWIDTH": "'$BANDWIDTH'",
        "NS": "'$NS'",
        "SHELL": "'$SHELL'",
        "BACKUPS": "'$BACKUPS'",
        "CONTACT": "'$CONTACT'",
        "CRON_REPORTS": "'$CRON_REPORTS'",
        "RKEY": "'$RKEY'",
        "SUSPENDED": "'$SUSPENDED'",
        "SUSPENDED_USERS": "'$SUSPENDED_USERS'",
        "SUSPENDED_WEB": "'$SUSPENDED_WEB'",
        "SUSPENDED_DNS": "'$SUSPENDED_DNS'",
        "SUSPENDED_MAIL": "'$SUSPENDED_MAIL'",
        "SUSPENDED_DB": "'$SUSPENDED_DB'",
        "SUSPENDED_CRON": "'$SUSPENDED_CRON'",
        "IP_AVAIL": "'$IP_AVAIL'",
        "IP_OWNED": "'$IP_OWNED'",
        "U_USERS": "'$U_USERS'",
        "U_DISK": "'$U_DISK'",
        "U_DISK_DIRS": "'$U_DISK_DIRS'",
        "U_DISK_WEB": "'$U_DISK_WEB'",
        "U_DISK_MAIL": "'$U_DISK_MAIL'",
        "U_DISK_DB": "'$U_DISK_DB'",
        "U_BANDWIDTH": "'$U_BANDWIDTH'",
        "U_WEB_DOMAINS": "'$U_WEB_DOMAINS'",
        "U_WEB_SSL": "'$U_WEB_SSL'",
        "U_WEB_ALIASES": "'$U_WEB_ALIASES'",
        "U_DNS_DOMAINS": "'$U_DNS_DOMAINS'",
        "U_DNS_RECORDS": "'$U_DNS_RECORDS'",
        "U_MAIL_DOMAINS": "'$U_MAIL_DOMAINS'",
        "U_MAIL_DKIM": "'$U_MAIL_DKIM'",
        "U_MAIL_ACCOUNTS": "'$U_MAIL_ACCOUNTS'",
        "U_DATABASES": "'$U_DATABASES'",
        "U_CRON_JOBS": "'$U_CRON_JOBS'",
        "U_BACKUPS": "'$U_BACKUPS'",
        "LANGUAGE": "'$LANGUAGE'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
        }'
        ((i++))
    done < <(grep '@' /etc/passwd |cut -f1 -d:)
    echo '}'
}

# SHELL list function
shell_list() {
    echo "USER   PKG   WEB   DNS   MAIL   DB   DISK   BW   SPND   DATE"
    echo "----   ---   ---   ---   ---    --   ----   --   ----   ----"
    while read USER; do
        source $HESTIA/data/users/$USER/user.conf
        if [ ! -f "$HESTIA/data/users/$USER/user.conf" ]; then
            continue;
        fi
        source $HESTIA/data/users/$USER/user.conf
        echo -n "$USER $PACKAGE $U_WEB_DOMAINS $U_DNS_DOMAINS $U_MAIL_DOMAINS"
        echo " $U_DATABASES $U_DISK $U_BANDWIDTH $SUSPENDED $DATE"
    done < <(grep '@' /etc/passwd |cut -f1 -d:)
}

# PLAIN list function
plain_list() {
    while read USER; do
        source $HESTIA/data/users/$USER/user.conf
        if [ ! -f "$HESTIA/data/users/$USER/user.conf" ]; then
            continue;
        fi
        source $HESTIA/data/users/$USER/user.conf
        echo -ne "$USER\t$NAME\t$PACKAGE\t$WEB_TEMPLATE\t"
        echo -ne "$BACKEND_TEMPLATE\t$PROXY_TEMPLATE\t$DNS_TEMPLATE\t"
        echo -ne "$WEB_DOMAINS\t$WEB_ALIASES\t$DNS_DOMAINS\t$DNS_RECORDS\t"
        echo -ne "$MAIL_DOMAINS\t$MAIL_ACCOUNTS\t$DATABASES\t$CRON_JOBS\t"
        echo -ne "$DISK_QUOTA\t$BANDWIDTH\t$NS\t$SHELL\t$BACKUPS\t"
        echo -ne "$CONTACT\t$CRON_REPORTS\t$RKEY\t$SUSPENDED\t"
        echo -ne "$SUSPENDED_USERS\t$SUSPENDED_WEB\t$SUSPENDED_DNS\t"
        echo -ne "$SUSPENDED_MAIL\t$SUSPENDED_DB\t$SUSPENDED_CRON\t"
        echo -ne "$IP_AVAIL\t$IP_OWNED\t$U_USERS\t$U_DISK\t$U_DISK_DIRS\t"
        echo -ne "$U_DISK_WEB\t$U_DISK_MAIL\t$U_DISK_DB\t$U_BANDWIDTH\t"
        echo -ne "$U_WEB_DOMAINS\t$U_WEB_SSL\t$U_WEB_ALIASES\t"
        echo -ne "$U_DNS_DOMAINS\t$U_DNS_RECORDS\t$U_MAIL_DOMAINS\t"
        echo -ne "$U_MAIL_DKIM\t$U_MAIL_ACCOUNTS\t$U_DATABASES\t"
        echo -e "$U_CRON_JOBS\t$U_BACKUPS\t$LANGUAGE\t$TIME\t$DATE\t"
    done < <(grep '@' /etc/passwd |cut -f1 -d:)
}

# CSV list function
csv_list() {
    echo -n "USER,NAME,PACKAGE,WEB_TEMPLATE,BACKEND_TEMPLATE,"
    echo -n "PROXY_TEMPLATE,DNS_TEMPLATE,WEB_DOMAINS,WEB_ALIASES,"
    echo -n "DNS_DOMAINS,DNS_RECORDS,MAIL_DOMAINS,MAIL_ACCOUNTS,"
    echo -n "DATABASES,CRON_JOBS,DISK_QUOTA,BANDWIDTH,NS,HOME,SHELL,"
    echo -n "BACKUPS,CONTACT,CRON_REPORTS,RKEY,SUSPENDED,SUSPENDED_USERS,"
    echo -n "SUSPENDED_WEB,SUSPENDED_DNS,SUSPENDED_MAIL,SUSPENDED_DB,"
    echo -n "SUSPENDED_CRON,IP_AVAIL,IP_OWNED,U_USERS,U_DISK,U_DISK_DIRS,"
    echo -n "U_DISK_WEB,U_DISK_MAIL,U_DISK_DB,U_BANDWIDTH,U_WEB_DOMAINS,"
    echo -n "U_WEB_SSL,U_WEB_ALIASES,U_DNS_DOMAINS,U_DNS_RECORDS,"
    echo -n "U_MAIL_DOMAINS,U_MAIL_DKIM,U_MAIL_ACCOUNTS,U_DATABASES"
    echo "U_CRON_JOBS,U_BACKUPS,LANGUAGE,TIME,DATE"
    while read USER; do
        source $HESTIA/data/users/$USER/user.conf
        if [ ! -f "$HESTIA/data/users/$USER/user.conf" ]; then
            continue;
        fi
        source $HESTIA/data/users/$USER/user.conf
        echo -n "$USER,\"$NAME\",$PACKAGE,$WEB_TEMPLATE,"
        echo -n "$BACKEND_TEMPLATE,$PROXY_TEMPLATE,$DNS_TEMPLATE,"
        echo -n "$WEB_DOMAINS,$WEB_ALIASES,$DNS_DOMAINS,$DNS_RECORDS,"
        echo -n "$MAIL_DOMAINS,$MAIL_ACCOUNTS,$DATABASES,$CRON_JOBS,"
        echo -n "$DISK_QUOTA,$BANDWIDTH,\"$NS\",$HOME,$SHELL,$BACKUPS,"
        echo -n "$CONTACT,$CRON_REPORTS,\"$RKEY\",$SUSPENDED,"
        echo -n "$SUSPENDED_USERS,$SUSPENDED_WEB,$SUSPENDED_DNS,"
        echo -n "$SUSPENDED_MAIL,$SUSPENDED_DB,$SUSPENDED_CRON,$IP_AVAIL,"
        echo -n "$IP_OWNED,$U_USERS,$U_DISK,$U_DISK_DIRS,$U_DISK_WEB,"
        echo -n "$U_DISK_MAIL,$U_DISK_DB,$U_BANDWIDTH,$U_WEB_DOMAINS,"
        echo -n "$U_WEB_SSL,$U_WEB_ALIASES,$U_DNS_DOMAINS,$U_DNS_RECORDS,"
        echo -n "$U_MAIL_DOMAINS,$U_MAIL_DKIM,$U_MAIL_ACCOUNTS,$U_DATABASES,"
        echo "$U_CRON_JOBS,$U_BACKUPS,$LANGUAGE,$TIME,$DATE"
    done < <(grep '@' /etc/passwd |cut -f1 -d:)
}

# Raw list function
raw_list() {
    while read USER; do
        echo $HESTIA/data/users/$USER/user.conf
        cat $HESTIA/data/users/$USER/user.conf
        if [ ! -f "$HESTIA/data/users/$USER/user.conf" ]; then
            continue;
        fi
        echo $HESTIA/data/users/$USER/user.conf
        cat $HESTIA/data/users/$USER/user.conf
    done < <(grep '@' /etc/passwd |cut -f1 -d:)
}


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

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


#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit
