#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2008.
# 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_lfc_mysql_upgrade
#
# DESCRIPTION : This function upgrades the schema for MySQL
#
# AUTHORS :     Akos.Frohner@cern.ch
#
# YAIM MODULE:  glite-yaim-lfc
#
##############################################################################

config_lfc_mysql_upgrade_check () {
 requires $1 MYSQL_PASSWORD LFC_DB_PASSWORD LFC_DB LFC_DB_HOST GLITE_LOCATION LCG_LOCATION
 retcode=$?
 return ${retcode}
}


function config_lfc_mysql_upgrade () {

INSTALL_ROOT=${INSTALL_ROOT:-/opt} 

if [ "x${LFC_HOST}x" == "x${LFC_DB_HOST}x" ]; then
 start_mysql || return 1
 set_mysql_passwd || return 1
else
 yaimlog INFO "mysql database machine not local, not checking or setting the mysql root password"
fi


# database doesn't exist -> fresh install
if ( ! mysql -p"$MYSQL_PASSWORD" -h $LFC_DB_HOST -e "use $LFC_DB;" > /dev/null 2>&1 ); then
	return 0
fi

# checking the version
yaimlog INFO "Checking for database schema version... "
aux=`mysql --skip-column-names -s -u root --pass=${MYSQL_PASSWORD}  -h ${LFC_DB_HOST} -e "use $LFC_DB ; select * from schema_version ;" 2> /dev/null`
if [ $? -ne 0 ]; then
  yaimlog ERROR "No LFC database schema version information found. Exiting."
  return 1
fi
cnsvdots=`echo $aux | awk '{print $1"."$2"."$3}'`
cnsv=`echo $aux | awk '{print $1$2$3}'`

yaimlog INFO "Database version used: $cnsvdots"

# Do we need to update?
ls ${LCG_LOCATION}/share/lcgdm/upgrades/cns-db-$cnsv-* &> /dev/null
cnsupdate=$(($?==0))

if [ $cnsupdate -ne 0 ]; then
  # Stop services to update
  service lfc-dli stop
  service lfcdaemon stop

  # Store password
  passfile=`mktemp /tmp/lfc_upgrade.XXXXXX`
  echo "$MYSQL_PASSWORD" > $passfile

  # Change working directory
  cd ${LCG_LOCATION}/share/lcgdm/upgrades/ &> /dev/null

  # Update CNS Schema
  for file in `ls cns-db-*`
  do
    fromv=`echo $file | cut -d - -f 3`
    tov=`echo $file | cut -d - -f 5 | cut -d . -f 1`
    if [ $tov -gt $cnsv ]; then
      yaimlog INFO "Upgrading CNS schema from $fromv to $tov"
      ./$file --db-vendor MySQL --db-host ${LFC_DB_HOST} --pwd-file ${passfile} --db ${LFC_DB} --verbose
      if [ $? -ne 0 ]; then
        yaimlog ERROR "Error upgrading CNS schema"
        return 1
      fi
    fi
  done

  yaimlog INFO "CNS schema up to date"

  # Return to previous directory
  cd - &> /dev/null

  # Remove password
  rm -f ${passfile}

  # Restart
  service lfcdaemon start
  service lfc-dli start
fi

return 0
}

