#!/bin/bash
# info: list backup exclusions
# options: USER [FORMAT]
#
# The function for obtaining the backup exclusion list


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

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

# Includes
source $VESTA/func/main.sh

# JSON list function
json_list() {
    set -f
    i=1
    echo '{'
    echo '    "WEB": {'
    objects=$(echo "${WEB//,/ }" |wc -w)
    for object in $(echo "${WEB//,/ }"); do
        j=1
        object_keys=$(echo ${object//:/ } |wc -w)
        for key in $(echo "${object/:/ }"); do
            if [ "$j" -eq 1 ]; then
                echo -n "        \"${key}\": "
                if [ "$object_keys" -eq 1 ]; then
                    echo -n '""'
                fi
            else
                echo -n "\"${key//:/,}\""
            fi
            ((j++))
        done
        if [ "$i" -lt "$objects" ]; then
            echo ','
        else
            echo
        fi
        ((i++))
    done
    echo '    },'
    i=1
    echo '    "MAIL": {'
    objects=$(echo "${MAIL//,/ }" |wc -w)
    for object in $(echo "${MAIL//,/ }"); do
        j=1
        object_keys=$(echo ${object//:/ } |wc -w)
        for key in $(echo "${object/:/ }"); do
            if [ "$j" -eq 1 ]; then
                echo -n "        \"$key\": "
                if [ "$object_keys" -eq 1 ]; then
                    echo -n '""'
                fi
            else
                echo -n "\"${key//:/,}\""
            fi
            ((j++))
        done
        if [ "$i" -lt "$objects" ]; then
            echo ','
        else
            echo
        fi
        ((i++))
    done
    echo '    },'
    i=1
    echo '    "DB": {'
    objects=$(echo "${DB//,/ }" |wc -w)
    for object in $(echo "${DB//,/ }"); do
        j=1
        object_keys=$(echo ${object//:/ } |wc -w)
        for key in $(echo "${object/:/ }"); do
            if [ "$j" -eq 1 ]; then
                echo -n "        \"$key\": "
                if [ "$object_keys" -eq 1 ]; then
                    echo -n '""'
                fi
            else
                echo -n "\"${key//:/,}\""
            fi
            ((j++))
        done
        if [ "$i" -lt "$objects" ]; then
            echo ','
        else
            echo
        fi
        ((i++))
    done
    echo '    },'
    i=1
    echo '    "USER": {'
    objects=$(echo "${USER//,/ }" |wc -w)
    for object in $(echo "${USER//,/ }"); do
        j=1
        object_keys=$(echo ${object//:/ } |wc -w)
        for key in $(echo "${object/:/ }"); do
            if [ "$j" -eq 1 ]; then
                echo -n "        \"$key\": "
                if [ "$object_keys" -eq 1 ]; then
                    echo -n '""'
                fi
            else
                echo -n "\"${key//:/,}\""
            fi
            ((j++))
        done
        if [ "$i" -lt "$objects" ]; then
            echo ','
        else
            echo
        fi
        ((i++))
    done
    echo '    }'
    echo '}'

}

# SHELL list function
shell_list() {
    echo "WEB:            $WEB"
    echo "MAIL:           $MAIL"
    echo "DB:             $DB"
    echo "USER DIRS:      $USER"
}

# PLAIN list function
plain_list() {
    echo  "$WEB\t$MAIL\t$DB\t$USER"
}

# CSV list function
csv_list() {
    echo "WEB,MAIL,DB,USER"
    echo "\"$WEB\",\"$MAIL\",\"$DB\",\"$USER\""
}


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

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


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

# Flushing variables
WEB=''
MAIL=''
DB=''
USER=''

# Parsing backup exclusion list
if [ -e "$USER_DATA/backup-excludes.conf" ]; then
    source $USER_DATA/backup-excludes.conf
fi

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


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

exit
