#!/bin/bash
# info: rebuild databases
# options: USER
#
# The function for rebuilding of all databases of a single user.


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

# Argument defenition
user=$1

# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/main.sh
source $VESTA/func/db.sh


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

check_args '1' "$#" 'USER'
validate_format 'user'
is_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"


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

# Starting rebuild loop
for database in $(search_objects 'db' 'SUSPENDED' "no" 'DB'); do
    get_database_values
    # Switching on db type
    case $TYPE in
        mysql) rebuild_mysql_database ;;
        pgsql) rebuild_pgsql_database ;;
    esac
done


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

# Logging
log_event "$OK" "$EVENT"

exit
