#!/bin/bash
##############################################################################
# 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_DPM_mysql
#
# DESCRIPTION : This function configures the MySQL server
#
# AUTHORS :     David.Smith@cern.ch, Gergely.Debreczeni@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

config_DPM_mysql_check() {

 requires $1 MYSQL_PASSWORD DPM_DB_PASSWORD VOS DPM_HOST DPM_DB_USER DPM_DB_HOST DPM_DB DPNS_DB
 retcode=$?
 return ${retcode}

}

config_DPM_mysql(){

INSTALL_ROOT=${INSTALL_ROOT:-/opt}

if [ ! "$DPM_DB_HOST" ]; then
 DPM_DB_HOST=localhost
fi

thishost=`hostname -f | tr '[:upper:]' '[:lower:]'`


####@ Check that the DPM DB password doesn't contain "@"

badpassword=`echo "${DPM_DB_PASSWORD}" | grep "@"`
echo "$badpassword"
if [ "x${badpassword}x" != "xx" ]; then
	yaimlog ERROR "The DPM database user password cannot contain the \"@\" character. Exiting."
	return 1
fi

####@ Sets up dpm user mysql password.
####@ If you use remote DB it does not try to set the mysql root password.

if [ "x$DPM_DB_HOST" = "xlocalhost" ] || [ "x$thishost" = "x$DPM_DB_HOST" ]; then

 if [ "x${DPM_HOST}x" == "x${DPM_DB_HOST}x" ]; then
	start_mysql || return 1
 fi

 yaimlog INFO  "Setting mysql password on $thishost."
 set_mysql_passwd || return 1 # the function uses $MYSQL_PASSWORD
 yaimlog DEBUG  "Granting access to  DPM_DB_USER on ${DPM_DB_HOST}."
 mysql --pass=$MYSQL_PASSWORD --exec "grant all on ${DPM_DB}.* to '$DPM_DB_USER'@'localhost' identified by '$DPM_DB_PASSWORD' with grant option"  || return 1
 mysql --pass=$MYSQL_PASSWORD --exec "grant all on ${DPM_DB}.* to '$DPM_DB_USER'@'$DPM_HOST' identified by '$DPM_DB_PASSWORD' with grant option"  || return 1
 mysql --pass=$MYSQL_PASSWORD --exec "grant all on ${DPNS_DB}.* to '$DPM_DB_USER'@'localhost' identified by '$DPM_DB_PASSWORD' with grant option" || return 1
 mysql --pass=$MYSQL_PASSWORD --exec "grant all on ${DPNS_DB}.* to '$DPM_DB_USER'@'$DPM_HOST' identified by '$DPM_DB_PASSWORD' with grant option" || return 1

else 

 yaimlog DEBUG  "Granting access to  DPM_DB_USER on ${DPM_DB_HOST}."
 mysql  -h ${DPM_DB_HOST} --pass=$MYSQL_PASSWORD --exec "grant all on ${DPM_DB}.* to '$DPM_DB_USER'@'$DPM_HOST' identified by '$DPM_DB_PASSWORD' with grant option"  || return 1
 mysql  -h ${DPM_DB_HOST} --pass=$MYSQL_PASSWORD --exec "grant all on ${DPNS_DB}.* to '$DPM_DB_USER'@'$DPM_HOST' identified by '$DPM_DB_PASSWORD' with grant option" || return 1

fi

####@ If the mysql tables are not created , creates them.

DPNS_SQL="${LCG_LOCATION}/share/DPM/create_dpns_tables_mysql.sql"
if [ -e "/usr/share/lcgdm/create_dpns_tables_mysql.sql" ]; then
    DPNS_SQL="${LCG_LOCATION}/share/lcgdm/create_dpns_tables_mysql.sql"
fi

if ( ! mysql -h $DPM_DB_HOST -u $DPM_DB_USER --pass=$DPM_DB_PASSWORD -e "use $DPNS_DB;" > /dev/null 2>&1 ); then
        DPNS_SCRIPT=`mktemp /tmp/create_dpns_tables_mysql.XXXXXX`
        sed -e "s/CREATE DATABASE cns_db;/CREATE DATABASE $DPNS_DB;/" \
                -e "s/USE cns_db;/USE $DPNS_DB;/" \
                ${DPNS_SQL} \
                > $DPNS_SCRIPT

        mysql -u $DPM_DB_USER --pass=$DPM_DB_PASSWORD -h $DPM_DB_HOST < $DPNS_SCRIPT || return 1
        rm $DPNS_SCRIPT
        unset DPNS_SCRIPT
fi

DPM_SQL="${LCG_LOCATION}/share/DPM/create_dpm_tables_mysql.sql"
if [ -e "/usr/share/lcgdm/create_dpm_tables_mysql.sql" ]; then
    DPM_SQL="${LCG_LOCATION}/share/lcgdm/create_dpm_tables_mysql.sql"
fi

if ( ! mysql -h $DPM_DB_HOST -u $DPM_DB_USER --pass=$DPM_DB_PASSWORD -e "use $DPM_DB;" > /dev/null 2>&1 ); then
        DPM_SCRIPT=`mktemp /tmp/create_dpm_tables_mysql.XXXXXX`
        sed -e "s/CREATE DATABASE dpm_db;/CREATE DATABASE $DPM_DB;/" \
                -e "s/USE dpm_db;/USE $DPM_DB;/" \
                ${DPM_SQL} \
                > $DPM_SCRIPT

        mysql -u $DPM_DB_USER --pass=$DPM_DB_PASSWORD -h $DPM_DB_HOST < $DPM_SCRIPT || return 1
        rm $DPM_SCRIPT
        unset DPM_SCRIPT
fi

####@ Configures the DPMCONFIG and NSCONFIG files and sets their proper permission.

DPMCONFIG="${LCG_LOCATION}/etc/DPMCONFIG"
touch ${DPMCONFIG}
chown ${DPMMGR_USER}:${DPMMGR_GROUP} ${DPMCONFIG}
chmod 600 ${DPMCONFIG}
cat << EOC > $DPMCONFIG
$DPM_DB_USER/$DPM_DB_PASSWORD@$DPM_DB_HOST/${DPM_DB}
EOC

NSCONFIG="${LCG_LOCATION}/etc/NSCONFIG" 
touch ${NSCONFIG}
chown ${DPMMGR_USER}:${DPMMGR_GROUP} ${NSCONFIG}
chmod 600 ${NSCONFIG}
cat << EOF > $NSCONFIG
$DPM_DB_USER/$DPM_DB_PASSWORD@$DPM_DB_HOST/${DPNS_DB}
EOF

####@ Configure the requests table cleanup by default.

add_to_shift 'DPM REQCLEAN' 3m

return 0

}

