#!/bin/bash
# info: delete database server
# options: type host
#
# The function for deleting the database host from vesta configuration. It will
# be deleted if there are no databases created on it only.


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

# Argument defenition
type=$1
host=$2

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


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

check_args '2' "$#" "type host"
validate_format 'type' 'host'
is_system_enabled "$DB_SYSTEM"
is_type_valid "$DB_SYSTEM" "$type"
is_object_valid "../../conf/$type" 'HOST' "$host"
is_dbhost_free


#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#
# Deleting server
sed -i "/HOST='$host' /d" $VESTA/conf/$type.conf


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

# Logging
log_event "$OK" "$EVENT"

exit
