#!/bin/sh
# DocumentId:	$Id: dpsyco-mysql-dbupdaccess,v 1.1 2001/09/03 08:33:05 ola Exp $
# Author:	$Author: ola $
#		Ola Lundqvist <ola.lundqvist@euronetics.se>
# Arguments:	username host database
# Summary:
#	Helps to create a user for the mysql-database.

dbserver=localhost

. /usr/share/wwwconfig-common/mysql-localadmpass.get

dbuser=$1
dballow=$2
dbname=$3

status=error

. /usr/share/wwwconfig-common/mysql.get
if [ -z "$dbuser" ] ; then
    echo "No database user specified. Can not create it if it does not exist."
elif [ -z "$dbserver" ] ; then
    echo "No database server specified."
elif [ -z "$dbadmin" ] ; then
    echo "No database administrator specified."
elif [ -z "$dbadmpass" ] ; then
    echo "No database ($dbadmin) administrator password specified."
elif [ ! -x $(which mysql) ] ; then
    echo "No mysql client to execute."
elif ! $mysqlcmd -f mysql -e "show tables;" >/dev/null 2>&1 ; then
    echo "Error when trying to connect to the mysql database."
    echo "This error can occur if you have no database to connect to, or"
    echo "if the password was incorrect."
    echo "use: dpkg-reconfigure -plow packagename to reconfigure."
else
    echo "Gives update access to database user ($dbuser) for $dbname on $dballow."
    if $mysqlcmd -f mysql -e "
	CONNECT mysql;

	REPLACE INTO db ( host, db, user, select_priv, insert_priv, update_priv,
			  delete_priv )
	VALUES (
	    '$dballow',
	    '$dbname',
	    '$dbuser',
	    'Y', 'Y', 'Y',
	    'Y'
	);

	flush privileges;

	" ; then

	if ! $mysqlcmd -f mysql -e "select User from db;" | grep $dbuser >/dev/null 2>&1 ; then
	    echo "Database user $dbuser NOT successfully granted. You have to do it manually."
	fi
    else
	echo "Unable to run the update access script."
    fi
fi
