#!/bin/sh
# Copyright (C) 2001-2007 Dmitry V. Levin <ldv@altlinux.org>
#                         Anton Kachalov  <mouse@altlinux.org>
#                         Konstantin A. Lepikhov <lakostis@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; version 2 of the License.
# 
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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
GetConf mysqld basedir '' /usr

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

# don't run this script if mysql.user table exists
if eval "echo 'describe mysql.user;' | \
                         $mysqld --bootstrap --skip-innodb \
                                 --max_allowed_packet=8M \
                                 --net_buffer_length=16K" >/dev/null 2>&1;then
        exit 0
fi

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

# Initialize variables
fill_help_tables=
mysql_system_tables=
mysql_system_tables_data=
create_user=

for file in mysql_system_tables mysql_system_tables_data fill_help_tables; do
	if test ! -f "/usr/share/mysql/$file.sql"
	then
		echo "FATAL ERROR: Could not find SQL file '$file.sql' in"
		echo "/usr/share/mysql"
		exit 1
	else 
		eval $file="/usr/share/mysql/$file.sql"
	fi
done

[ -f "$chroot$datadir/mysql/user.frm" ] || create_user=1

# Peform the install of system tables
if [ -n "$mysql_system_tables" -o -n "$mysql_system_tables_data" -o -n "$fill_help_tables" ]; then
	echo "Installing all prepared tables"
	for dir in mysql; do
			[ -d "$chroot$datadir/$dir" ] || mkdir $chroot$datadir/$dir
			# Change permissions to fix any new files.
			chown -R mysql:mysql "$chroot$datadir/$dir"

			# Fix permissions for the permission database so that only the user can read them.
			chmod -R g-w,o-rw "$chroot$datadir/$dir"
	done
	if eval "(echo 'use mysql;'; cat $mysql_system_tables $mysql_system_tables_data $fill_help_tables) | \
			 $mysqld --bootstrap --skip-innodb \
			         --max_allowed_packet=8M \
			         --net_buffer_length=16K"
	then
		if [ -n "$create_user" ]; then
			cat << END_OF_DATA
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root  password 'new-password'
/usr/bin/mysqladmin -u root -h $hostname  password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

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
END_OF_DATA
  	else
		cat << END_OF_DATA
Installation of system 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!
END_OF_DATA
		exit 1
	fi
else
	echo No additional actions have to be done with MySQL privilege tables.
fi
