#!/bin/bash
echo "Available : mysql mariadb oracle tibero postgresql"
echo -n "what kind of database do you use? : "
        read database

########################### mysql ###################################
case "$database" in 'mysql')
	echo -n "do you wanna create user? y|n : "
	read create

	if [ $create = "y" ]; then
	echo -n "Enter database user name : "
	read user
	echo -e "\033[47;31m Enter mysql root password \033[0m "
	mysql_config_editor set --login-path=root --host=localhost --user=root --password
	mysql --login-path=root -e "create user '$user'@'%' identified by '$user';" --connect-expired-password
	mysql --login-path=root -e " grant all privileges on $user.* to $user@'%' identified by '$user';" --connect-expired-password
mysql --login-path=root -e "create database $user default character set utf8;" --connect-expired-password
	mysql --login-path=root -e "FLUSH PRIVILEGES;" --connect-expired-password	
	echo -e "\033[47;31m user, pw, db명 모두 user명과 같습니다. \033[0m"
	else
		echo "user 생성하지 않습니다"
	fi

	echo -n "do you wanna excute sql query? y|n : "
	read excute
	
	if [ $excute = "y" ]; then
	echo -n "Enter where is the sql? ex) /root/script : "
	read where
	echo -n "Enter sql name? ex)test.sql : "
	read sql
	echo -e "\033[47;31m Enter ↑ just created user password \033[0m "
	
	#new user password save
	mysql_config_editor set --login-path=$user --host=localhost --user=$user --password
	#sql excute
	mysql --login-path=$user $user < $where/$sql
	#db table list
	#mysql --login-path=$user -e "select * from information_schema.`TABLES` where TABLE_SCHEMA = '$user';" --connect-expired-password
	else
		echo "sql문 실행하지않습니다."
	fi

	;;

'mariadb')
	echo -n "not read"
	;;
############################# other dbs ################################

*)
	echo "not ready" ;;
esac
	
