#!/bin/bash
# info: list dns templates
# options: [format]
#
# The function for obtaining the list of all DNS templates available.


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

# Argument defenition
format=${1-shell}

# Includes
source $VESTA/func/main.sh

# Json function
json_list_dnstpl() {
    echo '{'
    for template in $(ls $DNSTPL/| grep '.descr'); do
        if [ -n "$data" ]; then
            echo -e '        },'
        fi
        tpl_descr=$(cat $DNSTPL/$template |grep '#'|tr -d '\n')
        tpl_name="${template//.descr/}"
        echo -e  "\t\"$tpl_name\": {"
        echo -e "\t\t\"DESCR\": \"${tpl_descr//# /}\""
        data=1
    done
    if [ -n "$data" ]; then
        echo -e '        }'
    fi
    echo -e '}'
}

# Shell function
shell_list_dnstpl() {
    for template in $(ls $DNSTPL/| grep '.descr'); do
        tpl_name="${template//.descr/}"
        tpl_descr=$(cat $DNSTPL/$template |grep '#')
        if [ -z "$nohead" ] ; then
            echo "----------"
        fi
        echo "TEMPLATE: $tpl_name"
        echo "DESCRIPTION: ${tpl_descr//# /}"
        if [ -z "$nohead" ] ; then
            echo
        fi
    done
}


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

# Listing domains
case $format in 
    json)   json_list_dnstpl;;
    plain)  nohead=1; shell_list_dnstpl ;;
    shell)  shell_list_dnstpl ;;
    *)      check_args '1' '0' '[format] [limit] [offset]';;
esac


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

exit
