function if_error
{
	if [[ $? -ne 0 ]]; then # check return code passed to function
		echo "$1" # if rc > 0 then print error msg and quit
		exit $?
	fi
}

clear

cd ~

echo "--- Creating a database and database user for FunnelFlux"
echo " "
read -p "Enter a name for your DB (only letters, numbers and _): " FF_DB
read -p "Enter a name for the DB user (only letters, numbers and _): " FF_DB_USER
read -p "Enter a password for the DB user: " FF_DB_PWD
cmd="mysql -e \"create database "$FF_DB"; GRANT ALL PRIVILEGES ON "$FF_DB".* TO "$FF_DB_USER"@localhost IDENTIFIED BY '"$FF_DB_PWD"'\""
eval $cmd
echo "host=localhost"    > /usr/share/nginx/html/fluxdb.ini
echo "db="$FF_DB        >> /usr/share/nginx/html/fluxdb.ini
echo "user="$FF_DB_USER >> /usr/share/nginx/html/fluxdb.ini
echo "pwd="$FF_DB_PWD   >> /usr/share/nginx/html/fluxdb.ini
if_error "Cannot create database - make sure that your database name and user name do NOT start with a number and are valid names"

clear
echo "----------------------------------------------------"
echo "--- The database for FunnelFlux is now ready"
echo "--- Please run the 2 following commands one by one:"
echo " "
echo "wget https://assets.funnelflux.com/tools/confs/clickhouse/ff3.sh"
echo "sh ff3.sh"
echo " "
