#!/bin/bash
# Licensed Materials - Property of IBM
# 5724-I68
#
# (C) Copyright IBM Corp. 2000, 2006
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

# The purpose of this script is to migrate the database schema used by WPC prior to 5.2.1.1 to the WPC 5.2.1.1. 
# Please consult WPC support before attempting to use this script

# 
# # Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# # IBM makes no representations or warranties about the suitability of
# # this program, either express or implied, including but not limited to
# # the implied warranties of merchantability, fitness for a particular
# # purpose, or non-infringement. 
# 
#


if [ -z "$TOP" -o ! -r "$TOP" ]; then
   ${ECHO} TOP variable not defined
   exit 1
fi

${GREP} -i '^enable_ldap=true' $CCD_ETC_DIR/default/common.properties > /dev/null
MIGRATE=$?

if [ $MIGRATE -eq 0 -a $# -lt 3 ]; then
   ${ECHO} "Usage: $0 ldap_url name_attribute parent_dn"
   ${ECHO} "You have LDAP configured - you must specify all attributes"
   exit 2
fi
# Asking for Confirmation - just in case. 
${ECHO}
${ECHO} "This script will cause permanent changes to the database schema use by WPC."
${ECHO} "This change is irreversible. We urge you take a backup of the database schema before you continue."
${ECHO} "Do you want to continue ? (y/n) "
read res
if [ ! "$res" = "y" ]; then
    ${ECHO} "!! exiting"
    exit
fi
# Asking for re-confirmation - just in case. 
${ECHO} "Have you taken a back-up  of the database schema ? (y/n) "
read res1
if [ ! "$res1" = "y" ]; then
    ${ECHO} "!! exiting"
    exit
fi


${ECHO} "Schema upgrade has been initiated"

# Set variables based on the parameters passed into the script
LDAPURL=$1
NAMEATTR=$2
PARENT_DN=$3


${ECHO} "Removing enable_ldap flag from common.properties"

${CP} $CCD_ETC_DIR/default/common.properties $CCD_ETC_DIR/default/.common.properties
if [ $? -eq 0 ]; then
  ${SED} 's/^enable_ldap=/#NO_LONGER_REQUIRED# enable_ldap=/g' $CCD_ETC_DIR/default/.common.properties > $CCD_ETC_DIR/default/common.properties
  [ $? -eq 0 ] && ${RM} $CCD_ETC_DIR/default/.common.properties
fi


. ${TOP}/src/db/schema/util/set_db.sh

#case $CCD_DB in
#    oracle)
#        DBCLI="sqlplus $CONNECTION"
#        CONNECTSTRING=
#        DISCONNECTSTRING="quit;"
#        OPTERM=";"
#        ;;
#    db2)
#        DBCLI="db2"
#        CONNECTSTRING="$CONNECTION"
#        DISCONNECTSTRING="$DB_DISCONNECT"
#        OPTERM=
#        ;;
#      *)
#        ${ECHO} Unrecognised database type : $CCD_DB
#        exit 3
#        ;;
#esac


${ECHO} "Updating user table structure ..."

#$DBCLI << EOT
#$CONNECTSTRING
#alter table TSEC_SCU_USER add SCU_ENTRY_DN varchar2(200) add SCU_LDAP_SVR_URL varchar2(800) $OPTERM
#$DISCONNECTSTRING
#EOT
. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_scu_table.sql --errorfilter=yes 


${ECHO} "Generating default User authentication scripts ..."

#COMPANIES=`$DBCLI << EOT | egrep -v 'CMP_COMPANY_CODE|-----------'
#$CONNECTSTRING
#select cmp_company_code from tsec_cmp_company where cmp_company_id > $OPTERM
#$DISCONNECTSTRING
#EOT
#`

GET_COMPANIES=`. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_CMN_SCRIPTS/get_cmpcode.sql --errorfilter=yes | ${GREP} 'cmpcode%' `
COMPANIES="${GET_COMPANIES}"

for CMP_CODE_COL in $COMPANIES; 
do
    CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode%' | ${CUT} -f2 -d "%"`
    if [ "$CMP_CODE" != "" ]; then
    	${ECHO} Updating login scripts for company $CMP_CODE
    	${JAVA_RT_NOOUT} com.ibm.ccd.docstore.util.DocLoader $CMP_CODE Admin $SCRIPTS_DIR/common/ldap_scripts.xml Cp1252
    fi
done





${MV} $CCD_ETC_DIR/default/ldap_config.xml $CCD_ETC_DIR/default/ldap_config.xml-NOLONGERREQUIRED


if [ $MIGRATE -eq 1 ]; then
    ${ECHO} "LDAP not currently enabled - not making any further modifications"
    ${ECHO} "Schema Migration complete"
    exit 0
fi

${ECHO} "Updating SCU table for all LDAP users"

${TOUCH} temp.sql
${ECHO} "update TSEC_SCU_USER" > temp.sql
${ECHO} "set SCU_LDAP_ENTRY_DN='${NAMEATTR}='||SCU_USERNAME||',${PARENT_DN}'," >> temp.sql
${ECHO} "      SCU_LDAP_SVR_URL='${LDAPURL}', " >> temp.sql
${ECHO} "      SCU_FIRST_NAME=null,  " >> temp.sql
${ECHO} "      SCU_LAST_NAME=null,  " >> temp.sql
${ECHO} "      SCU_USER_PHONE=null,  " >> temp.sql
${ECHO} "      SCU_USER_ADDRESS=null,  " >> temp.sql
${ECHO} "      SCU_USER_EMAIL=null,  " >> temp.sql
${ECHO} "      SCU_USER_TITLE=null,  " >> temp.sql
${ECHO} "      SCU_USER_FAX=null  " >> temp.sql
${ECHO} "where SCU_LDAP_FLAG='Y'; " >> temp.sql
${ECHO} " commit; " >> temp.sql

#$DBCLI << EOT
#$CONNECTSTRING
#update TSEC_SCU_USER set SCU_ENTRY_DN='${NAMEATTR}='||SCU_USERNAME||',${PARENT_DN}', SCU_LDAP_SVR_URL='${LDAPURL}', SCU_FIRST_NAME=null, SCU_LAST_NAME=null, SCU_USER_PHONE=null, SCU_USER_ADDRESS=null, SCU_USER_EMAIL=null, SCU_USER_TITLE=null, SCU_USER_FAX=null where SCU_LDAP_FLAG='Y' $OPTERM
#$DISCONNECTSTRING
#EOT


. ${DB_DIR}/util/run_db_cmd.sh --file=temp.sql

${RM} temp.sql

${ECHO} "Schema Migration complete"




