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

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

config_DPM_oracle(){

    INSTALL_ROOT=${INSTALL_ROOT:-/opt}

    # Yaim uses ORACLE_LOCATION, while it is usually
    # ORACLE_HOME, what is set by Oracle packages
    export ORACLE_HOME=${ORACLE_HOME:-${ORACLE_LOCATION}/client}

    ####@ 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

    CONNECT_STRING=${DPM_DB_USER}/${DPM_DB_PASSWORD}@${DPM_DB_HOST}
    # tnsnames.ora is supposed to be in /home/dpmmgr/.tnsadmin/tnsnames.ora
    export TNS_ADMIN=${TNS_ADMIN:-/home/${DPMMGR_USER}/.tnsadmin}

    # check if the schema is there already, and add it if not
    #
    QUERY_FILE=`mktemp "/tmp/config_dpm_oracle.XXXXXX"`
    echo "SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON" > $QUERY_FILE
    echo "select count(*) from schema_version;" >> $QUERY_FILE
    NUM_ENTRIES=$(${ORACLE_HOME}/bin/sqlplus -S ${CONNECT_STRING} < $QUERY_FILE)
    RETVAL=$?
    /bin/rm $QUERY_FILE

    if [ $RETVAL -eq 0 ] ; then
      NUM_ENTRIES=$(echo $NUM_ENTRIES| tr -d "\t \n")
      ERROR=$(echo $NUM_ENTRIES|grep "ERROR")
      RETVAL=$?
      if [ $RETVAL -eq 0 ] ; then
        ${ORACLE_HOME}/bin/sqlplus ${CONNECT_STRING} < ${LCG_LOCATION}/share/DPM/create_dpns_tables_oracle.sql
        ${ORACLE_HOME}/bin/sqlplus ${CONNECT_STRING} < ${LCG_LOCATION}/share/DPM/create_dpm_tables_oracle.sql
      fi
    else
      yaimlog ERROR "Could not connect to Oracle using supplied connect string : ${DPM_DB_USER}/XXXXXXX@${DPM_DB_HOST}"
      echo $NUM_ENTRIES
      return 1
    fi

    DPMCONFIG="${LCG_LOCATION}/etc/DPMCONFIG"
    touch ${DPMCONFIG}
    chown ${DPMMGR_USER}:${DPMMGR_GROUP} ${DPMCONFIG}
    chmod 600 ${DPMCONFIG}
    echo "${CONNECT_STRING}" > ${DPMCONFIG}

    NSCONFIG="${LCG_LOCATION}/etc/NSCONFIG" 
    touch ${NSCONFIG}
    chown ${DPMMGR_USER}:${DPMMGR_GROUP} ${DPMCONFIG}
    chmod 600 ${DPMCONFIG}
    echo "${CONNECT_STRING}" > ${NSCONFIG}

    return 0

}
