#!/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_upgrade_oracle
#
# DESCRIPTION : This function upgrades the schema in Oracle
#
# AUTHORS :     David.Smith@cern.ch, Gergely.Debreczeni@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

config_DPM_upgrade_oracle_check () {
 requires $1 DPM_DB_USER DPM_DB_PASSWORD DPM_DB_HOST
 retcode=$?
 return ${retcode}
}

function upgrade_stop_services () {
 yaimlog INFO "Stopping DPM/DPNS services"
 service dpmcopyd stop
 service dpm-gsiftp stop 
 service srmv2.2 stop 
 service srmv2 stop 
 service srmv1 stop 
 service dpm stop 
 service rfiod stop 
 service dpnsdaemon stop
}

function upgrade_start_services () {
 yaimlog "Starting up the DPM/DPNS services"
 service dpnsdaemon start
 service rfiod start
 service dpm start
 service srmv1 start
 service srmv2 start
 service srmv2.2 start
 service dpm-gsiftp start
 service dpmcopyd start
}

function config_DPM_upgrade_oracle () {

INSTALL_ROOT=${INSTALL_ROOT:-/opt}
CONNECT_STRING=${DPM_DB}/${DPM_DB_PASSWORD}@${DPM_DB_HOST}

# Gets the cns and dpm db's schema version
yaimlog INFO "Checking for database 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 * from schema_version_dpm;" >> $QUERY_FILE
aux=$(/usr/bin/sqlplus -S ${CONNECT_STRING} < $QUERY_FILE)
if [ $? -ne 0 ]; then
  yaimlog ERROR "No DPM database schema version information found. Exiting."
  return 1
fi
dpmvdots=`echo $aux | awk '{print $1"."$2"."$3}'`
dpmv=`echo $aux | awk '{print $1$2$3}'`


echo "SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON" > $QUERY_FILE
echo "select * from schema_version;" >> $QUERY_FILE
aux=$(/usr/bin/sqlplus -S ${CONNECT_STRING} < $QUERY_FILE)
if [ $? -ne 0 ]; then
  yaimlog ERROR "No DPNS 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}'`

rm -f $QUERY_FILE &> /dev/null

yaimlog INFO "Database versions used: DPM $dpmvdots - DPNS $cnsvdots"

# Good version?
if [ $dpmv -lt 300 ]; then
  yaimlog WARNING "This version of yaim supports only DPM database schema upgrade from versions 3.0.0 or 3.1.0 to 3.2.0"
  yaimlog WARNING "If you have schema version less then 3.0.0 , then please use an earlier version of YAIM to upgrade to DPM database schema version 3.0.0 !"
  return 1
fi

# Do we need to update?
ls ${LCG_LOCATION}/share/lcgdm/upgrades/dpm-db-$dpmv-* &> /dev/null
dpmupdate=$(($?==0))
ls ${LCG_LOCATION}/share/lcgdm/upgrades/cns-db-$cnsv-* &> /dev/null
cnsupdate=$(($?==0))
if [ $dpmupdate -ne 0 ] || [ $cnsupdate -ne 0 ]; then
  # Stop the services
  upgrade_stop_services

  # Store password
  passfile=`mktemp /tmp/dpm_upgrade.XXXXXX`
  echo "$DPM_DB_PASSWORD" > $passfile

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

  # Update DPM Schema
  for file in `ls dpm-db-*`
  do
    fromv=`echo $file | cut -d - -f 3`
    tov=`echo $file | cut -d - -f 5 | cut -d . -f 1`
    if [ $tov -gt $dpmv ]; then
      yaimlog INFO "Upgrading DPM schema from $fromv to $tov"
      ./$file --db-vendor Oracle --db-host ${DPM_DB_HOST} --user ${DPM_DB_USER} --pwd-file ${passfile} --dpm-db ${DPM_DB} --verbose
      if [ $? -ne 0 ]; then
        yaimlog ERROR "Error upgrading DPNS schema"
        return 1
      fi
    fi
  done

  yaimlog INFO "DPM schema up to date"

  # Update DPNS 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 DPNS schema from $fromv to $tov"
      ./$file --db-vendor Oracle --db-host ${DPM_DB_HOST} --user ${DPM_DB_USER} --pwd-file ${passfile} --dpm-db ${DPM_DB} --verbose
      if [ $? -ne 0 ]; then
        yaimlog ERROR "Error upgrading DPNS schema"
        return 1
      fi
    fi
  done

  yaimlog INFO "DPNS schema up to date"

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

  # Remove password
  rm -f ${passfile}

  # Restart
  upgrade_start_services
else
  yaimlog INFO "No upgrades needed"
fi

return 0
}

