#!/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_oracle_upgrade
#
# DESCRIPTION : This function upgrades the schema for Oracle
#
# AUTHORS :     Akos.Frohner@cern.ch
#
# YAIM MODULE:  glite-yaim-lfc
#
##############################################################################

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


function config_lfc_oracle_upgrade () {

INSTALL_ROOT=${INSTALL_ROOT:-/opt} 

CONNECT_STRING=${LFC_DB}/${LFC_DB_PASSWORD}@${LFC_DB_HOST}
if [ -z "$ORACLE_HOME" ]; then
  export ORACLE_HOME=$ORACLE_LOCATION/client
  yaimlog WARNING "ORACLE_HOME not set, assuming $ORACLE_HOME"
fi

if [ -f /usr/bin/sqlplus ]; then
        SQLPLUSBINARY="/usr/bin/sqlplus"
elif [ -f $ORACLE_HOME/bin/sqlplus ]; then
        SQLPLUSBINARY="$ORACLE_HOME/bin/sqlplus"
elif [ -f $ORACLE_LOCATION/client/bin/sqlplus ]; then
        SQLPLUSBINARY="$ORACLE_LOCATION/client/bin/sqlplus"
elif [ -f $ORACLE_LOCATION/client64/bin/sqlplus ]; then
        SQLPLUSBINARY="$ORACLE_LOCATION/client64/bin/sqlplus"
else
        yaimlog ERROR "sqlplus not found"
fi

# get the schema version
#
QUERY_FILE=`mktemp "/tmp/config_lfc_oracle.XXXXXX"`
echo "SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON" > $QUERY_FILE
echo "select major, minor, patch  from schema_version;  " >> $QUERY_FILE
aux=$(${SQLPLUSBINARY} -S ${CONNECT_STRING} < $QUERY_FILE)
RETVAL=$?
/bin/rm $QUERY_FILE

# database doesn't exist -> fresh install
HAS_ERROR=$(echo $aux|grep "ERROR" > /dev/null)
NUM_MATCHES=$?
if [ "$NUM_MATCHES" -eq 0 ] ; then
	yaimlog INFO "Clean installation. Ignoring upgrade."
	return 0
fi

if [ -z "$aux" ]; then
  yaimlog ERROR "No schema version information found. Do not upgrading. Exiting."
fi

# checking the version
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 "$LFC_DB_PASSWORD" > $passfile

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

  export ORACLE_HOME
  # 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 Oracle --db-host ${LFC_DB_HOST} --pwd-file ${passfile} --db ${LFC_DB} --user ${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
}

