##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################


# This function has 4 input parameters:
# #1 db_name_grant
# #2 db_user_grant
# #3 db_password_grant

function grant_privileges() {

  db_name_grant=$1
  db_user_grant=$2
  db_password_grant=$3

    # grants access to db_name to user db_user and creates it
    mysql -h ${CREAM_DB_HOST} -u root --password="$MYSQL_PASSWORD" -e "GRANT ALL PRIVILEGES on ${db_name_grant}.* to ${db_user_grant}@localhost IDENTIFIED BY '${db_password_grant}' WITH GRANT OPTION;"
    mysql -h ${CREAM_DB_HOST} -u root --password="$MYSQL_PASSWORD" -e "GRANT ALL PRIVILEGES on ${db_name_grant}.* to ${db_user_grant}@'$HOSTNAME' IDENTIFIED BY '${db_password_grant}' WITH GRANT OPTION;"

    # optionally grants privileges to monitor the database accessing from external hosts
    if [ "$ACCESS_BY_DOMAIN" = "yes" ]; then
      mysql -h ${CREAM_DB_HOST} -u root --password="$MYSQL_PASSWORD" -e "GRANT ALL PRIVILEGES on ${db_name_grant}.* to ${CREAM_DB_USER}@'%.$MY_DOMAIN' IDENTIFIED BY '${CREAM_DB_PASSWORD}' WITH GRANT OPTION;"
    fi
}

