SET @@SESSION.SQL_LOG_BIN=0;

USE mysql;
DROP FUNCTION IF EXISTS sphinx_snippets;
#CREATE FUNCTION sphinx_snippets RETURNS STRING SONAME 'ha_sphinx.so';

CREATE DATABASE IF NOT EXISTS `repman_slave_definition`;

{% macro database(name, user, password, ssl, charset="") -%}
{% set charset_collate = "" -%}
{% set charset_dict = {'utf8_general_ci': 'utf8',
                       'utf8_unicode_ci': 'utf8',
                       'utf8mb4_bin': 'utf8mb4',
                       'utf8mb4_general_ci': 'utf8mb4',
                       'utf8mb4_unicode_ci': 'utf8mb4',
                       'latin1_general_ci': 'latin1',
                       'latin1_bin': 'latin1',
                       'ascii_general_ci': 'ascii',
                       'ascii_bin': 'ascii'} -%}
{% if charset %}
{%   set charset_collate = "DEFAULT CHARACTER SET %s" % charset -%}
{%   if charset_dict.get(charset) -%}
{%     set charset_collate = "DEFAULT CHARACTER SET %s COLLATE %s" % (charset_dict[charset], charset) -%}
{%   endif -%}
{% endif -%}
CREATE DATABASE IF NOT EXISTS `{{ name }}` {{ charset_collate }};
{% if user -%}
GRANT ALL PRIVILEGES ON `{{ name }}`.* TO `{{ user }}`@`%` IDENTIFIED BY '{{ password }}'{% if ssl %} REQUIRE SSL{% endif %};
GRANT ALL PRIVILEGES ON `{{ name }}`.* TO `{{ user }}`@localhost IDENTIFIED BY '{{ password }}';
GRANT ALL PRIVILEGES ON `{{ name }}`.* TO `{{ user }}`@'::' IDENTIFIED BY '{{ password }}';
{%- endif %}
{% endmacro -%}

{% for entry in parameter_dict['database-list'] -%}
{{   database(entry['name'], entry.get('user'), entry.get('password'), entry.get('require_ssl', True) ~ '', entry.get('charset')) }}
{% endfor -%}
