#!/bin/ksh 
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/cspoc/utilities/cl_ldapsr_alldel.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/cspoc/utilities/cl_ldapsr_alldel.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

. /usr/es/sbin/cluster/cspoc/cl_federatedsec_source
fsec_init

##MAIN START##
function usage_disp
{
    echo "Usage: cl_ldapsr_alldel <ITDS server home> <ITDS server version> (if version is 6.2.0.8 then specify 62) <Server SSL key path> {0|1} (0 - to delete the kdb path, 1 - not to delete the kdb
 path)"
    echo "Example: cl_ldapsr_alldel /opt/IBM/ldap/V6.2 62 /tmp/serverkey.kdb 1"
    exit 1
}

if [[ $# != 4 ]]
then
    usage_disp
fi

if [[ ! -d "$1" ]]
then
    usage_disp
fi

LDAP_INST=ldapdb2
DB2_INST=ldapdb2
LDAP_USR=ldapdb2
SERV_KDB_PATH="$3"
TDS_SRV_VER="$2"
KDB_BIT="$4"
LOG_FILE=${FSEC_LOG_DIR}/idsidrop.$$.log

 dspmsg -s 129 cspoc.cat 122 "WARNING: This will remove ldapdb2 instance completely from the machine. It may take quite a bit of time...\n"

if echo $TDS_SRV_VER|grep "\." > /dev/null
then
	TDS_SRV_VER_CC=`echo $TDS_SRV_VER|sed 's/\.//g'`  
	[[ -z $TDS_SRV_VER_CC ]] && ret_fail "Version not found." 1 
else
	TDS_SRV_VER_CC=$TDS_SRV_VER
fi

TDS_SRV_PATH="$1"
BASE_DB2_PATH=`/usr/local/bin/db2ls -c|/usr/bin/sed '1d'|cut -f1 -d:`  
[[ -z $BASE_DB2_PATH ]] && ret_fail "DB2 path not found." 1 

#echo "Stopping ibmslapd..."
ps -eo 'args'|grep ibmslapd|grep -vw grep|grep -w $LDAP_INST > /dev/null
if [[ $? -eq 0 ]]
then
	ps -eo 'pid,args'|grep ibmslapd|grep -w $LDAP_INST|grep -vw grep|awk '{print $1}'|xargs -i kill -9 {} 
fi

#echo "Stopping ibmdiradm..." 
ps -eo 'args'|grep ibmdiradm|grep -vw grep|grep -w $LDAP_INST > /dev/null
if [[ $? -eq 0 ]]
then
	ps -eo 'pid,args'|grep ibmdiradm|grep -w $LDAP_INST|grep -vw grep|awk '{print $1}'|xargs -i kill -9 {} 
fi
	
#echo "Droping directory server instance..."
${TDS_SRV_PATH}/sbin/idsilist -I $LDAP_INST >/dev/null 2>&1
[[ $? -ne 0 ]] && ret_fail "Cannot find TDS instance" 0

${TDS_SRV_PATH}/sbin/idsidrop -I $LDAP_INST -n >> $LOG_FILE 2>&1

${TDS_SRV_PATH}/sbin/idsilist -I $LDAP_INST >/dev/null 2>&1
[[ $? -ne 32 ]] && ret_fail "Cannot continue as TDS instance not removed or not accessible. Remove Manually and then try." 1

#echo "Dropping DB2 instance..."
for X in $BASE_DB2_PATH
do
	${X}/bin/db2ilist|grep $DB2_INST > /dev/null
	if [[ $? -eq 0 ]]
	then
		. /home/${LDAP_USR}/sqllib/db2profile
		/home/${LDAP_USR}/sqllib/bin/db2 force application all >> $LOG_FILE 2>&1
		/home/${LDAP_USR}/sqllib/adm/db2stop >> $LOG_FILE 2>&1
		/home/${LDAP_USR}/sqllib/bin/db2 terminate >> $LOG_FILE 2>&1
		${X}/instance/db2idrop $DB2_INST >> $LOG_FILE 2>&1
	fi
done
#echo "Cleaning $LDAP_USR data..."
lsuser $LDAP_USR  > /dev/null 2>&1
[[ $? -eq 0 ]] && rmuser -p ldapdb2 
[[ -d /home/$LDAP_USR ]] > /dev/null 2>&1 && rm -rf /home/$LDAP_USR 
/usr/sbin/slibclean 
/usr/sbin/lsitab "ids0" >/dev/null && /usr/sbin/rmitab "ids0"
/usr/sbin/lsitab "ldapd${TDS_SRV_VER_CC}" >/dev/null && /usr/sbin/rmitab "ldapd${TDS_SRV_VER_CC}"

#echo "Removing SSL keys..."
[[ $KDB_BIT -eq 0 ]] && [ -f $SERV_KDB_PATH ] && /usr/bin/rm -r `echo "$(echo $SERV_KDB_PATH |sed s/\.kdb//g)*"`

run_on_allnode "rm -rf $FSEC_LOG_DIR" || ret_fail "Removing log directory failed." $?

exit 0

##MAIN END##
