#!/bin/sh  
#********************************************************************************
# Copyright 2014-2025 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.
#********************************************************************************

# iprint_relocate
File_Version="1.0.6"
script_filename=$0

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

# Before running this script the iprint resource partition 
# must be created and mounted.
# The Path to the mount point is a required parameter 
# All error messages are sent to /var/opt/novell/logs/iprint/iprint_relocate.err
# Version 1.0.2  fixed logic in dealing with iprint_g.conf file...
# Version 1.0.3  fixed local copy backups
# ------------------------------------------------------------------------------

ERR_LOG='/var/opt/novell/log/iprint_relocate.err'
COPY_FILES=false
COPY_MOBILE_FILES=false
COPY_ORIENTDB_FILES=false
TEST_FILE='/etc/opt/novell/iprint/conf/idsd-template.conf'
TEST_FILE_MOBILE='/etc/opt/novell/iprintmobile/conf/iprintmobile.conf.template'
TEST_FILE_ORIENTDB='/usr/share/orientdb/bin/serverconfigured'
# NEW_ROOT is a passed in parameter with a preceding forward slash "/"   be careful
NEW_ROOT="$1"

# Log level is passed in parameter
log_level="$2"

DATE=$(date)
CCODE=0
IPRINT_MOBILE_SERVICE_RUNNING=0
IPRINT_MOBILE_SERVICE_INSTALLED=0
IPRINT_ORIENTDB_SERVICE_RUNNING=0
IPRINT_AUTH_SERVICE_RUNNING=0
IPRINT_CUPS_SERVICE=iprint-cups.service
IPRINT_CUPS_PATH="/etc/cups"

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

# Initialize Log file.
# Log script name and script version
log_info "script file name: ${script_filename} version: ${File_Version}"
log_info "iprint_relocate version: (${File_Version}) begin"
#log_info ${DATE} 
log_info "NEW_ROOT = ${NEW_ROOT}"
log_info "Checking for mounted volume"
CCODE=mount | grep evms
log_info "Return code from mount: ${CCODE}"

log_info "Running as `whoami` "
#log_info "`whoami`"

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

# If the psm is running shut them down
# -------------------------------------------
log_info "Shutdown iPrint ipsmd daemon"
/usr/bin/systemctl stop novell-ipsmd.service  > /dev/null 2>&1
COUNT=0
sleep 2

while /bin/ps -elL | grep ipsmd  ; do
   COUNT=$((${COUNT}+1))

   if [ ${COUNT} -le  60 ] ; then
      killall -9 ipsmd
      killall -9 iprintgw
   fi

   if [ ${COUNT} -le 62 ] ; then
      log_error "iPrint ipsmd daemom is running and could not be stopped. Stop the ipsmd and try again."
      exit 1
   fi
   sleep 2
done

log_debug "ipsmd shutdown COUNT = ${COUNT}"

# If the ids is running shut them down
# -------------------------------------------
log_info "Shutdown iPrint idsd daemon"
/usr/bin/systemctl stop novell-idsd.service > /dev/null 2>&1
sleep 2

/bin/ps -elL | grep idsd 
if [ "$?" = '0' ] ; then
	log_error "iPrint idsd daemom is running and could not be stopped. Stop the idsd and try again."
	exit 1
fi

#Check if iPrint Mobile Service is already running
log_debug "Checking iPrint Mobile Service status..."
rcnovell-iprint-mobile status >> "${ERR_LOG}"
#systemctl status novell-iprint-tomcat.service | grep "No such file or directory" > /dev/null 2>&1
CHECK_MOBILE_SERVICE=$?
if [ $CHECK_MOBILE_SERVICE -ne 127 ]; then
    IPRINT_MOBILE_SERVICE_INSTALLED=1
    /usr/bin/systemctl status novell-iprint-tomcat.service  > /dev/null 2>&1	
    if [ $? -eq 0 ] ; then
	    log_info "iPrint Mobile Service is running. Stopping it to proceed."
    	IPRINT_MOBILE_SERVICE_RUNNING=1
	    #rcnovell-iprint-mobile stop 
        /usr/bin/systemctl stop novell-iprint-tomcat.service > /dev/null 2>&1
    	sleep 10 
    else
	    log_debug "iPrint Mobile Service is not running. Proceeding..."
    fi
else
    log_debug "iPrint Mobile Service is not installed."
    IPRINT_MOBILE_SERVICE_INSTALLED=0
fi

CUPS_RUNNING_STATUS=0
if [ $IPRINT_MOBILE_SERVICE_INSTALLED == 1 ]; then
	log_debug "Checking for iprint-cups status ..."
	#rccups status  >> "${ERR_LOG}"
    /usr/bin/systemctl status $IPRINT_CUPS_SERVICE > /dev/null 2>&1
	cups_status=$?
	if [ $cups_status -eq 0 ]; then
		CUPS_RUNNING_STATUS=1
		log_info "iPrint Cups is running. Stopping it to proceed."
		/usr/bin/systemctl stop $IPRINT_CUPS_SERVICE
		sleep 2
		/bin/ps -elL | grep cups
		if [ $? -eq 0 ] ; then
			log_error "iPrint Cups daemom is running and could not be stopped. Stop the iprint-cups and try again."
			exit 1
		fi
	fi
fi

IPRINT_ORIENTDB_SERVICE_RUNNING=0
if [ $IPRINT_MOBILE_SERVICE_INSTALLED == 1 ]; then
#Check if Orientdb Service is already running
log_debug "Checking orientdb Service status..."
systemctl is-active  orientdb >> "${ERR_LOG}"
#systemctl status novell-iprint-tomcat.service | grep "No such file or directory" > /dev/null 2>&1	
    if [ $? -eq 0 ] ; then
	    log_info "Orientdb Service is running. Stopping it to proceed."
    	IPRINT_ORIENTDB_SERVICE_RUNNING=1	     
        /usr/bin/systemctl stop orientdb > /dev/null 2>&1
    	sleep 10 
    else
		IPRINT_ORIENTDB_SERVICE_RUNNING=0
	    log_debug "iPrint orientdb Service is not running. Proceeding..."
    fi
fi	

IPRINT_AUTH_SERVICE_RUNNING=0
if [ $IPRINT_MOBILE_SERVICE_INSTALLED == 1 ]; then
#Check if iprint auth Service is already running
log_debug "Checking iprint auth Service status..."
systemctl status  iprint-auth >> "${ERR_LOG}"
    if [ $? -eq 0 ] ; then
	    log_info "iprint auth Service is running. Stopping it to proceed."
    	IPRINT_AUTH_SERVICE_RUNNING=1	     
        /usr/bin/systemctl stop iprint-auth > /dev/null 2>&1
    	sleep 10 
    else
		IPRINT_AUTH_SERVICE_RUNNING=0
	    log_debug "iPrint auth Service is not running. Proceeding..."
    fi
fi

# Determine if we have already copied the files.
# We are only going to copy the files once.
# Note: "/" cannot be the target path. We are already there. 
# ----------------------------------------------------------

if [ -n "${NEW_ROOT}" ] ; then
   if [ "${NEW_ROOT}" = '/' ] ;	then
	log_error "Invalid path [ ${NEW_ROOT} ] was passed."
	exit 1 
   fi

   if [ -d "${NEW_ROOT}"  ] ; then
		log_debug "Looking for test file ${NEW_ROOT}$TEST_FILE"
      if [ -f "${NEW_ROOT}$TEST_FILE"  ] ; then
			COPY_FILES=false
      else
			COPY_FILES=true
      fi

      if [ -f "${NEW_ROOT}$TEST_FILE_MOBILE" ]; then
          COPY_MOBILE_FILES=false
      else
          COPY_MOBILE_FILES=true;
      fi
      if [ -f "${NEW_ROOT}$TEST_FILE_ORIENTDB" ]; then
          COPY_ORIENTDB_FILES=false
      else
          COPY_ORIENTDB_FILES=true;
      fi   
   else
      log_error "Couldn't find specified path  [${NEW_ROOT}]"
      exit 1
   fi
else
	log_error "Path Parameter Missing"
	exit 1
fi

log_info "Copy local files to shared volume = $COPY_FILES "
if [ "$COPY_FILES" = 'true' ] ; then
	# Create the shared directory structure.
	# --------------------------------------
	/bin/mkdir -p "${NEW_ROOT}"/etc/opt/novell/iprint/conf
	/bin/mkdir -p "${NEW_ROOT}"/var/opt/novell/iprint
	/bin/mkdir -p "${NEW_ROOT}"/var/opt/novell/log/iprint
    


	# Copy the iprint files to the shared disk
	# Note: Must use the -p option to preserve file permissions
	# ----------------------------------------------------------
    log_debug "copy '/etc/opt/novell/iprint/conf/*' to shared volume "

	/bin/cp -rp /etc/opt/novell/iprint/conf/*  "${NEW_ROOT}"/etc/opt/novell/iprint/conf
        RCODE=$? 
        log_debug "RCODE=$RCODE"
        
	if [ $RCODE != 0 ] ; then
		log_error "File copy Failed: cp -rp /etc/opt/novell/iprint/conf/*   "${NEW_ROOT}"/etc/opt/novell/iprint/conf"
		exit 1
    else
	    log_info "complete! "
	fi

    log_info  "copy '/var/opt/novell/iprint/*' to shared volume "

	/bin/cp -rp /var/opt/novell/iprint/*   "${NEW_ROOT}"/var/opt/novell/iprint
        RCODE=$?
        log_debug "RCODE=$RCODE"
        
	if [ $RCODE != 0 ] ; then
		log_error "File copy Failed: cp -rp /var/opt/novell/iprint/*   "${NEW_ROOT}"/var/opt/novell/iprint"
		exit 1
    else
	    log_info "complete! "
    fi
#
# comment out for version 1.0.3  8/17/2007
#	# Rename the old dirs so the names don't
#	# conflict when we create the symlinks.
#	# --------------------------------------
#	/bin/mv  /etc/opt/novell/iprint/conf /etc/opt/novell/iprint/conf_local
#	/bin/mv  /var/opt/novell/iprint      /var/opt/novell/iprint_local
#	/bin/mv  /var/opt/novell/log/iprint  /var/opt/novell/log/iprint_local

fi
if [ "$COPY_ORIENTDB_FILES" = 'true' ] ; then
    if [ ${IPRINT_MOBILE_SERVICE_INSTALLED} == 1 ]; then 		
		 # Create orientdb related directory structure.
		/bin/mkdir -p "${NEW_ROOT}"/var/opt/novell/orientdb
		/bin/mkdir -p "${NEW_ROOT}"/usr/share/orientdb/bin
		/bin/mkdir -p "${NEW_ROOT}"/usr/share/orientdb/config
		
		log_info "copy '/var/opt/novell/orientdb/*' to shared volume "
		/bin/cp -rp /var/opt/novell/orientdb/*  "${NEW_ROOT}"/var/opt/novell/orientdb
		RCODE=$? 
		log_debug "RCODE=$RCODE"
		if [ $RCODE != 0 ] ; then
			log_error "File copy Failed: cp -rp /var/opt/novell/orientdb/*   "${NEW_ROOT}"/var/opt/novell/orientdb"
			exit 1
		else
			log_info "complete! "
		fi
		
		log_info "copy '/usr/share/orientdb/bin/serverconfigured' to shared volume "
		/bin/cp -rp /usr/share/orientdb/bin/serverconfigured  "${NEW_ROOT}"/usr/share/orientdb/bin/serverconfigured
		RCODE=$? 
		log_debug "RCODE=$RCODE"
		if [ $RCODE != 0 ] ; then
			log_error "File copy Failed: cp -rp /usr/share/orientdb/bin/serverconfigured   "${NEW_ROOT}"/usr/share/orientdb/bin/serverconfigured"
			exit 1
		else
		    log_info "complete! "
		fi	
		log_info "  copy '/usr/share/orientdb/config/orientdb-server-config.xml' to shared volume "
		/bin/cp -rp /usr/share/orientdb/config/orientdb-server-config.xml  "${NEW_ROOT}"/usr/share/orientdb/config/orientdb-server-config.xml
		RCODE=$? 
		log_debug "RCODE=$RCODE"
		if [ $RCODE != 0 ] ; then
			log_error "File copy Failed: cp -rp /usr/share/orientdb/config/orientdb-server-config.xml   "${NEW_ROOT}"/usr/share/orientdb/config/orientdb-server-config.xml"
			exit 1
		else
		    log_info "complete! "
		fi
	fi
fi	
if [ "$COPY_MOBILE_FILES" = 'true' ] ; then
    if [ ${IPRINT_MOBILE_SERVICE_INSTALLED} == 1 ]; then 
    	# Create iprintmobile related directory structure.
	    /bin/mkdir -p "${NEW_ROOT}"/var/opt/novell/iprintmobile
    	/bin/mkdir -p "${NEW_ROOT}"/etc/opt/novell/iprintmobile/conf
		/bin/mkdir -p "${NEW_ROOT}"/etc/opt/novell/iprintmobile/conf/certs
		/bin/mkdir -p "${NEW_ROOT}"/etc/opt/novell/iprint/licenseKeys
	    /bin/mkdir -p "${NEW_ROOT}"/var/opt/novell/log/iprintmobile
    	/bin/mkdir -p "${NEW_ROOT}"$IPRINT_CUPS_PATH
		
	# Copy the iprintmobile to the shared link
	    log_info "copy '/etc/opt/novell/iprintmobile/conf/*' to shared volume "
        /bin/cp -rp /etc/opt/novell/iprintmobile/conf/*  "${NEW_ROOT}"/etc/opt/novell/iprintmobile/conf
            RCODE=$? 
            log_debug "RCODE=$RCODE"
        
    	if [ $RCODE != 0 ] ; then
	    	log_error "File copy Failed: cp -rp /etc/opt/novell/iprintmobile/conf/*   "${NEW_ROOT}"/etc/opt/novell/iprintmobile/conf"
		    exit 1
        else
    	    log_info "complete! "
        fi
	
    	log_info "copy '/var/opt/novell/iprintmobile/*' to shared volume "
	    /bin/cp -rp /var/opt/novell/iprintmobile/*   "${NEW_ROOT}"/var/opt/novell/iprintmobile
        RCODE=$?
        log_debug "RCODE=$RCODE"
        
    	if [ $RCODE != 0 ] ; then
            log_error "File copy Failed: cp -rp /var/opt/novell/iprintmobile/*   "${NEW_ROOT}"/var/opt/novell/iprintmobile"
            exit 1
        else
            log_info "complete! "
        fi
    	
	    log_info "copy '/var/opt/novell/log/iprintmobile/*' to shared volume "
	    /bin/cp -rp /var/opt/novell/log/iprintmobile/*   "${NEW_ROOT}"/var/opt/novell/log/iprintmobile
        RCODE=$?
        log_debug "RCODE=$RCODE"
        
    	if [ $RCODE != 0 ] ; then
            log_error "File copy Failed: cp -rp /var/opt/novell/log/iprintmobile/*   "${NEW_ROOT}"/var/opt/novell/log/iprintmobile"
            exit 1
        else
            log_info "complete! "
        fi
    	log_info "copy '/etc/opt/novell/iprint/licenseKeys' to shared volume "
	    /bin/cp -rp /etc/opt/novell/iprint/licenseKeys/*   "${NEW_ROOT}"/etc/opt/novell/iprint/licenseKeys
        RCODE=$?
        log_debug "RCODE=$RCODE"
        
    	if [ $RCODE != 0 ] ; then
            log_error "File copy Failed: cp -rp /etc/opt/novell/iprint/licenseKeys/*   "${NEW_ROOT}"/etc/opt/novell/iprint/licenseKeys"
            exit 1
        else
            log_info "complete! "
        fi
		
		log_info "copy '/etc/opt/novell/iprintmobile/cups/' to shared volume "			
		if [ -d /etc/opt/novell/iprintmobile/cups ]; then
			/bin/cp -rp /etc/opt/novell/iprintmobile/cups/*   "${NEW_ROOT}"$IPRINT_CUPS_PATH
			RCODE=$?
			log_debug "RCODE=$RCODE"
			if [ $RCODE != 0 ] ; then
				log_error "File copy Failed: cp -rp /etc/opt/novell/iprintmobile/cups   "${NEW_ROOT}"$IPRINT_CUPS_PATH"
				exit 1
			else
				log_info "complete! "
			fi
		fi
		


    fi 
	
fi

log_info "Backup local copies "
# check to see if a backup has been done of the local directories,  if not make one..
if [ -d /etc/opt/novell/iprint/conf_local ] ; then
    log_debug "/etc/opt/novell/iprint/conf_local already exists! "
else
    log_debug "/etc/opt/novell/iprint/conf_local does not exist! "
    if [ -L /etc/opt/novell/iprint/conf ] ; then
        log_debug "/etc/opt/novell/iprint/conf is a possible symlink!  - Skip rename to conf_local "
    else
        log_debug "/etc/opt/novell/iprint/conf is a good directory!  - Rename to conf_local "
        /bin/mv /etc/opt/novell/iprint/conf /etc/opt/novell/iprint/conf_local
    fi

fi
if [ -d /var/opt/novell/iprint_local ] ; then
    log_debug "/var/opt/novell/iprint_local already exists! "
else
    log_debug "/var/opt/novell/iprint_local already does not exist! "
    if [ -L /var/opt/novell/iprint ] ; then
        log_debug "/var/opt/novell/iprint is a possible symlink!  - Skip rename to iprint_local "
    else
        log_debug "/var/opt/novell/iprint is a good directory!  - Rename to iprint_local "
        /bin/mv  /var/opt/novell/iprint /var/opt/novell/iprint_local
    fi
fi
if [ -d /var/opt/novell/log/iprint_local ] ; then
    log_debug "/var/opt/novell/log/iprint_local already exists! "
else
    log_debug "/var/opt/novell/log/iprint_local does not exist! "
    if [ -L /var/opt/novell/log/iprint ] ; then
        log_debug "/var/opt/novell/log is a possible symlink!  - Skip rename to iprint_local "
    else
        log_debug "/var/opt/novell/log/iprint is a good directory!  - Rename to iprint_local "
        /bin/mv  /var/opt/novell/log/iprint /var/opt/novell/log/iprint_local
    fi
fi
if [ -d /etc/opt/novell/iprint/licenseKeys_local ] ; then
    log_debug "/etc/opt/novell/iprint/licenseKeys_local already exists! "
else
    log_debug "/etc/opt/novell/iprint/licenseKeys_local does not exist! "
    if [ -L /etc/opt/novell/iprint/licenseKeys ] ; then
        log_debug "/etc/opt/novell/iprint/licenseKeys is a possible symlink!  - Skip rename to licenseKeys_local "
    else
        log_debug "/etc/opt/novell/iprint/licenseKeys is a good directory!  - Rename to licenseKeys_local "
        /bin/mv  /etc/opt/novell/iprint/licenseKeys /etc/opt/novell/iprint/licenseKeys_local
    fi
fi

if [ ${IPRINT_MOBILE_SERVICE_INSTALLED} == 1 ]; then
    #Backup iprintmobile related directories
    if [ -d /etc/opt/novell/iprintmobile/conf_local ] ; then
        log_debug "/etc/opt/novell/iprintmobile/conf_local already exists! "
    else
        log_debug "/etc/opt/novell/iprintmobile/conf_local does not exist! "
        if [ -L /etc/opt/novell/iprintmobile/conf ] ; then
            log_debug "/etc/opt/novell/iprintmobile/conf is a possible symlink!  - Skip rename to conf_local "
        else
            log_debug "/etc/opt/novell/iprintmobile/conf is a good directory!  - Rename to conf_local "
            /bin/mv /etc/opt/novell/iprintmobile/conf /etc/opt/novell/iprintmobile/conf_local
        fi
    fi
    
    if [ -d /var/opt/novell/iprintmobile_local ] ; then
        log_debug "/var/opt/novell/iprintmobile_local already exists! "
    else
        log_debug "/var/opt/novell/iprintmobile_local already does not exist! "
        if [ -L /var/opt/novell/iprintmobile ] ; then
            log_debug "/var/opt/novell/iprintmobile is a possible symlink!  - Skip rename to iprintmobile_local "
        else
            log_debug "/var/opt/novell/iprintmobile is a good directory!  - Rename to iprintmobile_local "
            /bin/mv  /var/opt/novell/iprintmobile /var/opt/novell/iprintmobile_local
        fi
    fi
    if [ -d /var/opt/novell/log/iprintmobile_local ] ; then
        log_debug "/var/opt/novell/log/iprintmobile_local already exists! "
    else
        log_debug "/var/opt/novell/log/iprintmobile_local does not exist! "
        if [ -L /var/opt/novell/log/iprintmobile ] ; then
            log_debug "/var/opt/novell/log is a possible symlink!  - Skip rename to iprintmobile_local "
        else
            log_debug "/var/opt/novell/log/iprintmobile is a good directory!  - Rename to iprintmobile_local "
            /bin/mv  /var/opt/novell/log/iprintmobile /var/opt/novell/log/iprintmobile_local
        fi
    fi
	
	if [ -d /etc/opt/novell/iprintmobile/cups_local ] ; then
        log_debug "/etc/opt/novell/iprintmobile/cups_local already exists! "
    else
        log_debug "/etc/opt/novell/iprintmobile/cups_local does not exist! "
        if [ -L /etc/opt/novell/iprintmobile/cups ] ; then
            log_debug "/etc/opt/novell/iprintmobile/cups is a possible symlink!  - Skip rename to cups_local "
        else
            log_debug "/etc/opt/novell/iprintmobile/cups is a good directory!  - Rename to cups_local "
			if [ -d /etc/opt/novell/iprintmobile/cups ]; then
				/bin/mv  /etc/opt/novell/iprintmobile/cups /etc/opt/novell/iprintmobile/cups_local
			fi	
        fi
    fi
	
	if [ -d /etc/opt/novell/iprint/licenseKeys_local ]; then
		log_debug "/etc/opt/novell/iprint/licenseKeys_local already exists! "
	else
		log_debug "/etc/opt/novell/iprint/licenseKeys_local doesn't exists! "
		if [ -L /etc/opt/novell/iprint/licenseKeys ]; then
			log_debug "/etc/opt/novell/iprint/licenseKeys is a possible symlink! - Skip rename."
		else
			log_debug "/etc/opt/novell/iprint/licenseKeys is a good directory!. Renaming to /etc/opt/novell/iprint/licenseKeys_local"
			if [ -d /etc/opt/novell/iprint/licenseKeys ]; then
				/bin/mv /etc/opt/novell/iprint/licenseKeys /etc/opt/novell/iprint/licenseKeys_local
			fi
		fi
	fi
	 if [ -d /var/opt/novell/orientdb_local ] ; then
        	log_debug "/var/opt/novell/orientdb_local already exists! "
   	 else
        	log_debug "/var/opt/novell/orientdb_local does not exist! "
        	if [ -L /var/opt/novell/orientdb ] ; then
            		log_debug "/var/opt/novell/orientdb is a possible symlink!  - Skip rename to orientdb_local "
        	else
            		log_debug "/var/opt/novell/orientdb is a good directory!  - Rename to orientdb_local "
            		/bin/mv  /var/opt/novell/orientdb /var/opt/novell/orientdb_local
        	fi
    	fi
	
	if [ -f /usr/share/orientdb/bin/serverconfigured_local ] ; then
        	log_debug "/usr/share/orientdb/bin/serverconfigured_local already exists! "
    	else
        	log_debug "/usr/share/orientdb/bin/serverconfigured_local does not exist! "
        	if [ -L /usr/share/orientdb/bin/serverconfigured ] ; then
            		log_debug "/usr/share/orientdb/bin/serverconfigured is a possible symlink!  - Skip rename to serverconfigured_local "
        	else
            		log_debug "/usr/share/orientdb/bin/serverconfigured is a good directory!  - Rename to serverconfigured_local "
            		/bin/mv  /usr/share/orientdb/bin/serverconfigured /usr/share/orientdb/bin/serverconfigured_local
        	fi
    	fi
	
	if [ -f /usr/share/orientdb/config/orientdb-server-config_local.xml ] ; then
        	log_debug "/usr/share/orientdb/config/orientdb-server-config_local.xml already exists! "
   	 else
        	log_debug "/usr/share/orientdb/config/orientdb-server-config_local.xml does not exist! "
        	if [ -L /usr/share/orientdb/config/orientdb-server-config.xml ] ; then
            		log_debug "/usr/share/orientdb/config/orientdb-server-config.xml is a possible symlink!  - Skip rename to orientdb-server-config_local.xml "
        	else
            		log_debug "/usr/share/orientdb/config/orientdb-server-config_local.xml is a good file!  - Rename to orientdb-server-config_local.xml "
            		/bin/mv  /usr/share/orientdb/config/orientdb-server-config.xml /usr/share/orientdb/config/orientdb-server-config_local.xml
        	fi
    	fi
fi

# Remove the old dirs / links so the names don't
# conflict when we create the new simlinks.
# ----------------------------------------------
log_info "Remove directories prior to symlink create "
/bin/rm -r /etc/opt/novell/iprint/conf
/bin/rm -r /var/opt/novell/iprint
/bin/rm -r /var/opt/novell/log/iprint

if [ ${IPRINT_MOBILE_SERVICE_INSTALLED} == 1 ]; then
    log_debug "Remove iprintmobile related directories" 
    /bin/rm -r /etc/opt/novell/iprintmobile/conf
    /bin/rm -r /var/opt/novell/iprintmobile
    /bin/rm -r /var/opt/novell/orientdb
    /bin/rm -r /var/opt/novell/log/iprintmobile
	/bin/rm -r /etc/opt/novell/iprint/licenseKeys
	/bin/rm -r /etc/opt/novell/iprintmobile/cups
fi

# Create the symlinks to the shared cluster disk.
# Also set permissions after the links are created.
# -------------------------------------------------
log_info "Create symlinks to shared volume "

if [ -L /var/opt/novell/iprint ]; then
   log_debug "/var/opt/novell/iprint is already a symlink."
else
   /bin/ln -s "${NEW_ROOT}"/var/opt/novell/iprint /var/opt/novell/iprint
   if [ $? != 0 ] ; then
      log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/var/opt/novell/iprint /var/opt/novell/iprint"
      exit 1
   fi
fi

if [ -L /etc/opt/novell/iprint/conf ]; then
   log_debug "/etc/opt/novell/iprint/conf is already a symlink."
else
   /bin/ln -s "${NEW_ROOT}"/etc/opt/novell/iprint/conf /etc/opt/novell/iprint/conf
   if [ $? != 0 ] ; then
      log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/etc/opt/novell/iprint/conf /etc/opt/novell/iprint/conf"
      exit 1
   fi
fi

if [ -L /var/opt/novell/log/iprint ]; then
   log_debug "/var/opt/novell/log/iprint is already a symlink."
else
   /bin/ln -s "${NEW_ROOT}"/var/opt/novell/log/iprint /var/opt/novell/log/iprint
   if [ $? != 0 ] ; then
      log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/var/opt/novell/log/iprint /var/opt/novell/log/iprint"
      exit 1
   fi
fi

if [ ${IPRINT_MOBILE_SERVICE_INSTALLED} == 1 ]; then
    #Create symlinks to iprintmobile directories
    log_info "Create iprintmobile symlinks to shared volume "
    if [ -L /var/opt/novell/iprintmobile ]; then
       log_debug "/var/opt/novell/iprintmobile is already a symlink." | tee- a "${ERR_LOG}"
    else
       /bin/ln -s "${NEW_ROOT}"/var/opt/novell/iprintmobile /var/opt/novell/iprintmobile
       if [ $? != 0 ] ; then
          log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/var/opt/novell/iprintmobile /var/opt/novell/iprintmobile"
          #exit 1
       fi
    fi

    if [ -L /etc/opt/novell/iprintmobile/conf ]; then
       log_debug "/etc/opt/novell/iprintmobile/conf is already a symlink."
    else
       /bin/ln -s "${NEW_ROOT}"/etc/opt/novell/iprintmobile/conf /etc/opt/novell/iprintmobile/conf
       if [ $? != 0 ] ; then
          log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/etc/opt/novell/iprintmobile/conf /etc/opt/novell/iprintmobile/conf"
          #exit 1
       fi
    fi
    
    if [ -L /var/opt/novell/log/iprintmobile ]; then
       log_debug "/var/opt/novell/log/iprintmobile is already a symlink. "
    else  
       /bin/ln -s "${NEW_ROOT}"/var/opt/novell/log/iprintmobile /var/opt/novell/log/iprintmobile
       if [ $? != 0 ] ; then
          log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/var/opt/novell/log/iprintmobile /var/opt/novell/log/iprintmobile"
          #exit 1
       fi
    fi

    if [ -L /etc/opt/novell/iprint/licenseKeys ]; then
       log_debug "/etc/opt/novell/iprint/licenseKeys is already a symlink."
    else
	   /bin/ln -s "${NEW_ROOT}"/etc/opt/novell/iprint/licenseKeys /etc/opt/novell/iprint/licenseKeys
       if [ $? != 0 ] ; then
          log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/etc/opt/novell/iprint/licenseKeys /etc/opt/novell/iprint/licenseKeys"
          #exit 1
       fi
    fi

    if [ -L /etc/opt/novell/iprintmobile/cups/ ]; then
	   log_debug "/etc/opt/novell/iprintmobile/cups is already a symlink."
    else	
     	/bin/ln -s "${NEW_ROOT}"$IPRINT_CUPS_PATH /etc/opt/novell/iprintmobile/cups
	    if [ $? != 0 ] ; then
      		log_error "Create Symlink Failed: ln -s ${NEW_ROOT}$IPRINT_CUPS_PATH /etc/opt/novell/iprintmobile/cups/"
	       #exit 1
    	fi
     fi	
	
	
    #Create symlinks to orientdb directories
    log_info "Create orientdb symlinks to shared volume "
    if [ -L /var/opt/novell/orientdb ]; then
       log_debug "/var/opt/novell/orientdb is already a symlink."
    else
       /bin/ln -s "${NEW_ROOT}"/var/opt/novell/orientdb /var/opt/novell/orientdb
       if [ $? != 0 ] ; then
          log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/var/opt/novell/orientdb /var/opt/novell/orientdb"
          exit 1
       fi
    fi	
	
	if [ -L /usr/share/orientdb/bin/serverconfigured ]; then
       log_debug "/usr/share/orientdb/bin/serverconfigured is already a symlink."
    else
		if [ -f /usr/share/orientdb/bin/serverconfigured ];then
			mv /usr/share/orientdb/bin/serverconfigured /usr/share/orientdb/bin/serverconfigured_local
		fi
       /bin/ln -s "${NEW_ROOT}"/usr/share/orientdb/bin/serverconfigured /usr/share/orientdb/bin/serverconfigured
       if [ $? != 0 ] ; then
          log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/usr/share/orientdb/bin/serverconfigured /usr/share/orientdb/bin/serverconfigured"
          exit 1
       fi
    fi
	
	if [ -L /usr/share/orientdb/config/orientdb-server-config.xml ]; then
       log_debug "/usr/share/orientdb/config/orientdb-server-config.xml is already a symlink."
    else
		if [ -f /usr/share/orientdb/config/orientdb-server-config.xml ]; then
			mv /usr/share/orientdb/config/orientdb-server-config.xml /usr/share/orientdb/config/orientdb-server-config_local.xml
		fi
       /bin/ln -s "${NEW_ROOT}"/usr/share/orientdb/config/orientdb-server-config.xml /usr/share/orientdb/config/orientdb-server-config.xml
       if [ $? != 0 ] ; then
          log_error "Create Symlink Failed: ln -s ${NEW_ROOT}/usr/share/orientdb/config/orientdb-server-config.xml /usr/share/orientdb/config/orientdb-server-config.xml"
          exit 1
       fi
    fi
fi    

# Update the UIDs & GIDs on the files on the shared disk.
# This has to be done because the UIDs & GIDs are not guarenteed
#   to be the same on different cluster nodes.
# ---------------------------------------------------------------
log_info "Set UID:GID on shared directories "

/bin/chown -R root:root "${NEW_ROOT}/etc/opt/novell/iprint/conf" || true
if [ $? != 0 ] ; then
   log_error "Setting Ownerships Failed: chown -R root:root /etc/opt/novell/iprint/conf. error $?"
   exit 1
fi
/bin/chown -R root:iprint "${NEW_ROOT}/var/opt/novell/iprint" || true
if [ $? != 0 ] ; then
   log_error "Setting Ownerships Failed: chown -R root:iprint /var/opt/novell/iprint"
   exit 1
fi
/bin/chmod g+w "${NEW_ROOT}/var/opt/novell/iprint" || true
if [ $? != 0 ] ; then
   log_error "Setting Permissions Failed: chmod g+w /var/opt/novell/iprint"
   exit 1
fi
/bin/chown -R root:iprint "${NEW_ROOT}/var/opt/novell/log/iprint" || true
if [ $? != 0 ] ; then
   log_error "Setting Ownerships Failed: chown -R root:iprint /var/opt/novell/log/iprint"
   exit 1
fi
/bin/chmod g+ws "${NEW_ROOT}/var/opt/novell/log/iprint" || true
if [ $? != 0 ] ; then
   log_error "Setting Permissions Failed: chmod g+ws /var/opt/novell/log/iprint"
   exit 1
fi

if [ ${IPRINT_MOBILE_SERVICE_INSTALLED} == 1 ]; then
    #Change owner/permissions for iprintmobile related directories
    /bin/chown -R iprintmobile:iprintgrp "${NEW_ROOT}/var/opt/novell/iprintmobile"
    if [ $? != 0 ] ; then
       log_error "Setting Ownerships Failed: chown -R iprintmobile:iprintgrp /var/opt/novell/iprintmobile"
       exit 1
    fi
    /bin/chmod 775 "${NEW_ROOT}/var/opt/novell/iprintmobile"
    if [ $? != 0 ] ; then
       log_error "Setting Permissions Failed: chmod 775 /var/opt/novell/iprintmobile"
       exit 1
    fi
	
	/bin/chown -R iprintmobile:iprintgrp "${NEW_ROOT}/var/opt/novell/orientdb"
    if [ $? != 0 ] ; then
       log_error "Setting Ownerships Failed: chown -R iprintmobile:iprintgrp /var/opt/novell/orientdb"
       exit 1
    fi
	
    /bin/chmod 775 "${NEW_ROOT}/var/opt/novell/orientdb"
    if [ $? != 0 ] ; then
       log_error "Setting Permissions Failed: chmod 775 /var/opt/novell/orientdb"
       exit 1
    fi
    
    /bin/chown -R iprintmobile:iprintgrp "${NEW_ROOT}/var/opt/novell/log/iprintmobile"
    if [ $? != 0 ] ; then
       log_error "Setting Ownerships Failed: chown -R iprintmobile:iprintgrp /var/opt/novell/log/iprintmobile"
       exit 1
    fi
    
    /bin/chown -R iprintmobile:iprintgrp "${NEW_ROOT}/var/opt/novell/log/iprintmobile/"
    if [ $? != 0 ] ; then
       log_error "Setting Ownerships Failed: chown -R iprintmobile:iprintgrp /var/opt/novell/log/iprintmobile/*"
       exit 1
    fi
	
	/bin/chown -R iprintmobile:iprintgrp "${NEW_ROOT}/etc/opt/novell/iprint/licenseKeys"
    if [ $? != 0 ] ; then
       log_error "Setting Ownerships Failed: chown -R iprintmobile:iprintgrp /etc/opt/novell/iprint/licenseKeys"
       exit 1
    fi
    
    /bin/chmod g+ws "${NEW_ROOT}/var/opt/novell/log/iprintmobile"
    if [ $? != 0 ] ; then
       log_error "Setting Permissions Failed: chmod g+ws /var/opt/novell/log/iprintmobile"
       exit 1
    fi
	
	# /bin/chown -R iprintmobile "${NEW_ROOT}/var/opt/novell/iprint-tomcat/work"
	# if [ $? != 0 ] ; then
       # echo "Setting Permissions Failed: /bin/chown -R iprintmobile /var/opt/novell/iprint-tomcat/work"
       # exit 1
    # fi
	
	/bin/chown -R iprintmobile /var/opt/novell/log/release-portal/
	if [ $? != 0 ] ; then
        log_error "Setting Permissions Failed: chown -R iprintmobile /var/opt/novell/log/release-portal/"
        #exit 1
        fi
	
	/bin/chown -R iprintmobile /var/opt/novell/iprint-tomcat/
	if [ $? != 0 ] ; then
        log_error "Setting Permissions Failed: chown -R iprintmobile /var/opt/novell/iprint-tomcat/"
        #exit 1
    fi
	
	/bin/chown iprintmobile "${NEW_ROOT}/etc/opt/novell/iprint/conf/iprintconf.properties"
	if [ $? != 0 ] ; then
        log_error "Setting Permissions Failed: chown iprintmobile /etc/opt/novell/iprint/conf/iprintconf.properties"
        #exit 1
    fi
	
	/bin/chmod +x /opt/novell/iprint/bin/ipsmri
	if [ $? != 0 ]; then
		log_error "Setting permission failed: /bin/chmod +x /opt/novell/iprint/bin/ipsmri"
		#exit 1
	fi
	
	/bin/chown -R iprintmobile:iprintgrp /usr/share/orientdb/
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprintmobile:iprintgrp /usr/share/orientdb"
		#exit 1
	fi	
	/bin/chown -R iprintmobile:iprintgrp "${NEW_ROOT}"/usr/share/orientdb/bin/serverconfigured
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprintmobile:iprintgrp /usr/share/orientdb/bin/serverconfigured"
		#exit 1
	fi
	
	/bin/chown -R iprintmobile:iprintgrp "${NEW_ROOT}"/usr/share/orientdb/config/orientdb-server-config.xml
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprintmobile:iprintgrp /usr/share/orientdb/config/orientdb-server-config.xml"
		#exit 1
	fi
	
	/bin/chown -R iprint:iprint /etc/opt/novell/iprintauth/
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprint:iprint /etc/opt/novell/iprintauth/"
		#exit 1
	fi
        
        /bin/chown iprint:iprint /opt/novell/iprintauth/bin/*.sh
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown iprint:iprint /opt/novell/iprintauth/bin/*.sh"
		#exit 1
	fi
        
        /bin/chown iprint:iprint /opt/novell/iprintauth/lib/*.jar
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown iprint:iprint /opt/novell/iprintauth/lib/*.jar"
		#exit 1
	fi
        
        /bin/chown -R iprint:iprint /var/opt/novell/iprintauth/
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprint:iprint /var/opt/novell/iprintauth/"
		#exit 1
	fi
	
	#Resetting the iprint_auth_apache.conf to root:root
	/bin/chown root:root /etc/opt/novell/iprintauth/conf/iprint_auth_apache.conf
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R root:root /etc/opt/novell/iprintauth/conf/iprint_auth_apache.conf"
		#exit 1
	fi
	
	/bin/chown -R iprint:iprint /var/opt/novell/log/iprintauth
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprint:iprint /var/opt/novell/log/iprintauth"
		#exit 1
	fi
	
	keyviewFile="/tmp/keyviewOutput.txt"
	if [ -f $keyviewFile ]; then
		/bin/chown iprintmobile:iprint $keyviewFile
		if [ $? != 0 ]; then
			log_error "Setting permission failed: chown iprintmobile:iprint /tmp/keyviewOutput.txt"
			#exit 1
		fi
	fi
        
        /bin/chown -R iprint:iprint /etc/opt/novell/iprint-management/conf
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprint:iprint /etc/opt/novell/iprint-management/conf"
		#exit 1
	fi
        
        /bin/chown iprint:iprint /opt/novell/iprint-management/lib/*.jar
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown iprint:iprint /opt/novell/iprint-management/lib/*.jar"
		#exit 1
	fi
        
        /bin/chown -R iprint:iprint /var/opt/novell/log/iprint-management
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprint:iprint /var/opt/novell/log/iprint-management"
		#exit 1
	fi
        
        /bin/chown -R iprint:iprint /var/opt/novell/iprint-management/
	if [ $? != 0 ]; then
		log_error "Setting permission failed: chown -R iprint:iprint /var/opt/novell/iprint-management/"
		#exit 1
	fi
fi    
#  The following directories are modifed in the ncs_iprint_svc_start script
#   "${NEW_ROOT}/var/opt/novell/iprint/mod_ipp" 
#   "${NEW_ROOT}/var/opt/novell/iprint/*.psm"


# Update the apache2 conf files to know about the shared disk
# If the iprint apache2 conf file backup does not exist then make a backup file.
# -------------------------------------------------------------------------------
## Changed 7/11/07
log_info "Check iprint_g.conf apache conf file local backup "

if [ -f /etc/opt/novell/iprint/httpd/conf/iprint_g-share.conf  ] ; then
    log_debug "/etc/opt/novell/iprint/httpd/conf/iprint_g-share.conf already exists! "
else
    log_debug "/etc/opt/novell/iprint/httpd/conf/iprint_g-share.conf does not exist! "
    /bin/cp -p /etc/opt/novell/iprint/httpd/conf/iprint_g.conf /etc/opt/novell/iprint/httpd/conf/iprint_g-share.conf
    if [ $? != 0 ] ; then
        log_warn "iprint_g-share.conf backup failed! "
    else
        log_info "iprint_g-share.conf backup file success! "
    fi
fi

# Update the iprint apache2 conf file. 
# ( We will do this every time the script is run. )
# ------------------------------------------------- 
log_info "Fix iprint_g.conf file with correct directive paths "

log_debug "fix 'Alias' directive for ippdocs as ${NEW_ROOT}/var/opt/novell/iprint/htdocs "

/usr/bin/sed -i -e "s@^[[:space:]]*Alias[[:space:]]*/ippdocs/.*@Alias /ippdocs/ \"${NEW_ROOT}/var/opt/novell/iprint/htdocs/\"@" /etc/opt/novell/iprint/httpd/conf/iprint_g.conf  

if [ $? != 0 ]; then
   log_warn "Fix 'Alias' directive for ippdocs is failed!"
else
   log_debug "Fix 'Alias' directive for ippdocs is success!"
fi
log_debug "fix '<Directory>' directive for htdocs as ${NEW_ROOT}/var/opt/novell/iprint/htdocs"


/usr/bin/sed -i -e "s@^[[:space:]]*<Directory[[:space:]].*/var/opt/novell/iprint/htdocs>@<Directory ${NEW_ROOT}/var/opt/novell/iprint/htdocs>@" /etc/opt/novell/iprint/httpd/conf/iprint_g.conf 
if [ $? != 0 ]; then
   log_warn "Fix '<Directory>' directive for ippdocs is failed!"
else
   log_debug "Fix '<Directory>' directive for ippdocs is success!"
fi

## Commented out 7/11/07
###if [ ! -f /etc/opt/novell/iprint/httpd/conf/iprint_g-share.conf  ] ; then
##grep "${NEW_ROOT}/var/opt/novell/iprint/htdocs/"  iprint_g.conf
##if [ "$?" == '0' ] ; then
##   /bin/cp -p /etc/opt/novell/iprint/httpd/conf/iprint_g.conf /etc/opt/novell/iprint/httpd/conf/iprint_g-share.conf
##   CCODE=$?
##fi
##
### Update the iprint apache2 conf file. 
### ( We will do this every time the script is run. )
### ------------------------------------------------- 
##if [ "$CCODE" == '0' ]; then
##   /usr/bin/sed -i -e "s@\([\" ]\)\(/var/opt/novell/iprint/htdocs\)@\1${NEW_ROOT}\2@g" /etc/opt/novell/iprint/httpd/conf/iprint_g.conf 
##fi
## Commented out 7/11/07

IPSMD_CONF="/etc/opt/novell/iprint/conf/ipsmd.conf"
print_manager=`ls -al ${IPSMD_CONF} | cut -d/ -f13 | sed 's/.ipsmd.conf//'`

log_info "Changing the owner of psmdb file"
chown iprint /var/opt/novell/iprint/${print_manager}.psm/psmdb.dat

log_info "Allow apache to access the mod_ipp folder"
chgrp www  /var/opt/novell/iprint/mod_ipp

log_info "Changing the owner and group of drivers"
#Not always this directory present. So added check
if [ -d /var/opt/novell/iprint/mod_ipp/drivers ]; then
	chown -R wwwrun:www /var/opt/novell/iprint/mod_ipp/drivers
fi

# We need to always restart apache because rights to files have changed.

log_info "Restarting  apache "
cmd_out="`/usr/bin/systemctl stop apache2`"
log_cmd_out "$cmd_out"

cmd_out="`/usr/bin/systemctl start apache2`"
log_cmd_out "$cmd_out"

log_info "iprint-relocate Completed"
   
