#!/bin/sh
# Copyright (C) 2001,03  Dmitry V. Levin <ldv@fandra.org>
#                        Anton Kachalov  <mouse@altlinux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

PROG="${0##*/}"

GetConf()
{
	local group=$1
	[ -n "$group" ] || group="$PROG"
	shift

	local name=$1
	shift

	local varname=$1
	[ -n "$varname" ] || varname="$name"
	shift

	local default=$1
	shift

	local val=`my_print_defaults "$group" |grep "^--$name=" |tail -1`
	if [ -n "$val" ]; then
		eval export $varname="${val#--$name=}"
	else
		eval export $varname="$default"
	fi
}

GetConf '' mysqld '' /usr/sbin/mysqld
GetConf mysqld chroot '' /var/lib/mysql
GetConf mysqld datadir '' /db

if ! cd "$chroot$datadir"; then
	echo "Cannot change to datadir '$datadir'"
	exit 1
fi

hostname=`/bin/hostname`		# Install this too in the user table

# Initialize variables
create_db=
insert_db=
create_host=
create_user=
insert_user=
create_func=
create_tables_priv=
create_columns_priv=

# Check for old tables
if [ ! -f mysql/db.frm ]; then
	echo "Preparing db table"

	create_db="\
CREATE TABLE db (\
  Host char(60) binary DEFAULT '' NOT NULL,\
  Db char(64) binary DEFAULT '' NOT NULL,\
  User char(16) binary DEFAULT '' NOT NULL,\
  Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  References_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  PRIMARY KEY Host (Host,Db,User),\
  KEY User (User)\
) comment='Database privileges' type=MyISAM;"

	insert_db="\
INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');
INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');"
fi

if [ ! -f mysql/host.frm ]; then
	echo "Preparing host table"

	create_host="\
CREATE TABLE host (\
  Host char(60) binary DEFAULT '' NOT NULL,\
  Db char(64) binary DEFAULT '' NOT NULL,\
  Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  References_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  PRIMARY KEY Host (Host,Db)\
) comment='Host privileges; Merged with database privileges' type=MyISAM;"
fi

if [ ! -f mysql/user.frm ]; then
	echo "Preparing user table"

	create_user="\
CREATE TABLE user (\
  Host char(60) binary DEFAULT '' NOT NULL,\
  User char(16) binary DEFAULT '' NOT NULL,\
  Password char(16) binary DEFAULT '' NOT NULL,\
  Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Reload_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Shutdown_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Process_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  File_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  References_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Show_db_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Super_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Execute_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Repl_slave_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  Repl_client_priv enum('N','Y') DEFAULT 'N' NOT NULL,\
  ssl_type enum('','ANY','X509', 'SPECIFIED') DEFAULT '' NOT NULL,\
  ssl_cipher BLOB NOT NULL,\
  x509_issuer BLOB NOT NULL,\
  x509_subject BLOB NOT NULL,\
  max_questions int(11) unsigned DEFAULT 0  NOT NULL,\
  max_updates int(11) unsigned DEFAULT 0  NOT NULL,\
  max_connections int(11) unsigned DEFAULT 0  NOT NULL,\
  PRIMARY KEY Host (Host,User)\
) comment='Users and global privileges' type=MyISAM;"

	insert_user="\
INSERT INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
INSERT INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);

REPLACE INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
REPLACE INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);

INSERT INTO user (host,user) values ('localhost','');
INSERT INTO user (host,user) values ('$hostname','');"
fi

if [ ! -f mysql/func.frm ]; then
	echo "Preparing func table"

	create_func="\
CREATE TABLE func (\
  name char(64) binary DEFAULT '' NOT NULL,\
  ret tinyint(1) DEFAULT '0' NOT NULL,\
  dl char(128) DEFAULT '' NOT NULL,\
  type enum ('function','aggregate') NOT NULL,\
  PRIMARY KEY (name)\
) comment='User defined functions' type=MyISAM;"
fi

if [ ! -f mysql/tables_priv.frm ]; then
	echo "Preparing tables_priv table"

	create_tables_priv="\
CREATE TABLE tables_priv (\
  Host char(60) binary DEFAULT '' NOT NULL,\
  Db char(64) binary DEFAULT '' NOT NULL,\
  User char(16) binary DEFAULT '' NOT NULL,\
  Table_name char(60) binary DEFAULT '' NOT NULL,\
  Grantor char(77) DEFAULT '' NOT NULL,\
  Timestamp timestamp(14),\
  Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL,\
  Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,\
  PRIMARY KEY (Host,Db,User,Table_name),\
  KEY Grantor (Grantor)\
) comment='Table privileges' type=MyISAM;"
fi

if [ ! -f mysql/columns_priv.frm ]; then
	echo "Preparing columns_priv table"

	create_columns_priv="\
CREATE TABLE columns_priv (\
  Host char(60) binary DEFAULT '' NOT NULL,\
  Db char(64) binary DEFAULT '' NOT NULL,\
  User char(16) binary DEFAULT '' NOT NULL,\
  Table_name char(64) binary DEFAULT '' NOT NULL,\
  Column_name char(64) binary DEFAULT '' NOT NULL,\
  Timestamp timestamp(14),\
  Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,\
  PRIMARY KEY (Host,Db,User,Table_name,Column_name)\
) comment='Column privileges' type=MyISAM;"
fi

if [ -n "$create_db" -o -n "$insert_db" -o -n "$create_host" -o -n "$create_user" -o -n "$insert_user" -o -n "$create_func" -o -n "$create_tables_priv" -o -n "$create_tables_priv" ]; then
	echo "Installing all prepared tables"
	if eval "$mysqld --bootstrap --skip-grant-tables --skip-innodb --skip-bdb" << END_OF_DATA
use mysql;
$create_db
$insert_db

$create_host
$insert_host

$create_user
$insert_user

$create_func

$create_tables_priv
$create_columns_priv
END_OF_DATA
	then
		if [ -n "$create_user" -o -n "$insert_user" ]; then
			cat << END_OF_DATA
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
This is done with:
$bindir/mysqladmin -u root  password 'new-password'
$bindir/mysqladmin -u root -h $hostname  password 'new-password'
See the manual for more instructions.
END_OF_DATA
		fi

		# Print message about upgrading unless we have created a new db table.
		if [ -z "$create_db" ]; then
			cat << END_OF_DATA
NOTE: If you are upgrading from a MySQL <= 3.22.10 you should run
the /usr/bin/mysql_fix_privilege_tables. Otherwise you will not be
able to use the new GRANT command!
END_OF_DATA
		fi

		cat << END_OF_DATA
Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at https://order.mysql.com
END_OF_DATA
	else
		cat << END_OF_DATA
Installation of grant tables failed!

Examine the logs in $chroot/log for more information.
You can also try to start the mysqld daemon with:
$mysqld --skip-grant &
You can use the command line tool
/usr/bin/mysql to connect to the mysql
database and look at the grant tables:

shell> /usr/bin/mysql -u root mysql
mysql> show tables

Try 'mysqld --help' if you have problems with paths. Using --log
gives you a log in $chroot/log that may be helpful.

The latest information about MySQL is available on the web at
http://www.mysql.com
Please consult the MySQL manual section: 'Problems running mysql_install_db',
and the manual section that describes problems on your OS.
Another information source is the MySQL email archive.
Please check all of the above before mailing us!
And if you do mail us, you MUST use the /usr/bin/mysqlbug script!
		cat << END_OF_DATA
Please report any problems with the /usr/bin/mysqlbug script!
END_OF_DATA

		exit 1
	fi
else
	echo No additional actions have to be done with MySQL privilege tables.
fi
