#!/bin/bash

# settings
MYSQL=mysql

mysql_data_def=verlihub
read -p "mysql database for verlihub will ba called?($mysql_data_def)" mysql_data
[ "_$mysql_data" == "_" ] && mysql_data=$mysql_data_def
echo "mysql database is $mysql_data"

mysql_user_def=verlihub
read -p "mysql user to access $mysql_data gonna be?($mysql_user_def)" mysql_user
[ "_$mysql_user" == "_" ] && mysql_user=$mysql_user_def
echo "mysql user is $mysql_user"

mysql_pass_def=verlihub
read -p "mysql password to access $mysql_data be?($mysql_pass_def)" mysql_pass
[ "_$mysql_pass" == "_" ] && mysql_pass=$mysql_pass_def
echo "mysql pass is $mysql_pass"

mysql_host_def=localhost
read -p "mysql server will run where?($mysql_host_def)" mysql_host
[ "_$mysql_host" == "_" ] && mysql_host=$mysql_host_def
echo "mysql server will be on $mysql_host"

read -p "Do you want to prepare database? (Y/N)" rep
if [ "$rep" == "Y" ] || [ "$rep" == "y" ]; then
	echo Ok preparing mysql..
	echo "You probably need administrator access to mysql database"
	
	mysql_root_def=root
	read -p "mysql administrator username?($mysql_root_def)" mysql_root
	[ "_$mysql_root" == "_" ] && mysql_root=$mysql_root_def
	echo "mysql administrator is $mysql_root"
		
	mysql_this_host_def=localhost
	mysql_this_host=$mysql_this_host_def
	if [ $mysql_host != 'localhost' ]; then
		read -p "Verlihub's server as host from the mysql's scope is?" mysql_this_host
		[ "_$mysql_this_host" == "_" ] && mysql_this_host=$mysql_this_host_def
	fi;

	echo "You'll be now twice promted by mysql client for password of $mysql_root@$mysql_host"
	$MYSQL -h $mysql_host -u $mysql_root -p -e "CREATE DATABASE $mysql_data;" 
	$MYSQL -h $mysql_host -u $mysql_root -p -e "GRANT ALL ON $mysql_data.* to $mysql_user@$mysql_this_host identified by '$mysql_pass'"
fi;

run_dir_def=$HOME
read -p "the directory where you wish to install configuration and from which you want to run hub?($run_dir_def)" run_dir
[ "_$run_dir" == "_" ] && run_dir=$run_dir_def
echo "running directory is $run_dir"

cfg_dir="$run_dir/.verlihub"
echo "configuration directory is $cfg_dir"

if [ -d $cfg_dir ]; then
	
	read -p "Found already existing config dir ($cfg_dir) use it anyway? (Y/N)" rep
	if [ "$rep" == "Y" ] || [ "$rep" == "y" ]; then
		echo "OK.. gonna replace"
	else
		echo "OK, then, restart.."
		exit 1
	fi
else
	mkdir -p $cfg_dir
fi

cfg_dbconfig="$cfg_dir/dbconfig"
echo "db_host= $mysql_host" >  $cfg_dbconfig
echo "db_data= $mysql_data" >> $cfg_dbconfig
echo "db_user= $mysql_user" >> $cfg_dbconfig
echo "db_pass= $mysql_pass" >> $cfg_dbconfig

echo "Written: $cfg_dbconfig"


echo
echo "Db script is a tiny but handy script that helps you easily connect to mysql, without all typing.. It will contain the password for verlihub's db"
read -p "Do you want to prepare the db script? (Y/N)" rep
if [ "$rep" == "Y" ] || [ "$rep" == "y" ]; then

db_script="$cfg_dir/db"
echo "#!/bin/bash" > $db_script
echo "mysql -u $mysql_user -D $mysql_data -p$mysql_pass " >> $db_script
chmod 700 $db_script
echo "Created $db_script"
fi;

#echo "Now I'm moving you to the run direcotory $run_dir"
echo use this command "cd $run_dir"
echo "ok, run verlihub executable and then kill it, it will create it's tables"
echo "then you can use regnick script to create admin user (e.g: regnick -n $USER -p blabla_password -c 10)"
echo "after it you can run verlihub again already using the runhub script, and enjoy it.."
echo done
