#!/bin/bash

# Adding all privileges to debian-sys-maint

exec_sql()
{
if [ $# -ne 1 ]
    then
    echo "Programing error!"
    exit 200
fi
mysql --defaults-file=/etc/mysql/debian.cnf -ss -n <<STOP
$1
\q
STOP
if [ $? -ne 0 ] ; then  echo "Privilege fixing failed for debian-sys-maint!" >&2
exit 131
fi
}

exec_sql "SHOW GRANTS FOR 'debian-sys-maint'@'localhost';" |grep "GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost'" >/dev/null

if [ $? -ne 0 ] ; then

    echo "Fixing mysql debian-sys-maint privileges"

    exec_sql "use mysql; \
	UPDATE user SET \
	Create_view_priv = 'Y', \
	Show_view_priv = 'Y', \
	Create_routine_priv = 'Y', \
	Alter_routine_priv = 'Y', \
	Create_user_priv = 'Y' \
	WHERE User = 'debian-sys-maint'; \
	FLUSH PRIVILEGES;"

    exec_sql "GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost';"

fi
