sub require_mysql
{
return if ($require_mysql++);
$mysql::use_global_login = 1;
&foreign_require("mysql", "mysql-lib.pl");
%mconfig = &foreign_config("mysql");
$password_func = $mysql::password_func || "password";
}

# check_depends_mysql(&dom)
# Ensure that a sub-server has a parent server with MySQL enabled
sub check_depends_mysql
{
return undef if (!$_[0]->{'parent'});
local $parent = &get_domain($_[0]->{'parent'});
return $text{'setup_edepmysql'} if (!$parent->{'mysql'});
return undef;
}

# setup_mysql(&domain, [no-db])
# Create a new MySQL database, user and permissions
sub setup_mysql
{
local $tmpl = &get_template($_[0]->{'template'});
&require_mysql();

# Create the user
$_[0]->{'mysql_user'} = &mysql_user($_[0]);
local $user = $_[0]->{'mysql_user'};
local @hosts = &get_mysql_hosts($_[0]);
local $wild = &substitute_domain_template($tmpl->{'mysql_wild'}, $_[0]);
if (!$_[0]->{'parent'}) {
	&$first_print($text{'setup_mysqluser'});
	my $pass = &mysql_pass($_[0]);
	local $qpass = &mysql_escape($pass);
	local $h;
	foreach $h (@hosts) {
		&mysql::execute_sql_logged($mysql::master_db, "insert into user (host, user, password) values ('$h', '$user', $password_func('$qpass'))");
		if ($wild && $wild ne $_[0]->{'db'}) {
			&add_db_table($h, $wild, $user);
			}
		}
	&$second_print($text{'setup_done'});
	}
&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');

# Create the initial DB (if requested)
if (!$_[1] && $tmpl->{'mysql_mkdb'} && !$_[0]->{'no_mysql_db'}) {
	&create_mysql_database($_[0], $_[0]->{'db'});
	}
else {
	# No DBs can exist
	$_[0]->{'db_mysql'} = "";
	}
}

# add_db_table(host, db, user)
# Adds an entry to the db table, with all permission columns set to Y
sub add_db_table
{
local @str = &mysql::table_structure($mysql::master_db, 'db');
local ($s, @fields, @yeses);
foreach $s (@str) {
	if ($s->{'field'} =~ /_priv$/i) {
		push(@fields, $s->{'field'});
		push(@yeses, "'Y'");
		}
	}
local $qdb = &quote_mysql_database($_[1]);
&mysql::execute_sql_logged($mysql::master_db, "insert into db (host, db, user, ".join(", ", @fields).") values ('$_[0]', '$qdb', '$_[2]', ".join(", ", @yeses).")");
}

# delete_mysql(&domain)
# Delete a mysql database, the domain's mysql user and all permissions for both
sub delete_mysql
{
# First remove the databases
&require_mysql();
if ($_[0]->{'db_mysql'}) {
	&delete_mysql_database($_[0], &unique(split(/\s+/, $_[0]->{'db_mysql'})));
	}

# Then remove the user
local $user = &mysql_user($_[0]);
local $tmpl = &get_template($_[0]->{'template'});
local $wild = &substitute_domain_template($tmpl->{'mysql_wild'}, $_[0]);
if (!$_[0]->{'parent'}) {
	# Delete the user and any database permissions
	&$first_print($text{'delete_mysqluser'});
	&mysql::execute_sql_logged($mysql::master_db, "delete from user where user = '$user'");
	&mysql::execute_sql_logged($mysql::master_db, "delete from db where user = '$user'");
	&$second_print($text{'setup_done'});
	}
if ($wild && $wild ne $_[0]->{'db'}) {
	# Remove any wildcard entry for the user
	&mysql::execute_sql_logged($mysql::master_db, "delete from db where db = '$wild'");
	}
&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');
}

# modify_mysql(&domain, &olddomain)
# Changes the mysql user's password if needed
sub modify_mysql
{
&require_mysql();
local $rv = 0;
local $changeduser = $_[0]->{'user'} eq $_[1]->{'user'} ? 0 : 1;
local $olduser = &mysql_user($_[1]);
local $user = &mysql_user($_[0], $changeduser);
$_[0]->{'mysql_user'} = $user;
my $oldpass = &mysql_pass($_[1]);
my $pass = &mysql_pass($_[0]);
local $tmpl = &get_template($_[0]->{'id'});
if ($pass ne $oldpass && !$_[0]->{'parent'} && !$tmpl->{'mysql_nopass'}) {
	# Change MySQL password
	&$first_print($text{'save_mysqlpass'});
	if (&mysql_user_exists($_[0])) {
		local $qpass = &mysql_escape($pass);
		&mysql::execute_sql_logged($mysql::master_db, "update user set password = $password_func('$qpass') where user = '$olduser'");
		&mysql::execute_sql_logged($master_db, 'flush privileges');
		&$second_print($text{'setup_done'});
		$rv++;
		}
	else {
		&$second_print($text{'save_nomysql'});
		}
	}
if (!$_[0]->{'parent'} && $_[1]->{'parent'}) {
	# Server has been converted to a parent .. need to create user, and
	# change access to old DBs
	&$first_print($text{'setup_mysqluser'});
	$_[0]->{'mysql_user'} = &mysql_user($_[0], 1);
	local $user = $_[0]->{'mysql_user'};
	local @hosts = &get_mysql_hosts($_[0]);
	local $wild = &substitute_domain_template($tmpl->{'mysql_wild'}, $_[0]);
	my $pass = &mysql_pass($_[0]);
	local $qpass = &mysql_escape($pass);
	local $h;
	foreach $h (@hosts) {
		&mysql::execute_sql_logged($mysql::master_db, "insert into user (host, user, password) values ('$h', '$user', $password_func('$qpass'))");
		if ($wild && $wild ne $_[0]->{'db'}) {
			&add_db_table($h, $wild, $user);
			}
		}
	foreach my $db (&domain_databases($_[0], [ "mysql" ])) {
		local $qdb = &quote_mysql_database($db->{'name'});
		&mysql::execute_sql_logged($mysql::master_db, "update db set user = '$user' where user = '$olduser' and (db = '$db->{'name'}' or db = '$qdb')");
		}
	&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');
	&$second_print($text{'setup_done'});
	}
elsif ($user ne $olduser && !$_[0]->{'parent'}) {
	# MySQL user in a parent domain has changed, perhaps due to username
	# change. Need to update user in DB and all db entries
	&$first_print($text{'save_mysqluser'});
	if (&mysql_user_exists($_[1])) {
		&mysql::execute_sql_logged($mysql::master_db, "update user set user = '$user' where user = '$olduser'");
		&mysql::execute_sql_logged($mysql::master_db, "update db set user = '$user' where user = '$olduser'");
		&mysql::execute_sql_logged($master_db, 'flush privileges');
		&$second_print($text{'setup_done'});
		$rv++;
		}
	else {
		&$second_print($text{'save_nomysql'});
		}
	}
elsif ($user ne $olduser && $_[0]->{'parent'}) {
	# Server has moved to a new user .. change ownership of DBs
	&$first_print($text{'save_mysqluser2'});
	foreach my $db (&domain_databases($_[0], [ "mysql" ])) {
		local $qdb = &quote_mysql_database($db->{'name'});
		&mysql::execute_sql_logged($mysql::master_db, "update db set user = '$user' where user = '$olduser' and (db = '$db->{'name'}' or db = '$qdb')");
		}
	$rv++;
	&mysql::execute_sql_logged($master_db, 'flush privileges');
	&$second_print($text{'setup_done'});
	}
return $rv;
}

# validate_mysql(&domain)
# Make sure all MySQL databases exist
sub validate_mysql
{
local ($d) = @_;
&require_mysql();
local %got = map { $_, 1 } &mysql::list_databases();
foreach my $db (&domain_databases($d, [ "mysql" ])) {
	$got{$db->{'name'}} || return &text('validate_emysql', $db->{'name'});
	}
return undef;
}

# disable_mysql(&domain)
# Modifies the mysql user for this domain so that he cannot login
sub disable_mysql
{
&$first_print($text{'disable_mysqluser'});
if ($_[0]->{'parent'}) {
	&$second_print($text{'save_nomysqlpar'});
	}
else {
	&require_mysql();
	local $user = &mysql_user($_[0]);
	if ($oldpass = &mysql_user_exists($_[0])) {
		&mysql::execute_sql_logged($mysql::master_db, "update user set password = '*LK*' where user = '$user'");
		&mysql::execute_sql_logged($master_db, 'flush privileges');
		$_[0]->{'disabled_oldmysql'} = $oldpass;
		&$second_print($text{'setup_done'});
		}
	else {
		&$second_print($text{'save_nomysql'});
		}
	}
}

# enable_mysql(&domain)
# Puts back the original password for the mysql user so that he can login again
sub enable_mysql
{
&$first_print($text{'enable_mysql'});
if ($_[0]->{'parent'}) {
	&$second_print($text{'save_nomysqlpar'});
	}
else {
	&require_mysql();
	local $user = &mysql_user($_[0]);
	if (&mysql_user_exists($_[0])) {
		if ($_[0]->{'disabled_oldmysql'}) {
			local $qpass = &mysql_escape(
				$_[0]->{'disabled_oldmysql'});
			&mysql::execute_sql_logged($mysql::master_db, "update user set password = '$qpass' where user = '$user'");
			delete($_[0]->{'disabled_oldmysql'});
			}
		else {
			local $pass = &mysql_pass($_[0]);
			local $qpass = &mysql_escape($pass);
			&mysql::execute_sql_logged($mysql::master_db, "update user set password = $password_func('$qpass') where user = '$user'");
			}
		&mysql::execute_sql($master_db, 'flush privileges');
		&$second_print($text{'setup_done'});
		}
	else {
		&$second_print($text{'save_nomysql'});
		}
	}
}

# mysql_user_exists(&domain)
# Returns his password if a mysql user exists for the domain's user, or undef
sub mysql_user_exists
{
&require_mysql();
local $user = &mysql_user($_[0]);
local $u = &mysql::execute_sql($mysql::master_db, "select password from user where user = '$user'");
return @{$u->{'data'}} ? $u->{'data'}->[0]->[0] : undef;
}

# check_mysql_clash(&domain, [field])
# Returns 1 if some MySQL database already exists
sub check_mysql_clash
{
if (!$_[1] || $_[1] eq 'db') {
	&require_mysql();
	local @dblist = &mysql::list_databases();
	return 1 if (&indexof($_[0]->{'db'}, @dblist) >= 0);
	}
if (!$_[0]->{'parent'} && (!$_[1] || $_[1] eq 'user')) {
	&require_mysql();
	return 1 if (&mysql_user_exists($_[0]));
	}
return 0;
}

# backup_mysql(&domain, file)
# Dumps this domain's mysql database to a backup file
sub backup_mysql
{
&require_mysql();

# Find all domain's databases
local $tmpl = &get_template($_[0]->{'template'});
local $wild = &substitute_domain_template($tmpl->{'mysql_wild'}, $_[0]);
local @alldbs = &mysql::list_databases();
local @dbs;
if ($wild) {
	$wild =~ s/\%/\.\*/g;
	$wild =~ s/_/\./g;
	@dbs = grep { /^$wild$/i } @alldbs;
	}
push(@dbs, split(/\s+/, $_[0]->{'db_mysql'}));
@dbs = &unique(@dbs);

# Create empty 'base' backup file
&open_tempfile(EMPTY, ">$_[1]");
&close_tempfile(EMPTY);

# Back them all up
local $db;
foreach $db (@dbs) {
	&$first_print(&text('backup_mysqldump', $db));
	local $dbfile = $_[1]."_".$db;
	local $cmd = "$mysql::config{'mysqldump'} $mysql::authstr --add-drop-table".
		     (&mysql::supports_quoting() ? " --quote-names" : "").
		     " ".quotemeta($db)." 2>&1 >".quotemeta($dbfile);
	local $out;
	&execute_command($cmd, undef, \$out);
	if ($? || $out) {
		&$second_print(&text('backup_mysqldumpfailed', "<pre>$out</pre>"));
		return 0;
		}
	else {
		&$second_print($text{'setup_done'});
		}
	}
return 1;
}

# restore_mysql(&domain, file)
# Restores this domain's mysql database from a backup file, and re-creates
# the mysql user.
sub restore_mysql
{
&$first_print($text{'restore_mysqldrop'});
	{
	local $first_print = \&null_print;	# supress messages
	local $second_print = \&null_print;
	&require_mysql();

	# First clear out all current databases and the MySQL login
	&delete_mysql($_[0]);

	# Now re-set up the login only
	&setup_mysql($_[0], 1);
	}
&$second_print($text{'setup_done'});

# Work out which databases are in backup
local ($dbfile, @dbs);
push(@dbs, [ $_[0]->{'db'}, $_[1] ]) if (-s $_[1]);
foreach $dbfile (glob("$_[1]_*")) {
	if (-r $dbfile) {
		$dbfile =~ /\Q$_[1]\E_(.*)$/;
		push(@dbs, [ $1, $dbfile ]);
		}
	}

# Finally, import the data
local $db;
foreach $db (@dbs) {
	&create_mysql_database($_[0], $db->[0]);
	&$first_print(&text('restore_mysqlload', $db->[0]));
	local $cmd = "$mysql::config{'mysql'} $mysql::authstr -t ".quotemeta($db->[0])." <".quotemeta($db->[1])." 2>&1";
	local $out = &backquote_logged($cmd);
	if ($? || $out) {
		&$second_print(&text('restore_mysqlloadfailed', "<pre>$out</pre>"));
		return 0;
		}
	else {
		&$second_print($text{'setup_done'});
		}
	}
return 1;
}

# mysql_user(&domain, [always-new])
# Returns the MySQL login name for a domain
sub mysql_user
{
if ($_[0]->{'parent'}) {
	# Get from parent domain
	return &mysql_user(&get_domain($_[0]->{'parent'}), $_[1]);
	}
return $_[0]->{'mysql_user'} if (defined($_[0]->{'mysql_user'}) && !$_[1]);
return length($_[0]->{'user'}) > 16 ?
	  substr($_[0]->{'user'}, 0, 16) : $_[0]->{'user'};
}

# set_mysql_user(&domain, newuser)
# Updates a domain object with a new MySQL username
sub set_mysql_user
{
$_[0]->{'mysql_user'} = length($_[1]) > 16 ? substr($_[1], 0, 16) : $_[1];
}

# mysql_username(username)
# Adjusts a username to be suitable for MySQL
sub mysql_username
{
return length($_[0]) > 16 ? substr($_[0], 0, 16) : $_[0];
}

# mysql_pass(&domain)
sub mysql_pass
{
return defined($_[0]->{'mysql_pass'}) ? $_[0]->{'mysql_pass'} : $_[0]->{'pass'};
}

# mysql_escape(string)
# Returns a string with quotes escaped, for use in SQL
sub mysql_escape
{
local $rv = $_[0];
$rv =~ s/'/''/g;
return $rv;
}

# mysql_size(&domain, dbname, [size-only])
# Returns the size and number of tables in a database
sub mysql_size
{
&require_mysql();
local $size;
if (-d $mysql::config{'mysql_data'}) {
	$size = &disk_usage_kb("$mysql::config{'mysql_data'}/$_[1]")*1024;
	}
local @tables = $_[2] ? () : &mysql::list_tables($_[1], 1);
return ($size, scalar(@tables));
}

# check_mysql_database_clash(&domain, dbname)
# Check if some MySQL database already exists
sub check_mysql_database_clash
{
&require_mysql();
local @dblist = &mysql::list_databases();
return 1 if (&indexof($_[1], @dblist) >= 0);
}

# create_mysql_database(&domain, dbname)
# Add one database to this domain, and grants access to it to the user
sub create_mysql_database
{
# Create the database
&$first_print(&text('setup_mysqldb', $_[1]));
local @dbs = split(/\s+/, $_[0]->{'db_mysql'});
&mysql::execute_sql_logged($mysql::master_db, "create database $_[1]");
push(@dbs, $_[1]);
$_[0]->{'db_mysql'} = join(" ", @dbs);

# Add db entries for the user for each host
local $tmpl = &get_template($_[0]->{'template'});
local $h;
local @hosts = &get_mysql_hosts($_[0]);
local $user = &mysql_user($_[0]);
foreach $h (@hosts) {
	&add_db_table($h, $_[1], $user);
	}
&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');
&$second_print($text{'setup_done'});
}

# delete_mysql_database(&domain, dbname, ...)
# Remove one or more MySQL database from this domain
sub delete_mysql_database
{
&require_mysql();
local @dblist = &mysql::list_databases();
&$first_print(&text('delete_mysqldb', join(", ", @_[1..$#_])));
local @dbs = split(/\s+/, $_[0]->{'db_mysql'});
local @missing;
foreach my $db (@_[1..$#_]) {
	local $qdb = &quote_mysql_database($db);
	if (&indexof($db, @dblist) >= 0) {
		# Drop the DB
		&mysql::execute_sql_logged($mysql::master_db, "drop database $db");

		# Remove any db permissions for it
		&mysql::execute_sql_logged($mysql::master_db, "delete from db where db = '$db' or db = '$qdb'");
		}
	else {
		push(@missing, $db);
		}
	@dbs = grep { $_ ne $db } @dbs;
	}
$_[0]->{'db_mysql'} = join(" ", @dbs);
&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');
if (@missing) {
	&$second_print(&text('delete_mysqlmissing', join(", ", @missing)));
	}
else {
	&$second_print($text{'setup_done'});
	}
}

# get_mysql_hosts(&domain)
# Returns the allowed MySQL hosts for some domain
sub get_mysql_hosts
{
local $tmpl = &get_template($_[0]->{'template'});
local @hosts = $tmpl->{'mysql_hosts'} eq "none" ? ( ) :
    split(/\s+/, &substitute_domain_template($tmpl->{'mysql_hosts'}, $_[0]));
@hosts = ( 'localhost' ) if (!@hosts);
return @hosts;
}

# list_mysql_database_users(&domain, db)
# Returns a list of MySQL users and passwords who can access some database
sub list_mysql_database_users
{
local ($d, $db) = @_;
&require_mysql();
local $qdb = &quote_mysql_database($db);
local $d = &mysql::execute_sql($mysql::master_db, "select user.user,user.password from user,db where db.user = user.user and (db.db = '$db' or db.db = '$qdb')");
local (@rv, %done);
foreach my $u (@{$d->{'data'}}) {
	push(@rv, $u) if (!$done{$u->[0]}++);
	}
return @rv;
}

# list_all_mysql_users()
# Returns a list of all MySQL usernames
sub list_all_mysql_users
{
&require_mysql();
local $d = &mysql::execute_sql($mysql::master_db, "select user from user");
return &unique(map { $_->[0] } @{$d->{'data'}});
}

# create_mysql_database_user(&domain, &dbs, username, password, [mysql-pass])
# Adds one mysql user, who can access multiple databases
sub create_mysql_database_user
{
local ($d, $dbs, $user, $pass, $encpass) = @_;
&require_mysql();
local $myuser = &mysql_username($user);
local @hosts = &get_mysql_hosts($d);
local $qpass = &mysql_escape($pass);
local $h;
foreach $h (@hosts) {
	&mysql::execute_sql_logged($mysql::master_db, "insert into user (host, user, password) values ('$h', '$myuser', ".($encpass ? "'$encpass'" : "$password_func('$qpass')").")");
	local $db;
	foreach $db (@$dbs) {
		&add_db_table($h, $db, $myuser);
		}
	}
&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');
}

# delete_mysql_database_user(&domain, username)
# Removes one database user and his access to all databases
sub delete_mysql_database_user
{
local ($d, $user) = @_;
&require_mysql();
local $myuser = &mysql_username($user);
&mysql::execute_sql_logged($mysql::master_db, "delete from user where user = '$myuser'");
&mysql::execute_sql_logged($mysql::master_db, "delete from db where user = '$myuser'");
&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');
}

# modify_mysql_database_user(&domain, &olddbs, &dbs, oldusername, username,
#			     [password])
# Renames or changes the password for a database user, and his list of allowed
# mysql databases
sub modify_mysql_database_user
{
local ($d, $olddbs, $dbs, $olduser, $user, $pass) = @_;
&require_mysql();
local $myuser = &mysql_username($user);
if ($olduser ne $user) {
	# Change the username
	local $myolduser = &mysql_username($olduser);
	&mysql::execute_sql_logged($mysql::master_db, "update user set user = '$myuser' where user = '$myolduser'");
	&mysql::execute_sql_logged($mysql::master_db, "update db set user = '$myuser' where user = '$myolduser'");
	}
if (defined($pass)) {
	# Change the password
	local $qpass = &mysql_escape($pass);
	&mysql::execute_sql_logged($mysql::master_db, "update user set password = $password_func('$qpass') where user = '$myuser'");
	}
if (join(" ", @$dbs) ne join(" ", @$olddbs)) {
	# Update accessible database list
	local @hosts = &get_mysql_hosts($d);
	&mysql::execute_sql_logged($mysql::master_db, "delete from db where user = '$myuser'");
	local $h;
	foreach $h (@hosts) {
		local $db;
		foreach $db (@$dbs) {
			&add_db_table($h, $db, $myuser);
			}
		}
	}
&mysql::execute_sql_logged($mysql::master_db, 'flush privileges');
}

# list_mysql_tables(database)
# Returns a list of tables in the given database
sub list_mysql_tables
{
&require_mysql();
return &mysql::list_tables($_[0], 1);
}

# get_database_host_mysql()
# Returns the hostname of the server on which MySQL is actually running
sub get_database_host_mysql
{
&require_mysql();
return $mysql::config{'host'} || 'localhost';
}

# sysinfo_mysql()
# Returns the MySQL version
sub sysinfo_mysql
{
&require_mysql();
local $ver = &mysql::get_mysql_version();
return ( [ $text{'sysinfo_mysql'}, $ver ] );
}

sub links_mysql
{
local ($d) = @_;
if ($config{'avail_mysql'}) {
	local @dbs = split(/\s+/, $d->{'db_mysql'});
	if (@dbs == 1) {
		return ( { 'mod' => 'mysql',
			   'desc' => $text{'links_mysql'},
			   'page' => "edit_dbase.cgi?db=$dbs[0]" } );
		}
	#elsif (@dbs > 1) {
	#	return ( { 'mod' => 'mysql',
	#		   'desc' => $text{'links_mysqls'},
	#		   'page' => 'index.cgi?'.
	#			join("&", map { "show=$_" } @dbs) } );
	#	}
	}
return ( );
}

sub startstop_mysql
{
&require_mysql();
return undef if (!&mysql::is_mysql_local());	# cannot stop/start remote
local $r = &mysql::is_mysql_running();
if ($r == 1) {
	return { 'status' => 1,
		 'name' => $text{'index_myname'},
		 'desc' => $text{'index_mystop'},
		 'restartdesc' => $text{'index_myrestart'},
		 'longdesc' => $text{'index_mystopdesc'} };
	}
elsif ($r == 0) {
	return { 'status' => 0,
		 'name' => $text{'index_myname'},
		 'desc' => $text{'index_mystart'},
		 'longdesc' => $text{'index_mystartdesc'} };
	}
else {
	return undef;
	}
}

sub stop_service_mysql
{
&require_mysql();
return &mysql::stop_mysql();
}

sub start_service_mysql
{
&require_mysql();
return &mysql::start_mysql();
}

# quote_mysql_database(name)
# Returns a mysql database name with % and _ characters escaped
sub quote_mysql_database
{
local ($db) = @_;
$db =~ s/_/\\_/g;
$db =~ s/%/\\%/g;
return $db;
}

# show_template_mysql(&tmpl)
# Outputs HTML for editing MySQL related template options
sub show_template_mysql
{
local ($tmpl) = @_;

print &ui_table_row(&hlink($text{'tmpl_mysql'}, "template_mysql"),
	&none_def_input("mysql", $tmpl->{'mysql'}, $text{'tmpl_mysqlpat'}, 1).
	&ui_textbox("mysql", $tmpl->{'mysql'}, 20));

print &ui_table_row(&hlink($text{'tmpl_mysql_suffix'}, "template_mysql_suffix"),
	&none_def_input("mysql_suffix", $tmpl->{'mysql_suffix'},
		        $text{'tmpl_mysqlpat'}, 0).
	&ui_textbox("mysql_suffix", $tmpl->{'mysql_suffix'} eq "none" ?
					undef : $tmpl->{'mysql_suffix'}, 20));

print &ui_table_row(&hlink($text{'tmpl_mysql_wild'}, "template_mysql_wild"),
	&none_def_input("mysql_wild", $tmpl->{'mysql_wild'},
			$text{'tmpl_mysqlpat'}, 1).
	&ui_textbox("mysql_wild", $tmpl->{'mysql_wild'}, 20));

print &ui_table_row(&hlink($text{'tmpl_mysql_hosts'}, "template_mysql_hosts"),
	&none_def_input("mysql_hosts", $tmpl->{'mysql_hosts'},
			$text{'tmpl_mysqlh'}).
	&ui_textbox("mysql_hosts", $tmpl->{'mysql_hosts'} eq "none" ? "" :
					$tmpl->{'mysql_hosts'}, 40));

print &ui_table_row(&hlink($text{'tmpl_mysql_mkdb'}, "template_mysql_mkdb"),
	&ui_radio("mysql_mkdb", $tmpl->{'mysql_mkdb'},
		[ [ 1, $text{'yes'} ], [ 0, $text{'no'} ],
		  ($tmpl->{'default'} ? ( ) : ( [ "", $text{'default'} ] ) )]));

print &ui_table_row(&hlink($text{'tmpl_mysql_nopass'}, "template_mysql_nopass"),
	&ui_radio("mysql_nopass", $tmpl->{'mysql_nopass'},
		[ [ 0, $text{'yes'} ], [ 1, $text{'no'} ],
		  ($tmpl->{'default'} ? ( ) : ( [ "", $text{'default'} ] ) )]));
}

# parse_template_mysql(&tmpl)
# Updates MySQL related template options from %in
sub parse_template_mysql
{
local ($tmpl) = @_;

# Save MySQL-related settings
if ($in{'mysql_mode'} == 1) {
	$tmpl->{'mysql'} = undef;
	}
else {
	$in{'mysql'} =~ /^\S+$/ || &error($text{'tmpl_emysql'});
	$tmpl->{'mysql'} = $in{'mysql'};
	}
if ($in{'mysql_wild_mode'} == 1) {
	$tmpl->{'mysql_wild'} = undef;
	}
else {
	$in{'mysql_wild'} =~ /^\S*$/ || &error($text{'tmpl_emysql_wild'});
	$tmpl->{'mysql_wild'} = $in{'mysql_wild'};
	}
if ($in{'mysql_hosts_mode'} == 0) {
	$tmpl->{'mysql_hosts'} = "none";
	}
elsif ($in{'mysql_hosts_mode'} == 1) {
	$tmpl->{'mysql_hosts'} = undef;
	}
else {
	$in{'mysql_hosts'} =~ /\S/ || &error($text{'tmpl_emysql_hosts'});
	$tmpl->{'mysql_hosts'} = $in{'mysql_hosts'};
	}
if ($in{'mysql_suffix_mode'} == 0) {
	$tmpl->{'mysql_suffix'} = "none";
	}
elsif ($in{'mysql_suffix_mode'} == 1) {
	$tmpl->{'mysql_suffix'} = undef;
	}
else {
	$in{'mysql_suffix'} =~ /\S/ || &error($text{'tmpl_emysql_suffix'});
	$tmpl->{'mysql_suffix'} = $in{'mysql_suffix'};
	}
$tmpl->{'mysql_mkdb'} = $in{'mysql_mkdb'};
$tmpl->{'mysql_nopass'} = $in{'mysql_nopass'};
}

$done_feature_script{'mysql'} = 1;

1;

