##############################################################################
# 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.
##############################################################################
#
# NAME :        config_storm_mysql
#
# DESCRIPTION : This function configures mysqld service on a StoRM SE.
#
# AUTHORS :     grid-release@infn.it
#
# NOTES :       
#
# YAIM MODULE:  yaim-storm
#
##############################################################################

function config_storm_mysql_check () {

  requires $1 MYSQL_PASSWORD STORM_DB_PWD STORM_DB_USER
  return $?

}

function config_storm_mysql_setenv () {

  yaimlog INFO "No enviroment variables to set."

}

function config_storm_mysql () {

  FUNCTION="config_storm_mysql"

  # General variables definition
  export MYSQL_PASSWORD=${MYSQL_PASSWORD}
  export STORM_DB_USER=${STORM_DB_USER}
  
  # Activate and start mysql service
  /sbin/chkconfig --add mysqld
  /sbin/chkconfig mysqld on
  yaimlog INFO "${FUNCTION}: Restart MySQL service"
  /sbin/service mysqld stop
  /sbin/service mysqld start

  # Set root password for localhost
  yaimlog DEBUG "${FUNCTION}: Set MySQL password for 'root@localhost'"
  mysqladmin -u root password ${MYSQL_PASSWORD} &> /dev/null
  if [ $? != 0 ]; then
    yaimlog WARNING "${FUNCTION}: MySQL password for 'root@localhost' is already set! Skipping"
  fi

  # Set root password for host
  HOST=`hostname -f`
  yaimlog DEBUG "${FUNCTION}: Set MySQL password for 'root@${HOST}'"
  mysqladmin -h ${HOST} -u root password ${MYSQL_PASSWORD} &> /dev/null
  if [ $? != 0 ]; then
    yaimlog WARNING "${FUNCTION}: MySQL password for 'root@${HOST}' is already set! Skipping"
  fi

  # Configure StoRM databases
  FILE="/etc/storm/backend-server/db/storm_database_config.sh"
  yaimlog DEBUG "${FUNCTION}: Run ${FILE} script"
  bash ${FILE}

  # Set STORM_DB_USER password
  if [ "${STORM_DB_USER}" != "root" ]; then
    FILE=`mktemp`
    cat > ${FILE} << EOF
use mysql;
update user set password=PASSWORD("${STORM_DB_PWD}") where User='${STORM_DB_USER}';
flush privileges;
EOF
    yaimlog DEBUG "${FUNCTION}: Set MySQL password for '${STORM_DB_USER}'"
    mysql -u root -p${MYSQL_PASSWORD} < ${FILE}
    if [ $? != 0 ]; then
      yaimlog ERROR "${FUNCTION}: MySQL password for '${STORM_DB_USER}' can't be set!"
      yaimlog ERROR "${FUNCTION}: Maybe \$MYSQL_PASSWORD for 'root' is wrong!"
      yestr ${YEX_CONFIG}
      yaimlog ERROR "${YERRORSTR}"
      return ${YEX_CONFIG}
    fi
  fi
  rm -f ${FILE}

  # Exit with success
  return 0

}
