#!/bin/sh
#********************************************************************************
# Copyright 2008-2026 Open Text.
#
# The only warranties for products and services of Open Text and its affiliates and licensors (“Open Text”)
# are as may be set forth in the express warranty statements accompanying such products and services.
# Nothing herein should be construed as constituting an additional warranty. Open Text shall not be liable
# for technical or editorial errors or omissions contained herein. The information contained herein is
# subject to change without notice.
#********************************************************************************

# -a - admin dn in ldap format (eg. cn=admin,o=novell) / -a
# -p - admin password (eg. novell) / -w
# -n - NSS Path / (eg. /media/nss/DATA or /media/nss/DATA/)
# -c - Optional to specify Container FDN where iPrint LUM object exist or should be created
# -w - Optional to specify Container FDN where apache LUM objects exist
# -l - Optional to indicate cluster / (can only be 'cluster')
# -t - Optional to indicate log level target. Log levels: error, warn, info, debug. Default log level is : info"
# 
# An example command line 'iprint_nss_relocate -a cn=admin,o=novell -n /media/nss/DATA [-l cluster]'
#

#Include logger file
. /opt/novell/iprint/bin/iprint_logger

GROUP_FILE=/etc/group
PASSWD_FILE=/etc/passwd
LUM_USER=iprint
LUM_MOBILE_USER=iprintmobile
LP_USER=lp
LUM_GROUP=iprintgrp
AP2_USER=wwwrun
AP2_GROUP=www
ERR_LOG=/var/opt/novell/log/iprint/iprint_nss_relocate.log
TMP_FILE=/tmp/iprint_nss_relocate_tmp
PSM_START_SCRIPT=/opt/novell/iprint/init.d/novell-ipsmd
IDS_START_SCRIPT=/opt/novell/iprint/init.d/novell-idsd
MOBILE_SEVER_START_SCRIPT=/var/opt/novell/iprint-tomcat/conf/novell-iprint-tomcat
MOBILE_SERVER_SERVICE_FILE=/usr/lib/systemd/system/novell-iprint-tomcat.service
ORIENTDB_SERVICE_FILE=/usr/lib/systemd/system/orientdb.service
LICENSE_SERVICE_FILE=/usr/lib/systemd/system/novell-iprint-license.service
PSM_SERVICE_FILE=/usr/lib/systemd/system/novell-ipsmd.service
IDS_SERVICE_FILE=/usr/lib/systemd/system/novell-idsd.service
START_SCRIPT_TMP=/tmp/novell-ipsmd_tmp
#NSCD_START_SCRIPT=/etc/init.d/nscd
NAMCD_START_SCRIPT=/etc/init.d/namcd
LUM_CONF_FILE=/etc/nam.conf
MOBILE_SERVER_INSTALLED=0
IPRINT_CUPS_PATH="/etc/cups"

VERSION=2022.0.0				#(OESVersion.SPVersion.incrementalIndexWithEveryCheckIn)

# To set the log level. debug:0; info:1; warn:2; error:3
LOGLEVEL="1" # Defaut set to info

#Assign error log file to logger.
LOG_FILE_PATH=$ERR_LOG

containerTypeArray=""
containerNameArray=""
containerTypeCount=0
# Indicates whether we encountered any error during the course and still continued.
continuedOnError=0
IPRINT_CUPS_SERVICE=iprint-cups.service
HOSTNAME=$(hostname)

function usage()
{
	echo "Usage :"
	echo "$0 -a <admin fqdn in ldap format> [-p <admin password>] -n <NSS Path> [-c <Specify Container FDN where iPrint LUM object exist or should be created>] [-w <Specify Container FDN where apache LUM objects exist>] [-l cluster]"
	echo "-a - Admin dn in ldap format (eg. cn=admin,o=novell)."
	echo "-p - Optional to specify Admin password (eg. novell). If not specified, need to enter interactively."
	echo "-n - NSS Path (eg. /media/nss/DATA or /media/nss/DATA/)."
	echo "-c - Optional to specify Container FDN in ldap format where iPrint LUM object exist or should be created."
	echo "-w - Optional to specify Container FDN in ldap format where apache LUM objects exist."
	echo "-l - Optional to indicate cluster. (It can only be 'cluster')."
	echo "-t - Optional to indicate log level. Log levels: error, warn, info, debug. Default log level is : info"
        echo "-s - Optional to stop all the iprint services"
	echo
	echo "e.g $0 -a cn=admin,o=novell -n /media/nss/DATA/ -c \"o=Novell,t=iPrint_Tree\" -l cluster"
	echo "    $0 -a cn=admin,o=novell -n /media/nss/DATA/ -c \"o=Novell,t=iPrint_Tree\" -w \"o=Novell,t=iPrint_Tree\" -l cluster"
	echo "    $0 -a cn=admin,o=novell -n /media/nss/DATA -c \"o=Novell,t=iPrint_Tree\""
	echo "    $0 -a cn=admin,o=novell -n /media/nss/DATA -c \"o=Novell,t=iPrint_Tree\" -w \"o=Novell,t=iPrint_Tree\""
	echo "    $0 -a cn=admin,o=novell -n /media/nss/DATA -l cluster"
	echo "    $0 -a cn=admin,o=novell -n /media/nss/DATA"
	echo "    $0 -a cn=admin,o=novell -n /media/nss/DATA -c \"o=Novell,t=iPrint_Tree\" -l cluster -t debug"
	echo "    $0 -a cn=admin,o=novell -n /media/nss/DATA -c \"o=Novell,t=iPrint_Tree\" -l cluster -t info"
        echo "    $0 -s y"
	exit -1
}

function stop_services()
{	
        systemctl stop iprint-management.service
	systemctl stop novell-iprint-tomcat.service
        systemctl stop iprint-cups.service
        systemctl stop iprint-auth.service
        systemctl stop orientdb.service
        systemctl stop novell-iprint-license.service
        systemctl stop novell-iprint-ocs.service
        systemctl stop novell-ipsmd.service
        systemctl stop novell-idsd.service
        log_info "$0 : Done with stopping of iprint services"
	exit -1
}

function lum_object_exists()
{
# $1 - namuserlist or namegrouplist
# $2 - iprint or iprintgrp
# $3 - context in ldap format (Not needed now)

	#lum_object_list=`$1 -x $3 | grep -w $2`
	lum_object_list=`$1 $2 | grep -w $2`
	if [ "$lum_object_list" == "" ]; then
		return 1;
	else
		return 0;
	fi
}

function cleanup()
{
# $1 - ipsmd_init_run_status
# $2 - idsd_init_run_status
# $3 - orientdb_server_init_run_status
# $4 - mobile_server_init_run_status
# $5 - cups_server_init_run_status
# $6 - novell_iprint_license_status
# $7 - iprint_auth_init_run_status


	if [ ! -z $1 ] && [ $1 -eq 1 ]; then
		/usr/bin/systemctl start novell-ipsmd.service
		ipsmd_start_status=$?
		sleep 1
		/usr/bin/systemctl status novell-ipsmd.service > /dev/null 2>&1
		ipsmd_running=$?
		if [ $ipsmd_running -ne 0 ] || [ $ipsmd_start_status -ne 0 ] ; then
			log_warn "iPrint ipsmd daemon could not be started, try manually."
			exit 7
		fi
	fi
	if [ ! -z $2 ] && [ $2 -eq 1 ]; then
		/usr/bin/systemctl start novell-idsd.service
		idsd_start_status=$?
		sleep 1
		/usr/bin/systemctl status novell-idsd.service > /dev/null 2>&1
		idsd_running=$?
		if [ $idsd_running -ne 0 ] || [ $idsd_start_status -ne 0 ] ; then
			log_warn "iPrint idsd daemon could not be started, try manually."
			exit 8
		fi
	fi
	if [ ! -z $3 ] && [ $3 -eq 1 ]; then
		/usr/bin/systemctl start orientdb.service
		orientdb_service_start_status=$?
		sleep 10
		/usr/bin/systemctl status orientdb.service > /dev/null 2>&1
		orientdb_server_running=$?
		if [ $orientdb_server_running -ne 0 ] || [ $orientdb_service_start_status -ne 0 ] ; then
			log_warn "Orientdb  could not be started, try manually."
			exit 9
		fi
	fi
	
	if [ ! -z $4 ] && [ $4 -eq 1 ]; then
		/usr/bin/systemctl start novell-iprint-tomcat.service
		mobile_server_start_status=$?
		sleep 10
		/usr/bin/systemctl status novell-iprint-tomcat.service > /dev/null 2>&1
		mobile_server_running=$?
		if [ $mobile_server_running -ne 0 ] || [ $mobile_server_start_status -ne 0 ] ; then
			log_warn "iPrint Mobile Server could not be started, try manually."
			exit 9
		fi
	fi

	if [ ! -z $5 ] && [ $5 -eq 1 ]; then
		/usr/bin/systemctl start $IPRINT_CUPS_SERVICE
		cups_server_start_status=$?
		sleep 1
		/usr/bin/systemctl status $IPRINT_CUPS_SERVICE > /dev/null 2>&1
		cups_server_running=$?
		if [ $cups_server_running -ne 0 ] || [ $cups_server_start_status -ne 0 ] ; then
			log_warn "iPrint CUPS Server could not be started, try manually."
			exit 9
		fi
	fi
	
    if [ ! -z $6 ] && [ $6 -eq 1 ]; then
		/usr/bin/systemctl start novell-iprint-license.service
		license_service_start_status=$?
		sleep 5
		/usr/bin/systemctl status novell-iprint-license.service > /dev/null 2>&1
		license_service_running=$?
		if [ $license_service_running -ne 0 ] || [ $license_service_start_status -ne 0 ] ; then
			log_warn "iPrint license Server could not be started, try manually."
			exit 9
		fi
	fi

	if [ ! -z $7 ] && [ $7 -eq 1 ]; then
		/usr/bin/systemctl start iprint-auth.service
		auth_service_start_status=$?
		sleep 5
		/usr/bin/systemctl status iprint-auth.service > /dev/null 2>&1
		auth_service_running=$?
		if [ $auth_service_running -ne 0 ] || [ $auth_service_start_status -ne 0 ] ; then
			log_warn "iPrint auth Server could not be started, try manually."
			exit 9
		fi
	fi
}

function read_lum_container()
{
	# $1 - server container
	# $2 - FDN separator
	# $3 - Do you want to escape dot in name
	serverContainer=$1
	separator=$2
	escapeFlag=$3
	serverContainerLenCount=0
	separatorFlag=0
	containerType=""
	containerName=""
	containerTypeFlag=0
	containerTypeCount=0
	containerNameCount=-1
	while [ $serverContainerLenCount -lt ${#serverContainer} ]
	do
        	case "${serverContainer:serverContainerLenCount:1}" in
                	$separator)
                        	if [ $separatorFlag == 1 ];then
					if [ $escapeFlag == 1 ];then
	                                	containerName="$containerName\\$separator$containerType"
					else
						containerName="$containerName$separator$containerType"
					fi
	                        fi
        	                separatorFlag=1;
                	        #Start collecting containerType
                        	containerType=""
	                        containerTypeFlag=1
        	        ;;
                	=)	
				#End collecting containerType
				containerType=`echo $containerType|tr [a-z] [A-Z]`
				containerType=`echo $containerType | sed s/"^ *"//g | sed s/" *$"//g`
                        	containerTypeArray[$containerTypeCount]=$containerType
	                        containerTypeCount=`expr $containerTypeCount + 1`
        	                if [ $containerNameCount -ne -1 ];then
					containerName=`echo $containerName | sed s/"^ *"//g | sed s/" *$"//g`
                	                containerNameArray[$containerNameCount]=$containerName
                        	fi
	                        containerNameCount=`expr $containerNameCount + 1`
        	                containerType=""
	                        containerName=""
	                        containerTypeFlag=0
        	                separatorFlag=0
	                ;;
        	        *)
                	        if [ $containerTypeFlag == 1 ];then
                        	        containerType="$containerType${serverContainer:serverContainerLenCount:1}"
	                        else
        	                        containerName="$containerName${serverContainer:serverContainerLenCount:1}"
                	        fi
	                ;;
        	esac
		serverContainerLenCount=`expr $serverContainerLenCount + 1`
	done
	if [ $containerNameCount -ne -1 ];then
		containerName=`echo $containerName | sed s/"^ *"//g | sed s/" *$"//g`
		containerNameArray[$containerNameCount]=$containerName
	fi
}

#Function to log command output if not empty
function log_cmd_out()
{
    cmd_out="$1"
    if [ "${cmd_out}" != "" ]; then
    log_info "$cmd_out"
    fi
}

# Validate inputs
#-----------------------------------Update numbers below if arguments are added/removed.----------------------#
if [ $# -ne 2 ] && [ $# -ne 4 ] && [ $# -ne 6 ] && [ $# -ne 8 ] && [ $# -ne 10 ] && [ $# -ne 12 ] && [ $# -ne 14 ]; then
	log_error "$0 : Incorrect number of arguments : $#."
	usage
fi

admin_dn="";admin_pass="";nss_path="";is_cluster="";iprint_lum_container_input="";
apache_lum_container_input="";
admin_pass_provided=0
log_level="";
while getopts a:p:n:l:c:w:t:s: o
do
	case "$o" in
		a)	admin_dn="$OPTARG";;
		p)	admin_pass="$OPTARG";admin_pass_provided=1;;
		n)	nss_path="$OPTARG";;
		l)	is_cluster="$OPTARG";;
		c)	iprint_lum_container_input="$OPTARG";;
		w)	apache_lum_container_input="$OPTARG";;
		t)	log_level="$OPTARG";;
                s)      stop_services="$OPTARG";;
		*)	usage;;
	esac
done

if [ "$stop_services" == "y" ] || [ "$stop_services" == "yes" ]; then
	log_info "$0 : Stopping the iprint services."
	stop_services
fi

#if [ "$admin_dn" == "" ] || [ "$admin_pass" == "" ] || [ "$nss_path" == "" ]; then
if [ "$admin_dn" == "" ] || [ "$nss_path" == "" ]; then
	log_error "$0 : Incorrect number of arguments."
	usage
fi

shopt -s nocasematch
if [[ "$admin_dn" != cn=* ]]; then
	log_error "$0 : $admin_dn must be in FQDN format. e.g. cn=admin,o=novell."
	usage
fi
shopt -u nocasematch

if [ "$is_cluster" != "" ] && [ "$is_cluster" != "cluster" ]; then
	log_error "$0 : Cluster argument is invalid. It can be 'cluster' only."
	usage
fi

# Add the script version to the log 
log_info "Version : $VERSION."

#Set the log level provided by user
if [ "$log_level" != "" ]; then
	if [ "$log_level" == "debug" ]; then
		LOGLEVEL=0
	elif [ "$log_level" == "info" ]; then
		LOGLEVEL=1
	elif [ "$log_level" == "warn" ]; then
		LOGLEVEL=2
	elif [ "$log_level" == "error" ]; then
		LOGLEVEL=3
	else
		log_error "$0 : debug argument is invalid. It can be 'debug/info/warn/error' only."
		usage
	fi
fi

#If nss path contains the trailing slash "/" then remove the trailing slash as nss volume validation check
# works patch without trailing slash
#Check for trailing slash
if [[ $nss_path == */ ]]; then
	#Remove the last character
	temp_nss_path=`echo ${nss_path/%?/}`
	nss_path=$temp_nss_path
fi

#--------------- Check for NSS Volume -------------#
df_entry=`df -T | grep -w "$nss_path" | grep -w nssvol` #Explicitly check second column value is nssvol
is_nss=$?
if [ $is_nss -ne 0 ]; then
	log_error "$nss_path is not an NSS Volume or is not mounted."
	exit 1
fi

#--------------- Check for available space on NSS Volume -------------#
log_debug "Checking if Mobile Sever is installed."
/usr/bin/systemctl status novell-iprint-tomcat.service | grep "No such file or directory" > /dev/null 2>&1
if [ $? -eq 0 ]; then
    MOBILE_SERVER_INSTALLED=0
else
    MOBILE_SERVER_INSTALLED=1
fi    
nssvol_size=`df -Tm | grep -w "$nss_path" | grep -w nssvol| awk -F" " '{print $5}'` #Read NSS vol size
iprint_size=0;
if [ -d /var/opt/novell/log/iprint ] && [ -d /var/opt/novell/iprint ] && [ -d /etc/opt/novell/iprint/conf ];then
	iprint_size=`du -sm /var/opt/novell/log/iprint /var/opt/novell/iprint /etc/opt/novell/iprint/conf|awk '{sum +=$1}END{print sum}'`;
fi

iprint_mobile_size=0
if [ $MOBILE_SERVER_INSTALLED -eq 1 ]; then
	#Configure orientdb if not configured
	if [ ! -f /usr/share/orientdb/bin/serverconfigured ]; then 
		sh /usr/share/orientdb/bin/config_db.sh
	fi
        
        #Fix for defect OCTCR52A708051 - systemd-tmpfiles-setup.service loaded failed
        if [ -f /usr/lib/tmpfiles.d/cups.conf ]; then
                rm /usr/lib/tmpfiles.d/cups.conf
	fi
	
	if [ -L /etc/cups ]; then
			log_debug "Old cups configuration found from OES2018SP1/OES2018SP2, migrating to iprint-cups."
			unlink /etc/cups
			/bin/cp -pr ${nss_path}/etc/cups /etc/cups
			if [ -f /etc/cups/cupsd-iprint.conf ]; then
				rm /etc/cups/cupsd-iprint.conf
			fi
			isServerRootiPrint=`grep "^ServerRoot /etc/opt/novell/iprintmobile/cups" /etc/cups/cups-files.conf`
			if [ "$isServerRootiPrint" != "" ];then
				sed -i '/iprintmobile/d' /etc/cups/cups-files.conf
			fi
			if [ ! -f "${nss_path}"/etc/cups/cupsd-iprint.conf ]; then
				/bin/cp -p "${nss_path}"/etc/cups/cupsd.conf "${nss_path}"/etc/cups/cupsd-iprint.conf
			fi
			isServerRootUpdated=`grep "^ServerRoot /etc/opt/novell/iprintmobile/cups" "${nss_path}"/etc/cups/cups-files.conf`
			if [ "$isServerRootUpdated" = "" ];then
				sed -i '/#ServerRoot/a\ServerRoot /etc/opt/novell/iprintmobile/cups' "${nss_path}"/etc/cups/cups-files.conf
			fi
			/bin/cp -rp "${nss_path}"/etc/cups/ /etc/opt/novell/iprintmobile/cups_local
	elif [ -d /etc/cups ]; then
		if [ -f /etc/opt/novell/iprintmobile/cups/cupsd-iprint.conf ]; then
			log_debug "Cups configuration is available from OES2018SP3."
		else
			log_debug "Old cups configuration found from OES2015SP1/OES2018, migrating to iprint-cups."
			/bin/cp -p "${nss_path}"/etc/cups/cupsd.conf "${nss_path}"/etc/cups/cupsd-iprint.conf
			if [ ! -f "${nss_path}"/etc/cups/cups-files.conf ]; then
				/bin/cp -p /etc/cups/cups-files.conf "${nss_path}"/etc/cups/cups-files.conf
			fi
			isServerRootUpdated=`grep "^ServerRoot /etc/opt/novell/iprintmobile/cups" "${nss_path}"/etc/cups/cups-files.conf`
			if [ "$isServerRootUpdated" = "" ];then
				sed -i '/#ServerRoot/a\ServerRoot /etc/opt/novell/iprintmobile/cups' "${nss_path}"/etc/cups/cups-files.conf	
			fi
			/bin/cp -rp "${nss_path}"/etc/cups/ /etc/opt/novell/iprintmobile/cups_local
			/usr/bin/systemctl mask cups > /dev/null 2>&1 
		fi
	fi
	
	log_debug "Building iPrint renderer zip file"
	sh /opt/novell/iprintmobile/bin/certman_oes.sh -b > /dev/null 2>&1
	
    if [ -d /var/opt/novell/log/iprintmobile ] && [ -d /var/opt/novell/iprintmobile ] && [ -d /etc/opt/novell/iprintmobile/conf ] && [ -d /var/opt/novell/orientdb ];then
	    iprint_mobile_size=`du -sm /var/opt/novell/log/iprintmobile /var/opt/novell/iprintmobile /var/opt/novell/orientdb /etc/opt/novell/iprintmobile/conf|awk '{sum +=$1}END{print sum}'`;
    fi
fi
iprint_size=$((iprint_size + iprint_mobile_size))

log_info "Available space on Shared volume : $nssvol_size M." 
log_info "Required space on Shared volume : $iprint_size M."
if [ $nssvol_size -lt $iprint_size ];then
	log_error "Enough space is not available on $nss_path."
	exit 12;
fi

#---------------- Determine Server Container --------------#
        log_debug "Reading server container using ndsconfig get ..."
        server_name=`/opt/novell/eDirectory/bin/ndsconfig get n4u.nds.server-name | grep "n4u.nds.server-name" | awk -F"=" '{print $2}'`
        if [ "$server_name" == "" ];then
                log_error "ndsconfig get output gives an invalid server name."
                exit 18
        fi

        tree_name=`/opt/novell/eDirectory/bin/ndsconfig get n4u.base.tree-name | grep "n4u.base.tree-name" | awk -F"=" '{print $2}'`
        if [ "$tree_name" == "" ];then
                log_error "ndsconfig get output gives an invalid tree name."
                exit 18
        fi

        tmp_context=`/opt/novell/eDirectory/bin/ndsconfig get n4u.nds.server-context | grep "n4u.nds.server-context" | sed -e "s/=/@/"`
        server_context=`echo "$tmp_context" | awk -F"@" '{print $2}'`
        if [ "$server_context" == "" ];then
                log_error "ndsconfig get output gives an invalid server context."
                exit 18
        fi

        dot="."
        server_name_container="CN="
        tree_name_container="T="
        server_container_from_nds="Server Name: $dot$server_name_container$server_name$dot$server_context$dot$tree_name_container$tree_name$dot"
        if [ "$server_container_from_nds" == "" ];then
                log_error "ndsconfig get output gives an invalid server container."
                exit 18
        fi
	
	#Read server object name
	#Seperating out container type and container name in containerTypeArray and containerNameArray
    read_lum_container "$server_container_from_nds" "." 0
	serverObjectName=""
	if [ $containerTypeCount -gt 0 ];then
        serverObjectName=${containerNameArray[0]}
    fi

#---------------- Determine apache lum Container --------------#
	if [ "$apache_lum_container_input" == "" ]; then
                log_debug "Container for apache lum objects is not provided by user."
                log_debug "Using server container from ndsconfig output ..."
                apache_lum_container_input="$server_container_from_nds"
                separator="."
        else
                #Check for comma at begining and end of container string
                if [ "${apache_lum_container_input:0:1}" != "," ];then
                        apache_lum_container_input=",${apache_lum_container_input}"
                fi
                if [ "${apache_lum_container_input:${#apache_lum_container_input}:1}" != "," ];then
                        apache_lum_container_input="${apache_lum_container_input},"
                fi
                separator=","
        fi
	
	#Seperating out container type and container name in containerTypeArray and containerNameArray
        read_lum_container "$apache_lum_container_input" "$separator" 0

        #Check whether TREE name is specified in FDN. Assuming last container type should be tree.
        if [ "${containerTypeArray[$containerTypeCount - 1]}" != "T" ]; then
                log_error "Specified apache lum container does not contain tree name."
                log_error "Please include tree name and rerun the script."
                exit 17
        fi

	#Constructing apache lum container name (comma separated) from containerTypeArray and containerNameArray
        apache_lum_container_comma=""
        for (( index = 0; index < $containerTypeCount;  index++ ))
        do
		if [ "${containerTypeArray[$index]}" != "CN" ] && [ "${containerTypeArray[$index]}" != "T" ];then
                        if [ "$apache_lum_container_comma" == "" ]; then
                                apache_lum_container_comma="${containerTypeArray[$index]}=${containerNameArray[$index]}"
                        else
                                apache_lum_container_comma="$apache_lum_container_comma,${containerTypeArray[$index]}=${containerNameArray[$index]}";
                        fi
                fi
        done

	read_lum_container "$apache_lum_container_input" "$separator" 1
        #Constructing apache container name (dot separated) from containerTypeArray and containerNameArray
        apache_lum_container_dot=""
        for (( index = 0; index < $containerTypeCount;  index++ ))
        do
                if [ "${containerTypeArray[$index]}" != "CN" ]; then
                        if [ "$apache_lum_container_dot" == "" ]; then
                                apache_lum_container_dot="${containerTypeArray[$index]}=${containerNameArray[$index]}"
                        else
                                apache_lum_container_dot="$apache_lum_container_dot.${containerTypeArray[$index]}=${containerNameArray[$index]}";
                        fi
                fi
        done
        #Constructing apache container name (dot separated) from containerTypeArray and containerNameArray
        apache_lum_container=""
        for (( index = 0; index < $containerTypeCount;  index++ ))
        do
                if [ "${containerTypeArray[$index]}" != "CN" ]; then
                        if [ "$apache_lum_container" == "" ]; then
                                apache_lum_container="${containerNameArray[$index]}"
                        else
                                apache_lum_container="$apache_lum_container.${containerNameArray[$index]}";
                        fi
                fi
        done

#---------------- Determine iprint lum Container --------------#
	if [ "$iprint_lum_container_input" == "" ]; then
        	log_debug "Container for iprint lum user and iprintgrp lum group is not provided by user."
        	log_debug "Using server container from ndsconfig output ..."
		iprint_lum_container_input="$server_container_from_nds"
		separator="."
	else
		#Check for comma at begining and end of container string
		if [ "${iprint_lum_container_input:0:1}" != "," ];then
			iprint_lum_container_input=",${iprint_lum_container_input}"
		fi	
		if [ "${iprint_lum_container_input:${#iprint_lum_container_input}:1}" != "," ];then
			iprint_lum_container_input="${iprint_lum_container_input},"
		fi	
		separator=","
	fi

	#Seperating out container type and container name in containerTypeArray and containerNameArray
	read_lum_container "$iprint_lum_container_input" "$separator" 0

	#Check whether TREE name is specified in FDN. Assuming last container type should be tree.
	if [ "${containerTypeArray[$containerTypeCount - 1]}" != "T" ]; then
		log_error "Specified iPrint lum container does not contain tree name."
                log_error "Please include tree name and rerun the script."
                exit 17
	fi

	#Constructing container name from containerTypeArray and containerNameArray
	iprint_lum_container=""
	for (( index = 0; index < $containerTypeCount;  index++ ))
	do
		if [ "${containerTypeArray[$index]}" != "CN" ] && [ "${containerTypeArray[$index]}" != "T" ]; then
			if [ "$iprint_lum_container" == "" ]; then
				iprint_lum_container="${containerTypeArray[$index]}=${containerNameArray[$index]}"
			else
	                        iprint_lum_container="$iprint_lum_container,${containerTypeArray[$index]}=${containerNameArray[$index]}";
        	        fi
		fi
	done

	if [ "$iprint_lum_container" != "" ];then
		#Validating container
		namuserlist -x "$iprint_lum_container"  > /dev/null 2>&1
		isContainerValid=$?
		if [ $isContainerValid -ne 0 ]; then
        	      	log_error "Container $iprint_lum_container is invalid."
			exit 17
		fi
		log_debug "iprint lum container will be $iprint_lum_container."
	fi

# Check if user provided password for "admin" user. If not ask for password.
if [ $admin_pass_provided -eq 0 ]; then
	echo -n "Password for $admin_dn : "
	stty -echo
	read admin_pass
	stty echo
	echo ""
fi

# If the psm is running shut them down
# -------------------------------------------
ipsmd_init_run_status=0 # 0 is not running
log_debug "Shutdown iPrint ipsmd daemon if running."
/usr/bin/systemctl status novell-ipsmd.service > /dev/null 2>&1
ipsmd_running=$?
if [ $ipsmd_running -eq 0 ]; then
        ipsmd_init_run_status=1 # 1 psm was running
        log_info "iPrint ipsmd daemon is running."
        log_info "Shutting down iPrint ipsmd daemon..."
        /usr/bin/systemctl stop novell-ipsmd.service > /dev/null 2>&1
        ipsmd_stop_status=$?
        /usr/bin/systemctl status novell-ipsmd.service > /dev/null 2>&1
        ipsmd_running=$?
        if [ $ipsmd_running -ne 3 ] || [ $ipsmd_stop_status -ne 0 ] ; then
                log_warn "iPrint ipsmd daemon is running and could not be stopped."
                log_warn "Stop the ipsmd and try again."
                exit -2
        fi
        log_info "iPrint ipsmd daemon is stopped."
else
        log_debug "iPrint ipsmd daemon is not running."
fi

# If the ids is running shut them down
# -------------------------------------------
idsd_init_run_status=0 # 0 ids was not running
log_debug "Shutdown iPrint idsd daemon if running."
/usr/bin/systemctl status novell-idsd.service > /dev/null 2>&1
idsd_running=$?
if [ $idsd_running -eq 0 ]; then
        idsd_init_run_status=1 # 1 ids was running
        log_info "iPrint idsd daemon is running."
        log_info "Shutting down iPrint idsd daemon..."
        /usr/bin/systemctl stop novell-idsd.service > /dev/null 2>&1
        idsd_stop_status=$?
        /usr/bin/systemctl status novell-idsd.service > /dev/null 2>&1
        idsd_running=$?
        if [ $idsd_running -ne 3 ] || [ $idsd_stop_status -ne 0 ] ; then
                log_warn "iPrint idsd daemon is running and could not be stopped." 
                log_warn "Stop the idsd and try again."
                exit -1
        fi
        log_info "iPrint idsd daemon is stopped."
else
        log_debug "iPrint idsd daemon is not running."
fi


# If the iprint-mobile server is running shut them down
# -------------------------------------------
mobile_server_init_run_status=0 # 0 mobile server was not running
mobile_server_running=-1
orientdb_server_init_run_status=0 # 0 mobile server was not running
orientdb_server_running=-1
if [ $MOBILE_SERVER_INSTALLED -eq 1 ]; then

	log_debug "Shutdown iPrint Mobile Server if running."
    /usr/bin/systemctl status novell-iprint-tomcat.service > /dev/null 2>&1
    mobile_server_running=$?
    if [ $mobile_server_running -eq 0 ]; then
        mobile_server_init_run_status=1 # 1 mobile server was running
        log_info "iPrint Mobile Server is running."
        log_info "Shutting down iPrint Mobile Server..."
        /usr/bin/systemctl stop novell-iprint-tomcat.service > /dev/null 2>&1
		mobile_server_stop_status=$?
        sleep 5
        /usr/bin/systemctl status novell-iprint-tomcat.service > /dev/null 2>&1
        mobile_server_running=$?
        if [ $mobile_server_running -ne 3 ] || [ $mobile_server_stop_status -ne 0 ] ; then
            log_warn "iPrint Mobile Server is running and could not be stopped." 
            log_warn "Stop the Mobile Server and try again."
            exit -1
        fi
        log_info "iPrint Mobile Server is stopped."
    else
        log_debug "iPrint Mobile Server is not running."
    fi

	auth_service_init_run_status=0
	auth_service_running=-1
	if [ $MOBILE_SERVER_INSTALLED -eq 1 ]; then
    	log_debug "Shutdown auth Service if running."
    	/usr/bin/systemctl status iprint-auth.service > /dev/null 2>&1
    	auth_service_running=$?
    	if [ $auth_service_running -eq 0 ]; then
        	auth_service_init_run_status=1
        	log_info "Auth Service is running."
        	log_info "Shutting down auth service Server..."
        	/usr/bin/systemctl stop iprint-auth.service > /dev/null 2>&1
			auth_service_stop_status=$?
        	sleep 5
        	/usr/bin/systemctl status iprint-auth.service> /dev/null 2>&1
        	auth_service_running=$?
        	if [ $auth_service_running -ne 3 ] || [ $auth_service_stop_status -ne 0 ] ; then
            	log_warn "Auth Service is running and could not be stopped." 
            	log_warn "Stop the auth Service and try again."
            	exit -1
        	fi
        	log_info "Auth Service is stopped."
    	else
        	log_debug "Auth Service is not running."
    	fi
	fi
	
	log_debug "Shutdown orientdb Server if running."
    /usr/bin/systemctl status orientdb.service > /dev/null 2>&1
    orientdb_server_running=$?
    if [ $orientdb_server_running -eq 0 ]; then
        orientdb_server_init_run_status=1 # 1 orientdb server was running
        log_info "orientdb is running."
        log_info "Shutting down Orientdb..."
		#shutdown orientdb
		/usr/bin/systemctl stop orientdb.service > /dev/null 2>&1
		orientdb_server_stop_status=$?
        sleep 5
        /usr/bin/systemctl status orientdb.service > /dev/null 2>&1
        orientdb_server_running=$?
        if [ $orientdb_server_running -ne 3 ] || [ $orientdb_server_stop_status -ne 0 ] ; then
            log_warn "Orientdb is running and could not be stopped." 
            log_warn "Stop the Orientdb  and try again."
            exit -1
        fi
        log_info "Orientdb is stopped."
    else
        log_debug "Orientdb is not running."
    fi
fi

cups_server_init_run_status=0 # 0 cups server was not running
cups_server_running=-1

if [ $MOBILE_SERVER_INSTALLED -eq 1 ]; then
    log_debug "Shutdown iPrint CUPS Server if running."
    /usr/bin/systemctl status $IPRINT_CUPS_SERVICE > /dev/null 2>&1
    cups_server_running=$?
    if [ $cups_server_running -eq 0 ]; then
        cups_server_init_run_status=1 # 1 mobile server was running
        log_info "iPrint CUPS Server is running."
        log_info "Shutting down iPrint CUPS Server..."
        /usr/bin/systemctl stop $IPRINT_CUPS_SERVICE > /dev/null 2>&1
		cups_server_stop_status=$?
        sleep 1
        /usr/bin/systemctl status $IPRINT_CUPS_SERVICE > /dev/null 2>&1
        cups_server_running=$?
        if [ $cups_server_running -ne 3 ] || [ $cups_server_stop_status -ne 0 ] ; then
            log_warn "iPrint CUPS Server is running and could not be stopped." 
            log_warn "Stop the iPrint CUPS Server and try again."
            exit -1
        fi
        log_info "iPrint CUPS Server is stopped."
    else
        log_debug "iPrint CUPS Server is not running."
    fi
fi

license_service_init_run_status=0
license_service_running=-1
if [ $MOBILE_SERVER_INSTALLED -eq 1 ]; then
    log_debug "Shutdown License Service if running."
    /usr/bin/systemctl status novell-iprint-license.service > /dev/null 2>&1
    license_service_running=$?
    if [ $license_service_running -eq 0 ]; then
        license_service_init_run_status=1 # 1 mobile server was running
        log_info "License Service is running."
        log_info "Shutting down license service Server..."
        /usr/bin/systemctl stop novell-iprint-license.service > /dev/null 2>&1
		license_service_stop_status=$?
        sleep 5
        /usr/bin/systemctl status novell-iprint-license.service> /dev/null 2>&1
        license_service_running=$?
        if [ $license_service_running -ne 3 ] || [ $license_service_stop_status -ne 0 ] ; then
            log_warn "License Service is running and could not be stopped." 
            log_warn "Stop the License Service and try again."
            exit -1
        fi
        log_info "License Service is stopped."
    else
        log_debug "License Service is not running."
    fi
fi

#---------------- Remove the local iprint user (if exists) -------------#
cat $PASSWD_FILE | grep "^iprint:" > /dev/null 2>&1
iprint_user_status=$?
if [ $iprint_user_status -eq 0 ]; then 			#found local iprint user, lets remove it
	log_debug "A local $LUM_USER user exists, trying to delete."
	#echo "If 'id $LUM_USER' shows $LUM_USER as a member of $LUM_GROUP then the 'userdel $LUM_USER' command can hang." >> "${ERR_LOG}"
	id "$LUM_USER" >> "${ERR_LOG}" 2>&1	
	userdel_output=`userdel $LUM_USER 2>&1`
	userdel_status=$?

	if [ $userdel_status -ne 0 ]; then
		log_warn "Local $LUM_USER user deletion failed: $userdel_status, with following error"
		log_warn "$userdel_output"
	fi

	#Let us do an id iprint
	#To refresh the NSCD cache
	id "$LUM_USER" >> "${ERR_LOG}" 2>&1	
fi

#---------------- Remove the local iprintmobile user (if exists) -------------#
if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
	cat $PASSWD_FILE | grep "^iprintmobile:" > /dev/null 2>&1
	iprintmobile_user_status=$?
	if [ $iprintmobile_user_status -eq 0 ]; then 	#found local iprintmobile user, lets remove it
		log_debug "A local $LUM_MOBILE_USER user exists, trying to delete."
		#echo "If 'id $LUM_MOBILE_USER' shows $LUM_MOBILE_USER as a member of $LUM_GROUP then the 'userdel $LUM_MOBILE_USER' command can hang." >> "${ERR_LOG}"
		id "$LUM_MOBILE_USER" >> "${ERR_LOG}" 2>&1	
		userdel_output=`userdel $LUM_MOBILE_USER 2>&1`
		userdel_status=$?

		if [ $userdel_status -ne 0 ]; then
			log_warn "Local $LUM_MOBILE_USER user deletion failed: $userdel_status, with following error"
			log_warn "$userdel_output"
		fi

		#Let us do an id iprintmobile
		#To refresh the NSCD cache
		id "$LUM_MOBILE_USER" >> "${ERR_LOG}" 2>&1	
	fi
	cat $PASSWD_FILE | grep "^lp:" > /dev/null 2>&1
	lp_user_status=$?
	if [ $lp_user_status -eq 0 ]; then 	#found local lp user, lets remove it
		log_debug "A local $LP_USER user exists, trying to delete."
		id "$LP_USER" >> "${ERR_LOG}" 2>&1	
		userdel_output=`userdel $LP_USER 2>&1`
		userdel_status=$?

		if [ $userdel_status -ne 0 ]; then
			log_warn "Local $LP_USER user deletion failed: $userdel_status, with following error"
			log_warn "$userdel_output"
		fi

		#Let us do an id lp to refresh the NSCD cache
		id "$LP_USER" >> "${ERR_LOG}" 2>&1	
	fi
fi

#---------------- Group -------------#

iprint_group_record_count=`cat $GROUP_FILE | grep "iprint" | wc -l`
if [ "$iprint_group_record_count" == 0 ]; then 		#Not found iprint or #iprint
	log_warn "iprint group not found in /etc/group. Probably iPrint is not installed. Exiting."
    if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
    	cleanup $ipsmd_init_run_status $idsd_init_run_status 
    else
    	cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
    fi    
    exit 3
else	
	isCommented=1	#found iprint or #iprint
	iprint_group_record=`cat $GROUP_FILE | grep "iprint"`
	for each_iprint_group_record in $iprint_group_record
	do
		commented_print_group=`echo $each_iprint_group_record |cut -d# -f1`
		if [ "$commented_print_group" != "" ]; then
			isCommented=0
		fi
	done

	if [ $isCommented -eq 1 ]; then
		log_debug "iprint group is already commented in $GROUP_FILE."
	else
		log_debug "iprint group exists in $GROUP_FILE."
		# Comment iprint group
		# Commenting the below section till idsd is changed to not look for group user
        # Code to comment iprint user/group in local etc file
        #rm ${GROUP_FILE}.tmp > /dev/null 2>&1
        #while [ 1 ];
        #do
        #       read myLine || break;
        #       echo $myLine | grep iprint > /dev/null 2>&1;
        #       if [ $? -eq 0 ];
        #       then echo "#$myLine" >> ${GROUP_FILE}.tmp;
        #       else echo $myLine >> ${GROUP_FILE}.tmp;
        #       fi;
        #done < $GROUP_FILE
        #mv ${GROUP_FILE} ${GROUP_FILE}.bkp;
        #mv ${GROUP_FILE}.tmp ${GROUP_FILE};
	fi

	log_debug "Look for existence of iprint LUM Group."
	lum_object_exists "namgrouplist" $LUM_GROUP 	#Searching in entire tree
	lum_group_exists=$?
	if [ $lum_group_exists -eq 0 ]; then
		log_debug "iPrint LUM group exists."

		#Check if it exists in specified container
		if [ "$iprint_lum_container" == "" ];then
			#LUM group is already existing and we dont know its container
			log_error "Please specify iPrint LUM group container with -c switch and rerun the script."
			if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
				cleanup $ipsmd_init_run_status $idsd_init_run_status
			else
				cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			fi
			exit 19;
		fi
		#Verify group if it exists in specified container
		namgrouplist -x "CN=$LUM_GROUP,$iprint_lum_container" | grep $LUM_GROUP > /dev/null 2>&1
		if [ $? -ne 0 ];then
			log_warn "iPrint LUM Group does not exist in container $iprint_lum_container."
			log_error "Please specify iPrint LUM Group container with -c switch and rerun the script."
			if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
				cleanup $ipsmd_init_run_status $idsd_init_run_status
			else
				cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			fi
			exit 20;
		fi
		log_debug "iPrint LUM Group exists in container ${iprint_lum_container}."
		log_debug "Adding iPrint LUM Group in Unix Workstation object of this server..."
		lum_group_mod_status=-1
		if [ "$serverObjectName" != "" ];then
			lum_group_mod=`namgroupmod -a "$admin_dn" -w "$admin_pass" -W "$serverObjectName" "CN=$LUM_GROUP,$iprint_lum_container"`
			lum_group_mod_status=$?
			log_debug "iprintgrp LUM Group addition status $lum_group_mod_status : $lum_group_mod"
		else
			log_debug "Do not have server workstation object name."
		fi
		
		if [ $lum_group_mod_status -ne 0 ]; then
			if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
				cleanup $ipsmd_init_run_status $idsd_init_run_status
			else
				cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			fi
			exit 21
		fi

	else
		log_debug "iPrint LUM group does not exist.  Creating..."
		if [ "$iprint_lum_container" == "" ];then
            log_error "Please specify iPrint LUM container with -c switch and rerun the script."
            if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
                cleanup $ipsmd_init_run_status $idsd_init_run_status
            else
                cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
            fi
            exit 19;
        fi
		lum_group_create_status=-1
		if [ "$serverObjectName" != "" ];then
			#Creating group without specifying group id. LUM will provide unused one.
			lum_group_create=`namgroupadd -a "$admin_dn" -w "$admin_pass" -W "$serverObjectName" -x "$iprint_lum_container" $LUM_GROUP 2>&1`
			lum_group_create_status=$?
			log_debug "iprintgrp LUM Group creation status $lum_group_create_status : $lum_group_create."		
			if [ $lum_group_create_status -ne 0 ]; then 
				#Verify if group already exists and reset the error code if true and do namgroupmod
				echo $lum_group_create | grep -i "same name exists"
				lum_group_create_status=$?
				if [ $lum_group_create_status -eq 0 ]; then
					#Note: we can optimize on the namgroupmod code and share it in both cases.
			        log_debug "Adding iPrint LUM Group in Unix Workstation object of this server..."
        			lum_group_mod_status=-1   	
	        		lum_group_mod=`namgroupmod -a "$admin_dn" -w "$admin_pass" -W "$serverObjectName" "CN=$LUM_GROUP,$iprint_lum_container"`
        			lum_group_mod_status=$?
        			log_debug "iprintgrp LUM Group addition status $lum_group_mod_status : $lum_group_mod."
				    if [ $lum_group_mod_status -ne 0 ]; then
						cleanup $ipsmd_init_run_status $idsd_init_run_status
        			    exit 21
					fi
				fi
			fi        
		else
            log_debug "Do not have server workstation object name."
		fi
		
		if [ $lum_group_create_status -ne 0 ]; then 
            if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
                cleanup $ipsmd_init_run_status $idsd_init_run_status
            else
                cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
            fi
			exit 2 
		fi
		log_debug "iprint LUM group is created in container ${iprint_lum_container}."
		sleep 5
	fi
fi

#---------------- iprint User -------------#
namuseradd_successful=0
iprint_user_already_exists=0
#iprint_user_record_count=`cat $PASSWD_FILE | grep "iprint" | wc -l`
#if [ $iprint_user_record_count -eq 0 ]; then 			#Not found iprint or #iprint
#        echo "iprint user not found in /etc/passwd.  Probably iPrint is not installed.  Exiting." | tee -a "${ERR_LOG}"
#	cleanup $ipsmd_init_run_status $idsd_init_run_status
#        exit 5
#else								#found iprint or #iprint
#	isCommented=1                                           
#        iprint_user_record=`cat $PASSWD_FILE | grep "iprint"`
#        for each_iprint_user_record in $iprint_user_record 
#        do
#                commented_print_user=`echo $each_iprint_user_record |cut -d# -f1`
#                if [ "$commented_print_user" != "" ]; then
#                        isCommented=0
#                fi
#        done

#        if [ $isCommented -eq 1 ]; then
#                echo "iprint user is already commented in $PASSWD_FILE." | tee -a "${ERR_LOG}"
#        else
#		echo "iprint user exists in $PASSWD_FILE." | tee -a "${ERR_LOG}" 
#		echo "Commenting iprint user in $PASSWD_FILE." | tee -a "${ERR_LOG}" 
#		# Code to comment iprint user/group in local etc file
#                rm ${PASSWD_FILE}.tmp > /dev/null 2>&1
#                while [ 1 ];
#                do
#                        read myLine || break;
#                        echo $myLine | grep "^iprint" > /dev/null 2>&1;
#                        if [ $? -eq 0 ];
#                        then echo "#$myLine" >> ${PASSWD_FILE}.tmp;
#                        else echo $myLine >> ${PASSWD_FILE}.tmp;
#                        fi;
#                done < $PASSWD_FILE
#                mv ${PASSWD_FILE} ${PASSWD_FILE}.bkp;
#                mv ${PASSWD_FILE}.tmp ${PASSWD_FILE};
#	fi

	log_debug "Look for existence of iprint LUM User."
	lum_object_exists "namuserlist" $LUM_USER	#Searching in entire tree
	lum_user_exists=$?
	if [ $lum_user_exists -eq 0 ]; then
		log_debug "iPrint LUM User already exists."
#		echo "Checking for rights to iPrint LUM User..."
#		rights_flag=0
#		rights_to_iprint=-1
#		if [ -d $nss_path/etc/opt/novell/iprint ];then
#			rights_to_iprint=`rights -f $nss_path/etc/opt/novell/iprint show| grep -i ".cn=iprint."| wc -l`
#			if [ $rights_to_iprint -lt 1 ];then
#				rights_flag=1
#			fi
#		fi
#		rights_to_iprint=-1
#		if [ -d $nss_path/var/opt/novell/iprint ];then
#			rights_to_iprint=`rights -f $nss_path/var/opt/novell/iprint show| grep -i ".cn=iprint."| wc -l`
#			if [ $rights_to_iprint -lt 1 ];then
#				rights_flag=1
#			fi
#		fi
#		rights_to_iprint=-1
#		if [ -d $nss_path/var/opt/novell/log/iprint ];then
#			rights_to_iprint=`rights -f $nss_path/var/opt/novell/log/iprint show| grep -i ".cn=iprint."| wc -l`
#			if [ $rights_to_iprint -lt 1 ];then
#				rights_flag=1
#			fi
#		fi
#
#		if [ $rights_flag -eq 1 ];then
#			echo "Rights to iPrint LUM User are not already assigned."
			if [ "$iprint_lum_container" == "" ];then	
				#LUM user is already existing and we dont know its container
               log_error "Please specify iPrint LUM User container with -c switch and rerun the script."
               if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
                   cleanup $ipsmd_init_run_status $idsd_init_run_status
               else
                   cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
               fi
	           exit 15;
			fi
			#Verify user if it exists in specified container
			namuserlist -x "CN=$LUM_USER,$iprint_lum_container" | grep $LUM_USER > /dev/null 2>&1
			if [ $? -ne 0 ];then
				log_warn "iPrint LUM User does not exist in container $iprint_lum_container." 
				log_error "Please specify iPrint LUM User container with -c switch and rerun the script."
               if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
                   cleanup $ipsmd_init_run_status $idsd_init_run_status
               else
                   cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
               fi
				exit 16;
			fi	
			log_debug "iPrint LUM User exists in container ${iprint_lum_container}."	
			namuseradd_successful=1
#		else
#			echo "Rights to iPrint LUM User are already assigned."
#		fi
	else
		log_debug "iPrint LUM User does not exist. Creating..."
		#Creating user without specifying uid. LUM will provide unused one.
		lum_user_create=`namuseradd -a "$admin_dn" -w "$admin_pass" -x "$iprint_lum_container" -c "Novell iPrint LUM User" -g "cn=$LUM_GROUP,$iprint_lum_container" -s /sbin/nologin $LUM_USER 2>&1`
		lum_user_create_status=$?
		log_debug "iprint LUM User creation status $lum_user_create_status : $lum_user_create."
		if [ $lum_user_create_status -ne 0 ]; then 
			#Verify if user already exists and reset the error code if true and continue.
			echo $lum_user_create | grep -i "same name exists" > /dev/null 2>&1
			lum_user_create_status=$?
			if [ $lum_user_create_status -ne 0 ]; then 
               if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
                   cleanup $ipsmd_init_run_status $idsd_init_run_status
               else
                   cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
               fi
				exit 4  
			fi
			log_debug "An iprint User already exists in container ${iprint_lum_container}."
			iprint_user_already_exists=1
		else
			log_debug "iprint LUM User is created in container ${iprint_lum_container}."
		fi
		namuseradd_successful=1
		sleep 5
	fi
#fi

#---------------- iprintmobile User -------------#
if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
	iprintmobile_namuseradd_successful=0
	iprintmobile_user_already_exists=0
	log_debug "Look for existence of iprintmobile LUM User."
	lum_object_exists "namuserlist" $LUM_MOBILE_USER	#Searching in entire tree
	lum_user_exists=$?
	if [ $lum_user_exists -eq 0 ]; then
		log_debug "iPrint Mobile LUM User already exists."
		if [ "$iprint_lum_container" == "" ];then	
			#LUM user is already existing and we dont know its container
			log_error "Please specify iPrint LUM User container with -c switch and rerun the script."
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 15;
		fi
		#Verify user if it exists in specified container
		namuserlist -x "CN=$LUM_MOBILE_USER,$iprint_lum_container" | grep $LUM_MOBILE_USER > /dev/null 2>&1
		if [ $? -ne 0 ];then
			log_warn "iPrint Mobile LUM User does not exist in container $iprint_lum_container." 
			log_error "Please specify iPrint Mobile LUM User container with -c switch and rerun the script."
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 16;
		fi	
		log_debug "iPrint Mobile LUM User exists in container ${iprint_lum_container}."	
		iprintmobile_namuseradd_successful=1
	else
		log_debug "iPrint Mobile LUM User does not exist. Creating..."
		#Creating user without specifying uid. LUM will provide unused one.
		lum_user_create=`namuseradd -a "$admin_dn" -w "$admin_pass" -x "$iprint_lum_container" -c "Novell iPrint Mobile LUM User" -g "cn=$LUM_GROUP,$iprint_lum_container" -s /bin/sh -d /var/opt/novell/iprint-tomcat $LUM_MOBILE_USER 2>&1`
		lum_user_create_status=$?
		log_debug "iprintmobile LUM User creation status $lum_user_create_status : $lum_user_create."
		if [ $lum_user_create_status -ne 0 ]; then 
			#Verify if user already exists and reset the error code if true and continue.
			echo $lum_user_create | grep -i "same name exists" > /dev/null 2>&1
			lum_user_create_status=$?
			if [ $lum_user_create_status -ne 0 ]; then 
				cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
				exit 4  
			fi
			log_debug "An iprintmobile User already exists in container ${iprint_lum_container}."
			iprint_user_already_exists=1
		else
			log_debug "iprintmobile LUM User is created in container ${iprint_lum_container}."
		fi
		iprintmobile_namuseradd_successful=1
		sleep 5
	fi

	lp_namuseradd_successful=0
	lp_user_already_exists=0
	log_debug "Look for existence of lp LUM User."
	lum_object_exists "namuserlist" $LP_USER	#Searching in entire tree
	lum_user_exists=$?
	if [ $lum_user_exists -eq 0 ]; then
		log_debug "lp LUM User already exists."
		if [ "$iprint_lum_container" == "" ];then	
			#LUM user is already existing and we dont know its container
			log_error "Please specify iPrint LUM User container with -c switch and rerun the script."
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 15;
		fi
		#Verify user if it exists in specified container
		namuserlist -x "CN=$LP_USER,$iprint_lum_container" | grep $LP_USER > /dev/null 2>&1
		if [ $? -ne 0 ];then
			log_warn "lp LUM User does not exist in container $iprint_lum_container." 
			log_error "Please specify iPrint LUM User container with -c switch and rerun the script."
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 16;
		fi	
		log_debug "lp LUM User exists in container ${iprint_lum_container}."	
		lp_namuseradd_successful=1
	else
		log_debug "lp LUM User does not exist. Creating..."
		#Creating user without specifying uid. LUM will provide unused one.
		lum_user_create=`namuseradd -a "$admin_dn" -w "$admin_pass" -x "$iprint_lum_container" -c "Novell iPrint Mobile LUM User" -g "cn=$LUM_GROUP,$iprint_lum_container" -s /sbin/nologin $LP_USER 2>&1`
		lum_user_create_status=$?
		log_debug "lp LUM User creation status $lum_user_create_status : $lum_user_create."
		if [ $lum_user_create_status -ne 0 ]; then 
			#Verify if user already exists and reset the error code if true and continue.
			echo $lum_user_create | grep -i "same name exists" > /dev/null 2>&1
			lum_user_create_status=$?
			if [ $lum_user_create_status -ne 0 ]; then 
				cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
				exit 4  
			fi
			log_debug "lp user already exists in container ${iprint_lum_container}."
			iprint_user_already_exists=1
		else
			log_debug "lp LUM User is created in container ${iprint_lum_container}."
		fi
		iprintmobile_namuseradd_successful=1
		sleep 5
	fi
fi

#--------------- Restart namcd and nscd daemons -------------#
#Since we are removing the local iprint user and doing id iprint (which clears the nscd cache), no need to restart nscd.
#if $NSCD_START_SCRIPT status > /dev/null 2>&1 ; then
#	$NSCD_START_SCRIPT restart
#fi

#Note: At this point it looks that namcd restart should be sufficient.
#In case we later find caching issues then we might want to do namconfig cache_refresh.
is_cache_only_enabled=`grep "cache-only" $LUM_CONF_FILE | cut -d '=' -f2`
if [ "$is_cache_only_enabled" == "yes" ]; then
	log_warn "The cache is being refreshed. This may take a while... "
	namconfig cache_refresh
else
	if $NAMCD_START_SCRIPT status > /dev/null 2>&1 ; then
		$NAMCD_START_SCRIPT restart
	fi
fi

#--------------- Verify if LUM user has been created successfully -------------#
id_lum_user=`/usr/bin/id "$LUM_USER" 2>&1`
id_lum_user_status=$?

if [ $id_lum_user_status -ne 0 ]; then
	log_warn "Cannot resolve the iPrint LUM user, following error occurred:"
	log_warn "$id_lum_user"
	continuedOnError=1
	#cleanup $ipsmd_init_run_status $idsd_init_run_status
	#exit 22
else
	log_debug "Successfully resolved the iPrint LUM user:"
	log_debug "$id_lum_user"
fi

if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
	id_lum_user=`/usr/bin/id "$LUM_MOBILE_USER" 2>&1`
	id_lum_user_status=$?

	if [ $id_lum_user_status -ne 0 ]; then
		log_warn "Cannot resolve the iprintmobile LUM user, following error occurred:"
		log_warn "$id_lum_user"
		continuedOnError=1
		#cleanup $ipsmd_init_run_status $idsd_init_run_status
		#exit 22
	else
		log_debug "Successfully resolved the iprintmobile LUM user:"
		log_debug "$id_lum_user"
	fi
fi

#--------------- NSS Volume - Create Directories / Add Trustees / Relocate Files -------------#
	log_info "$nss_path is a valid shared Volume, proceeding to migrate data."

	# Create necessary directories on NSS Volume.
	if [ ! -d $nss_path/etc/opt/novell/iprint/conf ]; then mkdir -p $nss_path/etc/opt/novell/iprint/conf ; fi
	if [ ! -d $nss_path/var/opt/novell/iprint ]; then mkdir -p $nss_path/var/opt/novell/iprint ; fi
	if [ ! -d $nss_path/var/opt/novell/log/iprint ]; then mkdir -p $nss_path/var/opt/novell/log/iprint ; fi
	if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
	    if [ ! -d $nss_path/etc/opt/novell/iprintmobile/conf ]; then mkdir -p $nss_path/etc/opt/novell/iprintmobile/conf ; fi
	    if [ ! -d $nss_path/var/opt/novell/iprintmobile ]; then mkdir -p $nss_path/var/opt/novell/iprintmobile ; fi
	    if [ ! -d $nss_path/var/opt/novell/log/iprintmobile ]; then mkdir -p $nss_path/var/opt/novell/log/iprintmobile ; fi
	    if [ ! -d $nss_path/var/opt/novell/iprintmobile/drivers ]; then mkdir -p $nss_path/var/opt/novell/iprintmobile/drivers ; fi
	    if [ ! -d $nss_path$IPRINT_CUPS_PATH ]; then mkdir -p $nss_path$IPRINT_CUPS_PATH ; fi
	    if [ ! -d $nss_path/var/opt/novell/orientdb/databases ]; then mkdir -p $nss_path/var/opt/novell/orientdb/databases ; fi
	    if [ ! -d $nss_path/usr/share/orientdb ]; then mkdir -p $nss_path/usr/share/orientdb; fi
	    if [ ! -d $nss_path/usr/share/orientdb/bin ]; then mkdir -p $nss_path/usr/share/orientdb/bin ; fi
		if [ ! -d $nss_path/usr/share/orientdb/config ]; then mkdir -p $nss_path/usr/share/orientdb/config ; fi
	fi
	# Form the trustee name in normal eDirectory DN form
	separator="."
	iprint_lum_container_input=`echo $iprint_lum_container_input | tr "," "."`
	read_lum_container "$iprint_lum_container_input" "$separator" 1
	iprint_lum_container=""
        for (( index = 0; index < $containerTypeCount;  index++ ))
        do
                if [ "${containerTypeArray[$index]}" != "CN" ]; then
                        if [ "$iprint_lum_container" == "" ]; then
                                iprint_lum_container="${containerNameArray[$index]}"
                        else
                                iprint_lum_container="$iprint_lum_container.${containerNameArray[$index]}";
                        fi
                fi
        done	
	if [ $namuseradd_successful -eq 1 ]; then
		trustee_dn="$LUM_USER.$iprint_lum_container"
		# Give rights to the LUM user on the NSS Volume. Do three attempts.
		log_info "Assigning rights to $trustee_dn on the NSS volume..."
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
        	do
			rights -f $nss_path/etc/opt/novell/iprint -r rwcemf trustee "$trustee_dn"
            #TODO : Is this required for mobile server?
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprint : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprint : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
            if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
                cleanup $ipsmd_init_run_status $idsd_init_run_status
            else
                cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
            fi
			exit 9
		fi

		rightsAssignment=0
                for (( index = 1; index < 4;  index++ ))
                do
			rights -f $nss_path/var/opt/novell/iprint -r rwcemf trustee "$trustee_dn"
            #TODO: Is this required for Mobile Server
                        if [ $? -eq 0 ]; then
                                rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : SUCCESS."
                                break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : failed."
                        fi
                        sleep 5
                done
                if [ $rightsAssignment -eq 0 ]; then
                        cleanup $ipsmd_init_run_status $idsd_init_run_status
                        exit 10
                fi

		rightsAssignment=0
                for (( index = 1; index < 4;  index++ ))
                do
			rights -f $nss_path/var/opt/novell/log/iprint/ -r rwcemf trustee "$trustee_dn"
                        if [ $? -eq 0 ]; then
                                rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/log/iprint : Attempt $index : SUCCESS."
                                break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/log/iprint : Attempt $index : failed."
                        fi
                        sleep 5
                done
                if [ $rightsAssignment -eq 0 ]; then
                    if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
                        cleanup $ipsmd_init_run_status $idsd_init_run_status
                    else
                        cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
                    fi
                        exit 11
                fi
	fi

#--------------- NSS Volume - Create Directories / Add Trustees / Relocate Files for iprintmobile -------------#
if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
	if [ $namuseradd_successful -eq 1 ]; then
		trustee_dn="$LUM_MOBILE_USER.$iprint_lum_container"
		# Give rights to the LUM user on the NSS Volume. Do three attempts.
		log_info "Assigning rights to $trustee_dn on the NSS volume..."
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/etc/opt/novell/iprintmobile -r rwcemf trustee "$trustee_dn"
			#TODO : Is this required for mobile server?
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprintmobile : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprintmobile : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 9
		fi
		


		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/var/opt/novell/iprintmobile -r rwcemf trustee "$trustee_dn"
			#TODO: Is this required for Mobile Server
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprintmobile : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprintmobile : Attempt $index : failed."
			fi
			sleep 5
		done

		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 10
		fi

		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/var/opt/novell/log/iprintmobile/ -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/log/iprintmobile : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/log/iprintmobile : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi
		
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/var/opt/novell/orientdb -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/orientdb : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/orientdb : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi
		
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/usr/share/orientdb/config -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/usr/share/orientdb/config : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/usr/share/orientdb/config : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi
		
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/usr/share/orientdb/bin -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/usr/share/orientdb/bin : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/usr/share/orientdb/bin : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi
		
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/var/opt/novell/iprint/ -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi
		
		trustee_dn="$LUM_GROUP.$iprint_lum_container"
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/etc/opt/novell/iprint -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprint : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprint : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi

		trustee_dn="$LP_USER.$iprint_lum_container"
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path$IPRINT_CUPS_PATH -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path$IPRINT_CUPS_PATH : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path$IPRINT_CUPS_PATH : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi
		
        trustee_dn="$LP_USER.$iprint_lum_container"
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/var/opt/novell/iprintmobile/drivers -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprintmobile/drivers : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprintmobile/drivers : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			exit 11
		fi
	fi
fi

#Verify wwwrun user if it exists in specified container
namuserlist -x "CN=$AP2_USER,$apache_lum_container_comma" | grep -i $AP2_USER > /dev/null 2>&1
if [ $? -ne 0 ];then
	log_warn "Apache User does not exist in container $apache_lum_container_comma."
	log_warn "Please assign rights to appropriate Apache LUM user (wwwrun) manually."
else
	#Check for rights to apache user
	rights_to_ap2=-1
	rights_flag=0
	trustee_dn=".$AP2_USER.$apache_lum_container_dot."
	if [ -d $nss_path/var/opt/novell/iprint ];then
		rights_to_ap2=`rights -f $nss_path/var/opt/novell/iprint show| grep -i "$trustee_dn"| wc -l`	
		if [ $rights_to_ap2 -lt 1 ];then
			rights_flag=1
		else
			log_debug "Rights to Apache User are already assigned."	
		fi
	fi
	
	if [ $rights_flag -eq 1 ];then
		# Give rights to apache user (wwwrun) to htdocs(r), mod_ipp(all), psm(r)
		log_info "Assigning rights to apache user..."
		trustee_dn="$AP2_USER.$apache_lum_container"	
		
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/var/opt/novell/iprint -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : failed."
			fi
			sleep 5
		done
		
		if [ $rightsAssignment -eq 0 ]; then
			if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
				cleanup $ipsmd_init_run_status $idsd_init_run_status
			else
				cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			fi
			exit 9
		fi
	fi
fi

if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
	# Give rights to apache user (wwwrun) to iPrint conf directory as it has to read iprintconf.properties from the iprint conf directory
	trustee_dn="$AP2_USER.$apache_lum_container"
	rightsAssignment=0
	for (( index = 1; index < 4;  index++ ))
	do
		rights -f $nss_path/etc/opt/novell/iprint -r rwcemf trustee "$trustee_dn"
		if [ $? -eq 0 ]; then
			rightsAssignment=1
			log_debug "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprint : Attempt $index : SUCCESS."
			break
		else
			log_warn "Adding $trustee_dn as trustee to $nss_path/etc/opt/novell/iprint : Attempt $index : failed."
		fi
		sleep 5
	done
	if [ $rightsAssignment -eq 0 ]; then
		if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
			cleanup $ipsmd_init_run_status $idsd_init_run_status
		else
			cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
		fi
		exit 9
	fi
fi

#Verify www group if it exists in specified container
namgrouplist -x "CN=$AP2_GROUP,$apache_lum_container_comma" | grep -i $AP2_GROUP > /dev/null 2>&1
if [ $? -ne 0 ];then
	log_warn "Apache Group does not exist in container $apache_lum_container_comma."
	log_warn "Please assign rights to appropriate Apache LUM group (www) manually."
else
	rights_to_ap2=-1
	rights_flag=0
	trustee_dn=".$AP2_GROUP.$apache_lum_container_dot."
	if [ -d $nss_path/var/opt/novell/iprint ];then
		rights_to_ap2=`rights -f $nss_path/var/opt/novell/iprint show| grep -i "$trustee_dn"| wc -l`
		if [ $rights_to_ap2 -lt 1 ];then
			rights_flag=1
		else
			log_debug "Rights to Apache group are already assigned."
		fi
	fi
	
	if [ $rights_flag -eq 1 ];then
		# Give rights to apache group (www) to htdocs(r), mod_ipp(all), psm(r)
		log_info "Assigning rights to apache group..."
		trustee_dn="$AP2_GROUP.$apache_lum_container"	
		rightsAssignment=0
		for (( index = 1; index < 4;  index++ ))
		do
			rights -f $nss_path/var/opt/novell/iprint -r rwcemf trustee "$trustee_dn"
			if [ $? -eq 0 ]; then
				rightsAssignment=1
				log_debug "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : SUCCESS."
				break
			else
				log_warn "Adding $trustee_dn as trustee to $nss_path/var/opt/novell/iprint : Attempt $index : failed."
			fi
			sleep 5
		done
		if [ $rightsAssignment -eq 0 ]; then
			if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
				cleanup $ipsmd_init_run_status $idsd_init_run_status
			else
				cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
			fi
			exit 9
		fi
	fi
fi

log_info "Migrating data to the NSS volume..."
	namconfig cache_refresh
	# Run the iprint_relocate script to backup, copy and establish symlinks
	/opt/novell/iprint/bin/iprint_relocate $nss_path $LOGLEVEL
	iprint_relocate_status=$?
	
	#Reset error log file in logger file.
	LOG_FILE_PATH=$ERR_LOG

	# TEMP : Currently the chown errors in above script are of no consequence.
	# Hardcoding for now, till we decide on alternate approach.
	#iprint_relocate_status=0

	if [ $iprint_relocate_status -ne 0 ]; then
		log_warn "Relocate failed, check /var/opt/novell/log/iprint_relocate.err"
        if [ $MOBILE_SERVER_INSTALLED == 0 ]; then
            cleanup $ipsmd_init_run_status $idsd_init_run_status
        else
            cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
        fi
		exit 6
	fi
	log_info "Relocate successful."
	chgrp $LUM_GROUP /opt/novell/iprint/bin/iprintgw
	chgrp $LUM_GROUP /opt/novell/iprint/bin/ilprsrvrd
	if [ -d /var/opt/novell/run/iprint ]; then
		chown $LUM_USER /var/opt/novell/run/iprint
	fi
	chmod u+s /opt/novell/iprint/bin/iprintgw
	chmod u+s /opt/novell/iprint/bin/ilprsrvrd

#------------- Update Yast permission file for iprint -------------#
        if [ -f /etc/permissions.d/iprint ]; then
		PERM_BACKUP_FILE="iprint.bak.`date +%Y%m%d`.$$";
                cp /etc/permissions.d/iprint /etc/permissions.d/$PERM_BACKUP_FILE
		sed s/"[ \t]root:iprint[ \t]"/"\troot:iprintgrp\t"/g /etc/permissions.d/$PERM_BACKUP_FILE > /etc/permissions.d/iprint
        fi

#------------- Rename the iprintgw.lpr file to avoid access error in case of moving a configured setup -------------#
if [ -f $nss_path/var/opt/novell/iprint/iprintgw.lpr ]; then
	mv $nss_path/var/opt/novell/iprint/iprintgw.lpr $nss_path/var/opt/novell/iprint/iprintgw.lpr.bkp
fi

#------------- Include nss in Required start for starting PSM and IDS --------#
log_info "Updating rc scripts for psm and ids..."
required_start=""
required_start=`grep Required-Start $PSM_START_SCRIPT`
if [ "$required_start" != "" ];then
	echo $required_start | grep "nss" > /dev/null 2>&1 
	if [ $? -ne 0 ];then
		sed -e "/### BEGIN INIT INFO/,/### END INIT INFO/{ s/$required_start/$required_start nss/g; }" $PSM_START_SCRIPT > $START_SCRIPT_TMP
		cp $START_SCRIPT_TMP $PSM_START_SCRIPT
	fi
fi

if [ -f $PSM_SERVICE_FILE ]; then
    log_debug "Modifying $PSM_SERVICE_FILE. Adding dependency to nss service."
	grep -iq "novell-nss\.service" $PSM_SERVICE_FILE || sed -i '/Requires=/s/$/ novell-nss.service/' $PSM_SERVICE_FILE
    grep -iq "ExecStartPre" $PSM_SERVICE_FILE || sed -i '/Type=forking/aExecStartPre=/opt/novell/iprint/bin/precheck' $PSM_SERVICE_FILE
fi

required_start=""
required_start=`grep Required-Start $IDS_START_SCRIPT`
if [ "$required_start" != "" ];then
	echo $required_start | grep "nss" > /dev/null 2>&1 
	if [ $? -ne 0 ];then
		sed -e "/### BEGIN INIT INFO/,/### END INIT INFO/{ s/$required_start/$required_start nss/g; }" $IDS_START_SCRIPT > $START_SCRIPT_TMP
		cp $START_SCRIPT_TMP $IDS_START_SCRIPT
	fi
fi

if [ -f $IDS_SERVICE_FILE ]; then
    log_debug "Modifying $IDS_SERVICE_FILE. Adding dependency to nss service."
	grep -iq "novell-nss\.service" $IDS_SERVICE_FILE || sed -i '/Requires=/s/$/ novell-nss.service/' $IDS_SERVICE_FILE
    grep -iq "ExecStartPre" $IDS_SERVICE_FILE || sed -i '/Type=forking/aExecStartPre=/opt/novell/iprint/bin/precheck' $IDS_SERVICE_FILE
fi

#set -x
#trap read debug

if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
    log_info "Updating service file for Mobile Server"
    required_start=""
    required_start=`grep Requires $MOBILE_SERVER_SERVICE_FILE`
    if [ "$required_start" != "" ];then
		echo $required_start | grep "nss" > /dev/null 2>&1 
    	if [ $? -ne 0 ];then
		sed -i '/Requires=/s/$/ novell-nss.service/' $MOBILE_SERVER_SERVICE_FILE 
	    	#sed -e "/### BEGIN INIT INFO/,/### END INIT INFO/{ s/$required_start/$required_start nss/g; }" $MOBILE_SEVER_START_SCRIPT > $START_SCRIPT_TMP
    		#cp $START_SCRIPT_TMP $MOBILE_SEVER_START_SCRIPT
	    fi
	
		# namcd service will be running when the cluster service will be up. 
		# But in case of stand alone server it will be taken care by the cluster check.
		if [ "$is_cluster" != "cluster" ]; then
			echo $required_start | grep "namcd" > /dev/null 2>&1
			if [ $? -ne 0 ];then
			sed -i '/Requires=/s/$/ namcd.service/' $MOBILE_SERVER_SERVICE_FILE 
			fi
		fi
    fi
	
	log_info "Updating service file for Orientdb"			
	grep -q "iprintgrp" $ORIENTDB_SERVICE_FILE || sed -i 's/Group=iprint/Group=iprintgrp/g' $ORIENTDB_SERVICE_FILE 
	ORIENTDB_SERVER_FILE=/usr/share/orientdb/bin/server.sh
	grep -q "allowDirectIO" $ORIENTDB_SERVER_FILE || sed -i 's/-Dtx.useLog=false/-Dtx.useLog=false -Dstorage.wal.allowDirectIO=false/' $ORIENTDB_SERVER_FILE	
	
	orientdb_after_start=""
	orientdb_after_start=`grep After= $ORIENTDB_SERVICE_FILE`
	if [ "$orientdb_after_start" != "" ];then
		echo $orientdb_after_start | grep "nss" > /dev/null 2>&1 
		if [ $? -ne 0 ];then
			sed -i '/After=/s/$/ novell-nss.service/' $ORIENTDB_SERVICE_FILE
		fi
		
		# namcd service will be running when the cluster service will be up. 
		# But in case of stand alone server it will be taken care by the cluster check.
		if [ "$is_cluster" != "cluster" ]; then
			echo $orientdb_after_start | grep "namcd" > /dev/null 2>&1 
			if [ $? -ne 0 ];then
				sed -i '/After=/s/$/ namcd.service/' $ORIENTDB_SERVICE_FILE
			fi
		fi
	fi
	
	log_info "Updating service file for License"
	license_after_start=""
	license_after_start=`grep After= $LICENSE_SERVICE_FILE`
	if [ "$license_after_start" != "" ];then
		echo $license_after_start | grep "nss" > /dev/null 2>&1
		if [ $? -ne 0 ];then
			sed -i '/After=/s/$/ novell-nss.service/' $LICENSE_SERVICE_FILE
		fi

		# namcd service will be running when the cluster service will be up. 
		# But in case of stand alone server it will be taken care by the cluster check.
		if [ "$is_cluster" != "cluster" ]; then
			echo $license_after_start | grep "namcd" > /dev/null 2>&1
			if [ $? -ne 0 ];then
				sed -i '/After=/s/$/ namcd.service/' $LICENSE_SERVICE_FILE
			fi
		fi
	fi
	
	log_info "Updating service file for iprint-cups"
	CUPS_SERVICE_FILE="/usr/lib/systemd/system/$IPRINT_CUPS_SERVICE"
	cups_after_start=""
	cups_after_start=`grep After= $CUPS_SERVICE_FILE`
	if [ "$cups_after_start" != "" ];then
		echo $cups_after_start | grep "nss" > /dev/null 2>&1 
		if [ $? -ne 0 ];then				
			sed -i '/After=/s/$/ novell-nss.service/' $CUPS_SERVICE_FILE
		fi
		
		# namcd service will be running when the cluster service will be up. 
		# But in case of stand alone server it will be taken care by the cluster check.
		if [ "$is_cluster" != "cluster" ]; then
			echo $cups_after_start | grep "namcd" > /dev/null 2>&1 
			if [ $? -ne 0 ];then
				sed -i '/After=/s/$/ namcd.service/' $CUPS_SERVICE_FILE
			fi
		fi
	fi
        
        log_info "Updating service file for iprint-ocs"
	OCS_SERVICE_FILE="/usr/lib/systemd/system/novell-iprint-ocs.service"
	ocs_after_start=""
	ocs_after_start=`grep After= $OCS_SERVICE_FILE`
	if [ "$ocs_after_start" != "" ];then
		echo $ocs_after_start | grep "nss" > /dev/null 2>&1 
		if [ $? -ne 0 ];then				
			sed -i '/After=/s/$/ novell-nss.service/' $OCS_SERVICE_FILE
		fi		
		
	fi
	
fi    

# We have modified .service files, lets reload systemctl daemon
log_info "Reloading systemd "
cmd_out="`systemctl daemon-reload`"
log_cmd_out "$cmd_out"

novell_ipsmd_flag=0
#chkconfig novell-ipsmd |grep -i on > /dev/null 2>&1
/usr/bin/systemctl is-enabled novell-ipsmd | grep -i enabled > /dev/null 2>&1
if [ $? -eq 0 ];then
	novell_ipsmd_flag=1 
fi
novell_idsd_flag=0
/usr/bin/systemctl is-enabled novell-idsd | grep -i enabled > /dev/null 2>&1
if [ $? -eq 0 ];then
	novell_idsd_flag=1 
fi

novell_mobile_server_flag=0
novell_orientdb_server_flag=0
if [ $MOBILE_SERVER_INSTALLED == 1 ]; then
    #chkconfig novell-iprint-tomcat | grep -i on > /dev/bull 2>&1
    /usr/bin/systemctl is-enabled novell-iprint-tomcat | grep -i enabled > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        novell_mobile_server_flag=1
    fi   
	
	/usr/bin/systemctl is-enabled orientdb | grep -i enabled > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        novell_orientdb_server_flag=1
    fi  
fi    

/usr/bin/systemctl disable novell-idsd > /dev/null 2>&1
/usr/bin/systemctl disable novell-ipsmd > /dev/null 2>&1
#insserv $PSM_START_SCRIPT >> "${ERR_LOG}" 2>&1
#insserv $IDS_START_SCRIPT >> "${ERR_LOG}" 2>&1
if [ $novell_ipsmd_flag -eq 1 ];then
    /usr/bin/systemctl enable novell-ipsmd > /dev/null 2>&1
fi
if [ $novell_idsd_flag -eq 1 ];then
    /usr/bin/systemctl enable novell-idsd > /dev/null 2>&1
fi

#------------- Start the daemons, if it was initially running AND not cluster. -------------#

if [ "$is_cluster" != "cluster" ]; then
    if [ $MOBILE_SERVER_INSTALLED -eq 1 ]; then
		# Bug OCTCR52A459005
		# In case of standalone nss server need to enable the mobility services so that 
		# services will get started automatically after reboot.
		log_info "Cluster option not provided, eabling boot time startup of iPrint daemons"
		/usr/bin/systemctl enable novell-iprint-ocs > /dev/null 2>&1
		/usr/bin/systemctl enable orientdb > /dev/null 2>&1
		/usr/bin/systemctl enable novell-iprint-license > /dev/null 2>&1
		/usr/bin/systemctl enable novell-iprint-tomcat > /dev/null 2>&1
		/usr/bin/systemctl enable iprint-auth > /dev/null 2>&1
		
    	cleanup $ipsmd_init_run_status $idsd_init_run_status $orientdb_server_init_run_status $mobile_server_init_run_status $cups_server_init_run_status $license_service_init_run_status $auth_service_init_run_status
    else
    	cleanup $ipsmd_init_run_status $idsd_init_run_status
    fi        
else
	#------------- In cluster setup disable startup via init scripts -------------#
	log_info "Cluster option provided, disabling local boot time startup of iPrint daemons."
    /usr/bin/systemctl disable novell-idsd
    /usr/bin/systemctl disable novell-ipsmd
    if [ $MOBILE_SERVER_INSTALLED -eq 1 ];then
        #chkconfig novell-iprint-tomcat off
        /usr/bin/systemctl disable novell-iprint-ocs > /dev/null 2>&1
		/usr/bin/systemctl disable novell-iprint-license > /dev/null 2>&1
		/usr/bin/systemctl disable novell-iprint-tomcat > /dev/null 2>&1
		/usr/bin/systemctl disable iprint-auth > /dev/null 2>&1
		/usr/bin/systemctl disable orientdb > /dev/null 2>&1
    fi

        #
fi

#Start iprint-management service for performing ds/psm object creation
systemctl start iprint-management
log_info "Starting iprint management service."

if [ $continuedOnError -ne 0 ]; then

	log_info "Summary of failed operations:"
	if [ $id_lum_user_status -ne 0 ]; then
		log_warn "Failure: Could not resolve LUM user: $LUM_USER"
		if [ $iprint_user_already_exists -eq 1 ]; then
			log_warn "Recommendation: An existing $LUM_USER user was found during creation of LUM $LUM_USER user, try running \"id $LUM_USER\" to verify the name resolution."
		else
			log_warn "Recommendation: Run \"id $LUM_USER\" to verify the name resolution."
		fi
	fi

	
	log_warn "Run the script again after correcting the above failures."
else
	log_info "Move complete, use Unified Management Console for further configuration."
fi

