#!/bin/sh
# Copyright (C) 1997  TCX DataKonsult AB & Monty Program KB & Detron HB
# For a more info consult the file COPYRIGHT distributed with this file

# This scripts creates the privilige tables db, host, user in mysql
# It should be run from the top level installation directory

#if test ! -x /usr/bin/mysqladmin
#then
#  echo "Can't execute /usr/bin/mysqladmin"
#  if test "/var/lib/mysql" = "./data"
#  then
#    echo "You should be in the distribution directory when executing this script"
#  else
#    echo "You should do a 'make install' before executing this script"
#  fi
#  exit 1
#fi
#if test ! -d "./data"
#then
#  echo "You should be in the distribution directory when executing this script"
#  exit 1
#fi

if test -f /var/lib/mysql/mysql/db.ISM
then
  echo "mysql privilege databases already installed. If you want to recreate all"
  echo "privilege tables, execute 'rm -i /var/lib/mysql/mysql/*.IS?'"
  echo "and run /usr/bin/mysql_install_db"
  echo
  echo "You can now start the new mysql server with:"
  echo "/usr/bin/safe_mysqld -l &"
  echo
  echo "Plese report any problems with the /usr/bin/mysqlbug script"
  exit 1
fi

# On IRIX hostname is in /usr/bsd so add this to the path
PATH=$PATH:/usr/bsd

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

# create database mysql & test
#
if test ! -d /var/lib/mysql ; then mkdir /var/lib/mysql ; fi
if test ! -d /var/lib/mysql/mysql ; then mkdir /var/lib/mysql/mysql ; fi
if test ! -d /var/lib/mysql/test ; then mkdir /var/lib/mysql/test ; fi

/usr/bin/mysqladmin ver > /dev/null 2>&1
if test $? -eq 0
then
  echo "The mysqld demon is already running. Stop it with"
  echo  "'/usr/bin/mysqladmin shutdown' and try again."
  exit 1;
else 
  echo "Starting mysql server"
  /usr/bin/safe_mysqld -Sg -l &
  while true
  do
    sleep 1			# This should be enough
    /usr/bin/mysqladmin ver > /dev/null 2>&1
    if test $? -eq 0 ; then break; fi
    sleep 5;			# This must be enough
    /usr/bin/mysqladmin ver > /dev/null 2>&1
    if test $? -eq 0 ; then break; fi
    echo "mysqld demon is not responding. Please try to start it manually with"
    echo "/usr/lib/mysql/mysqld --skip-grant --log"
    echo
    echo "You can find some information about why it didn't start by examining"
    echo "the log file in the /var/lib/mysql directory."
    echo
    echo "Please report bugs with the /usr/bin/mysqlbug script!"
    exit 1;
  done
fi

# copy the definition files
#
if test "/var/lib/mysql" != "./data"
then
  cp -p /usr/lib/mysql/data/*.frm /var/lib/mysql/mysql
fi

# for debian, ask if we want a root password
#
echo
echo "It is highly recommended that you set a password for the root mysql"
echo "user. Otherwise any user on your system can delete all databases!"
echo
echo "Enter a password for the mysql root user (or 'none' for no password)"

stty -echo # turns off echoing
read -p 'Password: ' pass
stty echo  # turns echoing back on
echo
if  [ -z $pass ] 
then 
   mypass="''"    
   echo "OK, no password..."
elif  [ $pass = "none" ]
then 
   mypass="''"    
   echo "none entered, no password..."

else
    echo "mysql root password set..."
    mypass="password('$pass')"
fi 



/usr/bin/mysql mysql <<END_OF_DATA
# Create tables from the .frm files
#
delete from db ;
delete from host;
delete from user; 

#
# Dumping data for table 'db'
#

INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y');
INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y');

#
# Dumping data for table 'host'
#

INSERT INTO host VALUES ('localhost','%','Y','Y','Y','Y','Y','Y');
INSERT INTO host VALUES ('$hostname','%','Y','Y','Y','Y','Y','Y');

#
# Dumping data for table 'user'
#
# for debian, $mypass is the chosen root password (if any)


INSERT INTO user VALUES ('localhost','root', $mypass ,'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
INSERT INTO user VALUES ('localhost','','','N','N','N','N','N','N','N','N','N','N');
INSERT INTO user VALUES ('$hostname','root', $mypass ,'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
INSERT INTO user VALUES ('$hostname','','','N','N','N','N','N','N','N','N','N','N');
END_OF_DATA

if test $? -eq 0
then 
  /usr/bin/mysqladmin reload
  echo "mysqld demon is running and mysql grant tables are installed."
  echo
#  echo "PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !"
#  echo
  echo "You can test the MySQL demon with the benchmarks in the 'bench'"
  echo "directory:"
  echo "cd bench ; run-all-tests"
  echo
  echo "You can also try the mysql command line tool with:"
  echo "/usr/bin/mysql test"
  echo
  echo "Plese report any problems with the /usr/bin/mysqlbug script"
  echo
  echo "Have fun and at least consider supporting MySQL if you find it useful :)"
else
  echo "The grant tables was not installed. You should examine the log"
  echo "in /var/lib/mysql for more information. You can also try to start"
  echo "the mysqld demon with --skip-grant and use the command line tool"
  echo "/usr/bin/mysql to connect to the mysql database and look at the"
  echo "grant tables:"
  echo
  echo "shell> /usr/bin/mysql -u root mysql"
  echo "mysql> show tables"
  echo
  echo "Starting mysqld with --help gives some information if you have problems"
  echo "with paths. Using --log gives you a log that may be helpful."
  echo
  echo "Plese report any problems with the /usr/bin/mysqlbug script"
  echo
fi

echo "The latest information about MySQL is available on the web at http://www.tcx.se"
