#!/bin/bash
# info: change database password
# options: DATABASE USER
#
# The function for changing database owner.


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

# Argument defenition
database=$1
user=$2

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


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

check_args '2' "$#" 'DATABASE USER'
validate_format 'database' 'user'
is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"

# Check owner existance
owner=$(echo $database | cut -f 1 -d '_')
if [ ! -e "$VESTA/data/users/$owner" ]; then
    echo "Error: database owner doesn't exist"
    log_event "$E_NOTEXIST" "$EVENT"
    exit $E_NOTEXIST
fi

# Check if owner is the same as the dst user
if [ "$owner" = "$user" ]; then
    exit
fi

# Check db existance
db_data=$(grep "DB='$database'" $VESTA/data/users/$owner/db.conf)
if [ -z "$db_data" ]; then
    echo "Error: database $database doesn't exist"
    log_event "$E_NOTEXIST" "$EVENT"
    exit $E_NOTEXIST
fi

# Check if datbase name is uniq
new_db=$(echo $database | sed "s/^${owner}_/${user}_/")
check_db=$(grep "DB='$new_db'" $VESTA/data/users/$user/db.conf)
if [ ! -z "$check_db" ]; then
    echo "Error: $new_db database exists"
    log_event "$E_EXISTS" "$EVENT"
    exit $E_EXISTS
fi


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

# Creating temporary directory
tmpdir=$(mktemp -p $BACKUP -d)
if [ "$?" -ne 0 ]; then
    echo "Error: can't create $tmpdir"
    log_event "$E_NOTEXIST" "$EVENT"
    exit $E_NOTEXIST
fi

# Suspend database
$BIN/v-suspend-database $owner $database > /dev/null 2>&1

# Dump database
eval $db_data
dump="$tmpdir/$database.$TYPE.sql"
grants="$tmpdir/$database.$TYPE.$DBUSER"
send_mail='/bin/true'
case $TYPE in
    mysql) dump_mysql_database ;;
    pgsql) dump_pgsql_database ;;
esac

# Import configuration
db_data=$(echo "$db_data" | sed "s/'${owner}_/'${user}_/g")
echo "$db_data" >> $VESTA/data/users/$user/db.conf
eval $db_data

# Unsuspend db
$BIN/v-unsuspend-database $user $new_db > /dev/null 2>&1

# Rebuild databases
$BIN/v-rebuild-databases $user

# Import dump
case $TYPE in
    mysql) import_mysql_database $dump ;;
    pgsql) import_pgsql_database $dump ;;
esac

# Deleting tmpdir
rm -rf $tmpdir

# Remove old database
$BIN/v-unsuspend-database $owner $database > /dev/null 2>&1
$BIN/v-delete-database $owner $database > /dev/null 2>&1

# Update counters
$BIN/v-update-user-counters $owner
$BIN/v-update-user-counters $user


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

# Logging
log_event "$OK" "$EVENT"

exit
