#!/bin/sh

. /usr/lib/lib-ymc/ymc-mysql-tools.sh || exit 1

ymc_mysql_user="root"
if [ $(echo "$*" | grep -c '\--no-interaction') -eq 0 ]; then
  no_interaction='no'
else
  no_interaction='yes'
fi

ymc_mysql_tools_get_credentials || exit 1
addToMySQL "SET sql_log_bin = 0; \
            GRANT ALL PRIVILEGES \
               ON *.* \
               TO 'ymcTmpUserInsert'@'127.0.0.1' \
               WITH GRANT OPTION \
                    MAX_QUERIES_PER_HOUR 0 \
                    MAX_UPDATES_PER_HOUR 0 \
                    MAX_CONNECTIONS_PER_HOUR 0 \
                    MAX_USER_CONNECTIONS 0;"
if [ $? -ne 0 ]; then
  ymc_error_msg "Failed adding users - go and check the rights of user 'root'!"
fi
ymc_mysql_user="ymcTmpUserInsert"
ymc_mysql_pw_option=""
ymc_mysql_host="127.0.0.1"
root_accounts_deleted=0
anonymous_accounts_deleted=0

for user_raw_info in $(cat $(get_replication_information_dir)/simple.users | grep -v '^#' | grep -v -E -e '^[[:blank:]]*$' | sed -r 's/[ ]+/-,-,-,-/g')
do
  user_info=$(echo $user_raw_info | sed -r 's/-,-,-,-/ /g')
  # USER PASSWORD DATABASE HOST Queries/h Updates/h Connect/h MaxConnect GRANT PERMISSIONS
  mysql_user=$(echo "$user_info" | cut -d ' ' -f 1)
  mysql_pass=$(echo "$user_info" | cut -d ' ' -f 2)
  mysql_database=$(echo "$user_info" | cut -d ' ' -f 3)
  mysql_host=$(echo "$user_info" | cut -d ' ' -f 4)
  mysql_max_queries_per_hour=$(echo "$user_info" | cut -d ' ' -f 5)
  mysql_max_updates_per_hour=$(echo "$user_info" | cut -d ' ' -f 6)
  mysql_max_connections_per_hour=$(echo "$user_info" | cut -d ' ' -f 7)
  mysql_max_parallel_connections=$(echo "$user_info" | cut -d ' ' -f 8)
  mysql_grant=$(echo "$user_info" | cut -d ' ' -f 9)
  mysql_permissions=$(echo "$user_info" | cut -d ' ' -f 10-)

  if [ "$mysql_grant" = "yes" ]; then
    mysql_grant_option_sql="GRANT OPTION"
  else
    mysql_grant_option_sql=""
  fi

  if [ "$mysql_pass" = "_EMPTY_" ]; then
    mysql_pass_option_sql=""
  else
    mysql_pass_option_sql="IDENTIFIED BY '$mysql_pass'"
  fi


  #echo "-------------"
  #echo "mysql_user: '$mysql_user'"
  #echo "mysql_pass: '$mysql_pass'"
  #echo "mysql_database: '$mysql_database'"
  #echo "mysql_host: '$mysql_host'"
  #echo "mysql_max_queries_per_hour: '$mysql_max_queries_per_hour'"
  #echo "mysql_max_updates_per_hour: '$mysql_max_updates_per_hour'"
  #echo "mysql_max_connections_per_hour: '$mysql_max_connections_per_hour'"
  #echo "mysql_max_parallel_connections: '$mysql_max_parallel_connections'"
  #echo "mysql_grant: '$mysql_grant'"
  #echo "mysql_permissions: '$mysql_permissions'"

  if [ $anonymous_accounts_deleted -eq 0 ]; then
    addToMySQL "SET sql_log_bin = 0; DELETE FROM mysql.user WHERE User='';"
    addToMySQL "SET sql_log_bin = 0; DELETE FROM mysql.db WHERE User='';"
    addToMySQL "SET sql_log_bin = 0; FLUSH PRIVILEGES;"
    anonymous_accounts_deleted=1
  fi

  if [ "$mysql_user" = "root" ] &&
     [ $root_accounts_deleted -eq 0 ]; then
    addToMySQL "SET sql_log_bin = 0; DROP USER 'root'@'localhost';"
    addToMySQL "SET sql_log_bin = 0; DROP USER 'root'@'127.0.0.1';"
    addToMySQL "SET sql_log_bin = 0; DROP USER 'root'@'$(hostname)';"
    root_accounts_deleted=1
  fi

  addToMySQL "SET sql_log_bin = 0; \
              GRANT $mysql_permissions \
                 ON $mysql_database \
                 TO '$mysql_user'@'$mysql_host' $mysql_pass_option_sql \
                 WITH $mysql_grant_option_sql \
                      MAX_QUERIES_PER_HOUR $mysql_max_queries_per_hour \
                      MAX_UPDATES_PER_HOUR $mysql_max_updates_per_hour \
                      MAX_CONNECTIONS_PER_HOUR $mysql_max_connections_per_hour \
                      MAX_USER_CONNECTIONS $mysql_max_parallel_connections;"
  if [ $? -ne 0 ]; then
    ymc_error_msg "Failed adding user '$mysql_user@$mysql_host' for database '$mysql_database'!"
  else
    ymc_info_msg "Adding user '$mysql_user@$mysql_host' for database '$mysql_database':\
              \n# Queries/Updates/Connections per hour: $mysql_max_queries_per_hour/$mysql_max_updates_per_hour/$mysql_max_connections_per_hour\
              \n# Maximum parallel connections: $mysql_max_parallel_connections\
              \n# Permissions: $mysql_permissions\
              \n# GRANT allowed: $mysql_grant"
  fi
done


addToMySQL "SET sql_log_bin = 0; DROP USER 'ymcTmpUserInsert'@'127.0.0.1';"

