#!/bin/bash
# info: list user favourites
# options: USER [FORMAT]
#
# The function for getting the list of favourite user objects


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

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

# Includes
source $VESTA/func/main.sh

# JSON list function
json_list() {
    echo '{'
    echo '    "Favourites": {
        "USER": "'$USER'",
        "WEB": "'$WEB'",
        "DNS": "'$DN'",
        "DNS_REC": "'$DNS_REC'",
        "MAIL": "'$MAIL'",
        "MAIL_ACC": "'$MAIL_ACC'",
        "DB": "'$DB'",
        "CRON": "'$CRON'",
        "BACKUP": "'$BACKUP'",
        "IP": "'$IP'",
        "PACKAGE": "'$PACKAGE'",
        "FIREWALL": "'$FIREWALL'"
        }'
    echo '}'
}

# SHELL list function
shell_list() {
    echo "USER:           $USER"
    echo "WEB:            $WEB"
    echo "DNS:            $DNS"
    echo "DNS RECORD:     $DNS_REC"
    echo "MAIL:           $MAIL"
    echo "MAIL USER:      $MAIL_ACC"
    echo "DATABASE:       $DB"
    echo "CRON:           $CRON"
    echo "BACKUP:         $BACKUP"
    echo "IP:             $IP"
    echo "PACKAGE:        $PACKAGE"
    echo "FIREWALL:       $FIREWALL"
}

# PLAIN list function
plain_list() {
    echo -ne "$USER\t$WEB\t$DNS\t$DNS_REC\t$MAIL\t$MAIL_ACC\t$DB\t$CRON\t"
    echo -e "$BACKUP\t$IP\t$PACKAGE\t$FIREWALL"
}

# CSV list function
csv_list() {
    echo -n "USER,WEB,DNS,DNS_REC,MAIL,MAIL_ACC,DB,CRON,BACKUP,IP,"
    echo "PACKAGE,FIREWALL"
    echo -n "\"$USER\",\"$WEB\",\"$DNS\",\"$DNS_REC\",\"$MAIL\","
    echo -n "\"$MAIL_ACC\",\"$DB\",\"$CRON\",\"$BACKUP\",\"$IP\","
    echo "\"$PACKAGE\",\"$FIREWALL\""
}


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

# Checking args
check_args '1' "$#" 'USER [FORMAT]'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"


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

# Flushing vars
USER=''
WEB=''
DNS=''
DNS_REC=''
MAIL=''
MAIL_ACC=''
DB=''
CRON=''
BACKUP=''
IP=''
PACKAGE=''
FIREWALL=''

# Parsing user favourites
if [ -e "$VESTA/data/users/$user/favourites.conf" ]; then
    source $VESTA/data/users/$user/favourites.conf
fi

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


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

exit
