#!/bin/bash
# info: list user backups
# options: USER [FORMAT]
#
# The function for obtaining the list of available user backups.


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

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

# Includes
source $VESTA/func/main.sh

# JSON list function
json_list() {
    IFS=$'\n'
    i=1
    objects=$(grep BACKUP $USER_DATA/backup.conf |wc -l)
    echo "{"
    while read str; do
        eval $str
        echo -n '    "'$BACKUP'": {
        "TYPE": "'$TYPE'",
        "SIZE": "'$SIZE'",
        "WEB": "'$WEB'",
        "DNS": "'$DNS'",
        "MAIL": "'$MAIL'",
        "DB": "'$DB'",
        "CRON": "'$CRON'",
        "UDIR": "'$UDIR'",
        "RUNTIME": "'$RUNTIME'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
        if [ "$i" -lt "$objects" ]; then
            echo ','
        else
            echo
        fi
        ((i++))
    done < <(cat $USER_DATA/backup.conf)
    echo '}'
}

# SHELL list function
shell_list() {
    IFS=$'\n'
    echo "BACKUP   TYPE   SIZE  TIME  DATE"
    echo "------   ----   ---   ----  ----"
    while read str; do
        eval $str
        echo "$BACKUP $TYPE $SIZE $RUNTIME $DATE"
    done < <(cat $USER_DATA/backup.conf)
}

# PLAIN list function
plain_list() {
    IFS=$'\n'
    while read str; do
        eval $str
        echo -ne "$BACKUP\t$TYPE\t$SIZE\t$WEB\t$DNS\t$MAIL\t$CRON\t$UDIR\t"
        echo -e "$RUNTIME\t$TIME\t$DATE"
    done < <(cat $USER_DATA/backup.conf)
}

# CSV list function
csv_list() {
    IFS=$'\n'
    echo "BACKUP,TYPE,SIZE,WEB,DNS,MAIL,CRON,UDIR,RUNTIME,TIME,DATE"
    while read str; do
        eval $str
        echo -n "$BACKUP,$TYPE,$SIZE,\"$WEB\",\"$DNS\",\"$MAIL\",\"$CRON\","
        echo "\"$UDIR\",$RUNTIME,$TIME,$DATE"
    done < <(cat $USER_DATA/backup.conf)
}


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

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


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

# Checking config
conf=$USER_DATA/backup.conf
if [ ! -e "$conf" ]; then
    touch $conf
fi

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


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

exit
