#!/bin/sh

#Copyright (c) 2007, Novell, Inc.  All rights reserved.

##########################################################################
# eDirectory migration script
##########################################################################

##########################################################################
# Script Initialization
##########################################################################

if [ $# -ne 7 ]; then
	str=`gettext migrate "INFO: This utility is not to be executed directly. Execute 'migedir' for eDirectory server migration."`
	echo $str
	exit 1
fi

# Message severity
FATAL="Fatal"
ERROR="Error"
WARN="Warning"
INFO="Information"

DATE=`date +%d-%m-%Y:%T`
srcDSVer=0
DS862Ver=10310
DS871Ver=10510
DS873Ver=10552

this_file=$0
source_server=$1
verbose=$2
log_file_path=$3
doBackupOnly=$4
doRestoreOnly=$5
SRC_PLATFORM=$6
dontPrompt=$7

# Assign the credentials to local variables
user="$ADMIN"
passwd="$ADM_PASSWD"

cmd_line="$@"
printUsage=0
package_dir=`dirname $this_file`
if [ -z "$package_dir" -o $package_dir = "." ]; then
	package_dir=`pwd`
fi

this_file_name=`basename $this_file`
case "${1}" in
-h | --help | --h* )
	printUsage=1
	;;
esac

##########################################################################
# Hard coded path for files 
##########################################################################

ncpshell_path=/opt/novell/ncpserv/sbin
edir_migration_bin=/opt/novell/migration/sbin
source_system=system

source_backup_volume=SYS
source_NICI_dir=system/nici
backup_file=backup.nds
backupFile="[bB][aA][cC][kK][uU][pP].[nN][dD][sS]"
backup_log=backup.log
backupLog="[bB][aA][cC][kK][uU][pP].[lL][oO][gG]"
restore_log=restore.log
emtool_batch_command=backup.cmd
userID=0

unix_default_cfgdir=/etc/opt/novell/eDirectory/conf
unix_default_dibdir=/var/opt/novell/eDirectory/data/dib
unix_default_bindir=/opt/novell/eDirectory/bin
unix_nici_dir=/var/opt/novell/nici
source_backup_dir=/var/opt/novell/.edir_migrate
target_backup_dir=/var/opt/novell/.edir_migrate

mount_point=${target_backup_dir}/mnt

key_file=nicisdi.key
key_backup=nicisdi.bak

ks2_file=xmgrcfg.ks2
ks2_backup=xmgrcfg.2

ks3_file=xmgrcfg.ks3
ks3_backup=xmgrcfg.3

nif_file=xmgrcfg.nif
nif_backup=xmgrcfg.f

file_001=xarchive.001
backup_001=xarchive.1

file_000=xarchive.000
backup_000=xarchive.0

fk_file=nicifk
fk_backup=nicifk.bak

conf_file_path=/etc/opt/novell/eDirectory/conf
conf_file=nds.conf
log_file="$log_file_path"/$this_file_name".log"
localIPAddress=

status_wait=10

if [ $dontPrompt -eq 1 ]; then
	SSH_CMD="ssh -o BatchMode=yes -o ConnectionAttempts=5"
	SCP_CMD="scp -o BatchMode=yes -o ConnectionAttempts=5"
else
	SSH_CMD="ssh -o ConnectionAttempts=5"
	SCP_CMD="scp -o ConnectionAttempts=5"
fi


##########################################################################
# Functions #
##########################################################################
printToScreen()
{
	echo "$*"
}

logToFile()
{
	if [ $verbose -eq 1 -o "$1" = "$FATAL" ]; then
		printToScreen "$2"
	fi

	echo "${DATE}:$1: $2" >> "$log_file"
}


readDSVer()
{
	srcDSVer=`sed -n 's/SOURCE_DSVER://p' ${source_backup_dir}/migedir.conf 2> /dev/null`
	if [ $? -ne 0 ]; then
		srcDSVer=0
	fi
	str=`gettext migrate "The DS version on the source server is ${srcDSVer}."`
	logToFile $INFO "$str"
}

setupNetWareMigration()
{
	str=`gettext migrate "Setting up the ${SRC_PLATFORM} eDirectory migration environment..."`
	logToFile $FATAL "$str"

	edir_install_path=/opt/novell/eDirectory/bin
	export PATH=.:/usr/bin:/usr/sbin:/usr/local/bin:$edir_install_path:$PATH

	# Check if novell-client-script package is installed
	if rpm -q novell-client-script > /dev/null 2>&1
	then
		str=`gettext migrate "Package 'novell-client-script' has been installed."`
		logToFile $INFO "$str"
	else
		str=`gettext migrate "The package 'novell-client-script' is not installed. Install 'novell-client-script' package and restart the migration."`
		logToFile $FATAL "$str"
		exit 1
	fi

	# Check if novell-qtgui-cli package is installed
	if rpm -q novell-qtgui-cli > /dev/null 2>&1
	then
		str=`gettext migrate "Package 'novell-qtgui-cli' has been installed."`
		logToFile $INFO "$str"
	else
		str=`gettext migrate "The package 'novell-qtgui-cli' is not installed. Install 'novell-qtgui-cli' package and restart the migration."`
		logToFile $FATAL "$str"
		exit 1
	fi

	# Mount the NetWare volume
	if [ -d "$mount_point" ]; then
		str=`gettext migrate "Mounting the SYS volume of '${source_server}' at ${mount_point}..."`
		logToFile $INFO "$str"
		nwlogout -a > /dev/null 2>&1

		nwlogin -s "$source_server" -u "$user" -p "$passwd"
		err=$?
		if [ $err -eq 0 ]; then
			nwmap -d "$mount_point/SYS" -v "$source_backup_volume"
			err=$?
			if [ $err -eq 0 ]; then
				str=`gettext migrate "${source_backup_volume} mounted successfully."`
				logToFile $INFO "$str"
			else
				str=`gettext migrate "ERROR $err: Unable to mount the NetWare volume at ${mount_point}/SYS."`
				logToFile $FATAL "$str"
				exit 1
			fi
		else
			str=`gettext migrate "ERROR $err: Unable to mount the NetWare volume at ${mount_point}/SYS."`
			logToFile $FATAL "$str"
			exit 1
		fi
	else
		str=`gettext migrate "ERROR: Unable to find the mount point '${mount_point}/SYS'."`
		logToFile $FATAL "$str"
		exit 1
	fi

	# Check if novell-ncpserv-tools package is installed
	if rpm -q novell-ncpserv-tools > /dev/null 2>&1
	then
		str=`gettext migrate "'novell-ncpserv-tools' package is available."`
		logToFile $INFO "$str"
	else
		str=`gettext migrate "The package 'novell-ncpserv-tools' is not installed. Install the package and restart the migration."`
		logToFile $FATAL "$str"
		exit 1
	fi

	# Copy and load secure copy NLM in the remote NetWare Server
	cp $ncpshell_path/ncpmig.nlm $mount_point/SYS/$source_system/ncpmig.nlm 2>/dev/null
	err=$?
	if [ $err -eq 0 ]; then
		str=`gettext migrate "Copied scp peer file (ncpmig.nlm) to source server successfully."`
		logToFile $INFO "$str"
	else
		str=`gettext migrate "ERROR ${err}: Could not copy scp peer file (ncpmig.nlm) to source server."`
		logToFile $FATAL "$str"
		exit 1
	fi

	# We are unloading embox in 8.7.1 to 8.7.3.5 edirectories before loading dsbk with latest backupcr to avoid Netware abend.BUG#521487
        if [ "$srcDSVer" -ge "$DS871Ver" -a "$srcDSVer" -lt "$DS873Ver" ]; then
status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --uninvoke --parm1 "embox.nlm"`
               if [ ! -z "$status" ]; then
                       str=`gettext migrate "WARNING: Unable to unload embox."`
                       printToScreen "$str"
                       logToFile $FATAL "$status"
               fi
        fi
	
	# Copy the backup/restore NLMs for handling DS 8.6.2 to DS 8.7.3.5 and if the DS version is 8.7.3.6(10552.60)or later do not copy the nlms.
	if [ "$srcDSVer" -ge "$DS862Ver" -a "$srcDSVer" -lt "$DS873Ver" ]; then
		status=`cp -f ${edir_migration_bin}/*.[nN][lL][mM] $mount_point/SYS/$source_system/ 2>/dev/null`
		err=$?
		if [ $err -eq 0 ]; then
			str=`gettext migrate "Copied dsbk.nlm and backupcr.nlm to source server successfully."`
			logToFile $INFO "$str"
		else
			str=`gettext migrate "ERROR ${err}: Could not copy dsbk.nlm and backupcr.nlm to source server- ${status}."`
			logToFile $FATAL "$str"
			exit 1
		fi
	fi

	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --invoke --parm1 "ncpmig.nlm"`
	if [ ! -z "$status" ]; then
		echo $status | grep 514 > /dev/null
		if [ $? -eq 0 ]; then
			str=`gettext migrate "ncpmig.nlm already loaded in the source server."`
			logToFile $INFO "$str"
		else
			str=`gettext migrate "ERROR: Could not load secure NCP NLM, ncpmig.nlm on the source server - $status."`
			printToScreen "$str"
			logToFile $FATAL "$status"
			exit 1
		fi
	fi
}


setupLinuxMigration()
{
	str=`gettext migrate "Setting up the ${SRC_PLATFORM} eDirectory migration environment..."`
	logToFile $FATAL "$str"

	edir_install_path=/opt/novell/eDirectory/bin
	export PATH=.:/usr/bin:/usr/sbin:/usr/local/bin:$edir_install_path:$PATH

	Linux_DSVersion=`ndsstat -h $source_server | grep "Product Version"`
	if [ $? -eq 0 ]; then
		RC=`echo $Linux_DSVersion | grep -E "v8.8 | v9"`
		if [ "$RC" == "" ]; then
			RC=`echo $Linux_DSVersion | grep "v8.7"`
			if [ "$RC" == "" ]; then
				str=`gettext migrate "unable to detect the edir version on source server $source_server"`
				logToFile $FATAL "$str"
				exit 1;
			else
				Linux_source_bindir=/usr/bin
			fi
		else
			Linux_source_bindir=/opt/novell/eDirectory/bin
		fi
	fi
}


checkNICIBackupDir()
{
	# Backup NICI files from SRC=>DST
	str=`gettext migrate "Migrating ${SRC_PLATFORM} NICI files from source server..."`
	logToFile $FATAL "$str"

	# Check if NICI backup dir already exist at the target
	if [ -d "${target_backup_dir}/nici/${userID}" ]
	then
		str=`gettext migrate "${target_backup_dir} already exists"`
		logToFile $INFO "$str"
		rm -f "$target_backup_dir"/nici/* > /dev/null 2>&1
		rm -f "$target_backup_dir"/nici/${userID}/* > /dev/null 2>&1
	fi
}


# Migrate the NICI files from the NetWare source server to the temporary location 
# on the target Linux server.
migrateNetWareNICI()
{
	checkNICIBackupDir

	# nicisdi.key file
	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$key_file --dst=$target_backup_dir/nici/$userID/$key_file`
	if [ ! -z "$status" ]
	then
		sleep 20
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$key_file --dst=$target_backup_dir/nici/$userID/$key_file`
		if [ ! -z "$status" ]; then
			str=`gettext migrate "Could not migrate NICI file, ${key_file}. It is ok to continue..."`
			logToFile $INFO "$status"
		fi
	fi

	# xmgrcfg.ks2 file
	sleep 20
	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$ks2_file --dst=$target_backup_dir/nici/$userID/$ks2_file`
	if [ ! -z "$status" ]
	then
		sleep 20
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$ks2_file --dst=$target_backup_dir/nici/$userID/$ks2_file`
		if [ ! -z "$status" ]
		then
			str=`gettext migrate "Could not migrate NICI file, ${ks2_file}. Migration cannot proceed. Exiting"`
			printToScreen "$str"
			logToFile $FATAL "$status"
			exit 1
		fi
	fi

	# xmgrcfg.ks3 file
	sleep 20
	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$ks3_file --dst=$target_backup_dir/nici/$userID/$ks3_file`
	if [ ! -z "$status" ]
	then
		sleep 20
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$ks3_file --dst=$target_backup_dir/nici/$userID/$ks3_file`
		if [ ! -z "$status" ]
		then
			str=`gettext migrate "Could not migrate NICI file, ${ks3_file}. Migration cannot proceed. Exiting"`
			printToScreen "$str"
			logToFile $FATAL "$status"
			exit 1
		fi
	fi

	# xarchive.001 file
	sleep 20
	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$file_001 --dst=$target_backup_dir/nici/$userID/$file_001`
	if [ ! -z "$status" ]
	then
		sleep 20
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$file_001 --dst=$target_backup_dir/nici/$userID/$file_001`
		if [ ! -z "$status" ]
		then
			str=`gettext migrate "Error: Could not migrate NICI file, ${file_001}. Migration cannot proceed. Exiting"`
			printToScreen "$str"
			logToFile $FATAL "$status"
			exit 1
		fi
	fi

	# xarchive.000 file
	sleep 20
	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$file_000 --dst=$target_backup_dir/nici/$file_000`
	if [ ! -z "$status" ]
	then
		sleep 20
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$file_000 --dst=$target_backup_dir/nici/$file_000`
		if [ ! -z "$status" ]
		then
			str=`gettext migrate "Could not migrate NICI file, ${file_000}. Migration cannot proceed. Exiting"`
			printToScreen "$str"
			logToFile $FATAL "$status"
			exit 1
		fi
	fi

	# xmgrcfg.nif file
	sleep 20
	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$nif_file --dst=$target_backup_dir/nici/$nif_file`
	if [ ! -z "$status" ]
	then
		sleep 20
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_NICI_dir/$nif_file --dst=$target_backup_dir/nici/$nif_file`
		if [ ! -z "$status" ]
		then
			str=`gettext migrate "Could not migrate NICI file, ${nif_file}. Migration cannot proceed. Exiting"`
			printToScreen "$str"
			logToFile $FATAL "$status"
			exit 1
		fi
	fi

	# nicifk file
	# Not checking for return value because the NICIFK need not be present
	# because it is being deleted in some versions! after initialization.
	sleep 20
	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_system/$fk_file --dst=$target_backup_dir/nici/$fk_file`
	if [ ! -z "$status" ]
	then
		sleep 20
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --scp --src=$source_backup_volume:$source_system/$fk_file --dst=$target_backup_dir/nici/$fk_file`
	fi
}


# Migrate the NICI files from Linux source server to the temporary location 
# on the target Linux server.
migrateLinuxNICI()
{
	checkNICIBackupDir

	# Backup NICI files from SRC=>DST
	str=`gettext migrate "Migrating ${SRC_PLATFORM} NICI files from source server..."`
	logToFile $INFO "$str"

	# eDir. 873x NICI location is different from that from later versions
	str=`gettext migrate "Checking the NICI file location on the source server..."`
	printToScreen $str
	cmd="${SSH_CMD} -o NumberOfPasswordPrompts=1 root@${source_server} ls /var/novell/nici/primenici"
	logToFile $INFO "$cmd"

	while [ 1 ]; do
		status=`${cmd} 2>&1`
		err=$?

		if [ $err -eq 0 ]; then
			src_nici_dir="/var/novell/nici"
			break
		else
			echo $status | grep "Permission denied" > /dev/null
			if [ $? -eq 0 ]; then
				str=`gettext migrate "ssh login as 'root' to ${source_server} failed!, Retry..."`
				logToFile $FATAL "$str"

				if [ $dontPrompt -eq 1 ]; then
					exit 1
				else
					continue
				fi
			fi

			echo $status | grep "No such file or directory" > /dev/null
			if [ $? -eq 0 ]; then
				err=0
				src_nici_dir=${unix_nici_dir}
				break
			else
				str=`gettext migrate "ERROR: Unable to migrate NICI files from '${source_server}:${src_nici_dir}' to '${target_backup_dir}/nici/' - ssh returned ${err}, ${status}."`
				logToFile $FATAL "$str"
				exit 1
			fi
		fi
	done

	str=`gettext migrate "Copying the NICI files from the source server..."`
	printToScreen $str
	cmd="${SCP_CMD} -rp ${source_server}:${src_nici_dir}/* ${target_backup_dir}/nici/"
	logToFile $INFO "$cmd"

	status=`${cmd} 2>&1`
	err=$?

	if [ $err -ne 0 ]; then
		str=`gettext migrate "ERROR: Unable to migrate NICI files from '${source_server}:${src_nici_dir}' to '${target_backup_dir}/nici/' - scp returned ${err}, ${status}."`
		logToFile $FATAL "$str"
		exit 1
	fi

	# Set the file ownership for the userID 'wwwrun', assuming the UID to be 30 always.
	if [ -d ${target_backup_dir}/nici/30/ ]; then
		str=`gettext migrate "Setting the ownership to 'wwwrun' NICI directory..."`
		logToFile $INFO "$str"
		chown -R wwwrun:www ${target_backup_dir}/nici/30/ > /dev/null
	fi

	rm -f ${target_backup_dir}/nici/set_server_mode* ${target_backup_dir}/nici/nicimud* ${target_backup_dir}/nici/primenici* ${target_backup_dir}/nici/xmgrcfg.wks > /dev/null 2>&1
}


# Restore NICI files from backup location
restoreNICI()
{
	str=`gettext migrate "Restoring NICI files to the target location on this server..."`
	logToFile $FATAL "$str"

	if [ ! -d ${target_backup_dir}/nici ]; then
		str=`gettext migrate "ERROR: The NICI backup directory '${target_backup_dir}/nici/${userID}' does not exist."`
		logToFile $FATAL "$str"
		exit 1
	fi

	if [ ! -d ${unix_nici_dir} ]; then
		str=`gettext migrate "ERROR: The NICI directory '${unix_nici_dir}' does not exist. Check if the NICI rpm is installed on this machine."`
		logToFile $FATAL "$str"
		exit 1
	fi

	cp -rp ${target_backup_dir}/nici/* ${unix_nici_dir}/ > /dev/null
	if [ $? -ne 0 ]; then
		str=`gettext migrate "ERROR: Failed to restore NICI files from ${target_backup_dir}/nici to ${unix_nici_dir}."`
		logToFile $FATAL "$str"
		exit 1
	fi
}


backupNetWareDIB()
{
	str=`gettext migrate "Backing up DIB on the source ${SRC_PLATFORM} server..."`
	logToFile $FATAL "$str"

	if [ -f ${mount_point}/SYS/${backupLog} ]; then 
		rm -f ${mount_point}/SYS/${backupLog} > /dev/null
	fi

	if [ -f ${mount_point}/SYS/${backupFile} ]; then 
		rm -f ${mount_point}/SYS/${backupFile} > /dev/null
	fi
	if [  ! -f "$mount_point/SYS/$source_system/config.txt" ]; then
		status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --invoke --parm1 "config.nlm"` 	
		if [ ! -z "$status" ]; then
			str=`gettext migrate "ERROR: Unable to run the config.nlm."`
			printToScreen "$str"
			logToFile $FATAL "$status"
		fi
	fi

	sleep 20
	Lang_ID=`cat "$mount_point/SYS/$source_system/config.txt" | grep "Server language" | awk -F":" '{print $2}' |  awk -F" " '{print $2}' | awk -F"(" '{print $2}'|awk -F")" '{print $1}'`
	if [ -z "$Lang_ID" ]; then
		str=`gettext migrate "ERROR: Not able to get the default Language ID of the source server $source_server."`
		printToScreen "$str"
		logToFile $FATAL "$Lang_ID"	
		exit 1
	fi
	if [ "$Lang_ID" != "4" ]; then
		echo "language english" > "${source_backup_dir}/migration.ncf"
        	cp ${source_backup_dir}/migration.ncf $mount_point/SYS/$source_system/migration.ncf 2>/dev/null
        	err=$?
        	if [ $err -eq 0 ]; then
               		str=`gettext migrate "Copied migration NCF file (migration.ncf) to source server successfully."`
               	 	logToFile $INFO "$str"
        	else
			str=`gettext migrate "ERROR ${err}: Could not copy migration NCF file (migration.ncf) to source server."`
			logToFile $FATAL "$str"
               	 exit 1
        	fi
	

        	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --runncf --src="SYS:$source_system\migration.ncf"`
        	if [ ! -z "$status" ]; then
               		 str=`gettext migrate "ERROR: Unable to change language on the source server."`
               		 printToScreen "$str"
               		 logToFile $FATAL "$status"
               		 exit 1
        	fi
	fi

	status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --invoke --parm1 "dsbk.nlm backup -f SYS:\backup.nds -l SYS:\backup.log -t -b -c -o -w"`
	if [ ! -z "$status" ]; then
		str=`gettext migrate "ERROR: Unable to perform backup of eDirectory DIB."`
		printToScreen "$str"
		logToFile $FATAL "$status"
		
		if [ "$Lang_ID" != "4" ]; then
			echo "language $Lang_ID" > "${source_backup_dir}/migration.ncf"
			cp ${source_backup_dir}/migration.ncf $mount_point/SYS/$source_system/migration.ncf 2>/dev/null
			err=$?
			if [ $err -eq 0 ]; then
				str=`gettext migrate "Copied Again the migration NCF file (migration.ncf) to source server successfully."`
                	logToFile $INFO "$str"
			else
				str=`gettext migrate "ERROR ${err}: Could not copy migration NCF file (migration.ncf) to source server to reset the language to default."`
				logToFile $FATAL "$str"
				exit 1
			fi
			status=`"$ncpshell_path"/ncpshell --p="$passwd" --ip="$source_server" --u="$user" --runncf --src="SYS:$source_system\migration.ncf"`
			if [ ! -z "$status" ]; then
				str=`gettext migrate "ERROR: Unable to change language to default on the source server.  Restart The source server $source_server"`
				printToScreen "$str"
				logToFile $FATAL "$status"
               			exit 1
        		fi
		fi
		exit 1
	fi

	sleep $status_wait
	while test 1; do
		bstatus=`cat ${mount_point}/SYS/${backupLog} 2>/dev/null | grep -i "successfully"`
		berror=`cat ${mount_point}/SYS/${backupLog} 2>/dev/null | grep -i "error"`
		if [ ! -z "$berror" ]
		then
			str=`gettext migrate "mignds: Backup failed on the source server. Cannot migrate successfully. View 'SYS:\\${backup_log}' on the source server for more details."`
			logToFile $FATAL "$str"
			exit 1
		fi

		if [ ! -z "$bstatus" ]
		then
			str=`gettext migrate "Backup successful on the source server. The DIB on the source server will be locked."`
			logToFile $INFO "$str"
			copyBackup
			return
		fi
	done
}

backupLinuxDIB()
{
	str=`gettext migrate "Backing up DIB on the source ${SRC_PLATFORM} server..."`
	logToFile $FATAL "$str"

	str=`gettext migrate "Setting up backup environment on ${source_server}..."`
	printToScreen "$str"
	cmd="${SSH_CMD} root@${source_server} rm -f ${source_backup_dir}/${backup_log} ${source_backup_dir}/${backup_file}* ; echo /tmp/dsbk.command > /etc/dsbk.conf ; > /tmp/dsbk.command ; mkdir -p ${source_backup_dir}"
	logToFile $INFO "$cmd"

	status=`${cmd} 2>&1`
	err=$?
	if [ $err -eq 0 ]; then
		str=`gettext migrate "Performing backup at ${source_server}..."`
		printToScreen "$str"
			
		RC=`echo $Linux_DSVersion | grep "v8.7" 2> /dev/null`
		if [ "$RC" == "" ]; then
			cmd="${SSH_CMD} root@${source_server} ${Linux_source_bindir}/ndspath dsbk backup -f ${source_backup_dir}/${backup_file} -l ${source_backup_dir}/${backup_log} -t -b -c -o -w"
		else
			cmd="${SSH_CMD} root@${source_server} dsbk backup -f ${source_backup_dir}/${backup_file} -l ${source_backup_dir}/${backup_log} -t -b -c -o -w"
		fi
		logToFile $INFO "$cmd"

		status=`${cmd} 2>&1`
		err=$?
		if [ $err -eq 0 ]; then
			sleep $status_wait
			while test 1; do
				str=`gettext migrate "Getting backup status at ${source_server}..."`
				printToScreen "$str"
				cmd="${SSH_CMD} root@${source_server} grep -i successfully ${source_backup_dir}/${backup_log}"
				logToFile $INFO "$cmd"
				bstatus=`${cmd}`

				str=`gettext migrate "Getting backup return value at ${source_server}..."`
				printToScreen "$str"
				cmd="${SSH_CMD} root@${source_server} grep -i error ${source_backup_dir}/${backup_log}"
				logToFile $INFO "$cmd"
				berror=`${cmd}`

				if [ ! -z "$berror" ]; then
					str=`gettext migrate "mignds: Backup failed on the source server. Cannot migrate successfully. View '${source_server}:/${source_backup_dir}/${backup_log}' on the source server for more details."`
					printToScreen "$str"
					exit 1
				fi

				if [ ! -z "$bstatus" ]; then
					str=`gettext migrate "Backup successful on the source server. The DIB on the source server will be locked."`
					logToFile $INFO "$str"
					str=`gettext migrate "Transferring DIB to target server..."`
					logToFile $INFO "$str"

					str=`gettext migrate "Transferring the eDirectory backup file(s) from ${source_server} to target server..."`
					printToScreen "$str"
					cmd="${SCP_CMD} root@${source_server}:${source_backup_dir}/${backup_file}* ${target_backup_dir}/"
					logToFile $INFO "$cmd"
					status=`${cmd} 2>&1`
					err=$?
					# Test if copying is successful
					if [ $err -eq 0 ]; then
						str=`gettext migrate "The backup file, ${backup_file} is transferred to the location, ${target_backup_dir} successfully."`
						logToFile $INFO "$str"
						return
					else
						str=`gettext migrate "ERROR ${err}: Could not transfer the backup file, ${file} to the location ${target_backup_dir} on the target server - ${status}."`
						printToScreen "$str"

						exit 1
					fi
				fi
			done
		else
			str=`gettext migrate "ERROR ${err}: Backup failed on the source server. Cannot migrate successfully. View '${source_server}:/${source_backup_dir}/${backup_log}' on the source server for more details - ${status}."`
			printToScreen "$str"
			exit 1
		fi
	else
		str=`gettext migrate "ERROR ${err}: Unable to setup backup environment - ${status}."`
		logToFile $FATAL "$str"
		exit 1
	fi
}


copyBackup()
{
	str=`gettext migrate "Transferring DIB to target server..."`
	logToFile $FATAL "$str"

	# Check and clear old files
	if [ -f "$target_backup_dir"/"${backupFile}" ]; then
		rm -f "$target_backup_dir"/"${backupFile}" > /dev/null
	fi

	for file in `ls ${mount_point}/SYS/${backupFile}*`
	do
		file=`basename $file`
		#Reading checksum before copying
		source_cksum=`cksum ${mount_point}/SYS/${file} | awk -F" " '{print $1}'`
		logToFile $INFO "Checksum at source = $source_cksum"
	
		#Copy the backup DIB to the target location in lower case
		bkFile=`echo "$file" | tr '[:upper:]' '[:lower:]'`
		cp ${mount_point}/SYS/${file} ${target_backup_dir}/${bkFile} >/dev/null 2>&1
		#Test if copying is successful
		if [ $? -eq 0 ]
		then
			str=`gettext migrate "The backup file, ${file} is transferred to the location, ${target_backup_dir} successfully."`
			logToFile $INFO "$str"
		else
			str=`gettext migrate "Could not transfer the backup file, ${file} to the location ${mount_point}/SYS/${target_backup_dir} on the target server, Migration cannot proceed. Exiting."`
			printToScreen "$str"

			exit 1
		fi

		# Verifying checksum after copying
		dest_cksum=`cksum $target_backup_dir/$bkFile | awk -F" " '{print $1}'`
		logToFile $INFO "Checksum at target = $dest_cksum"

		if [ "$source_cksum" -eq "$dest_cksum" ]
		then
			str=`gettext migrate "Checksums of the backup files matched."`
			logToFile $INFO "$str"
		else	
			str=`gettext migrate "Checksum of the backup file did not match, possibly corrupted DIB. Exiting..."`
			printToScreen "$str"

			exit 1
		fi
	done

	# On successful copy unmount NetWare volume mount point
	nwlogout -a
	if [ $? -eq 0 ]
	then
		str=`gettext migrate "${mount_point} unmounted successfully."`
		logToFile $INFO "$str"
	else
		str=`gettext migrate "Could not unmount ${mount_point}."`
		logToFile $WARN "$str"
	fi 
}


# Rename *.NDS file -> *.nds. On NetWare the stream files are with upper case
# while that on Linux platform, it has to be lower case.
ChangeStreamFileExtension()
{
	str=`gettext migrate "Checking the stream files..."`
	logToFile $INFO "$str"

	for file in `ls ${unix_default_dibdir}/ | grep "\.NDS$" 2> /dev/null`; do
		file=${unix_default_dibdir}/${file}
		fDir=`dirname ${file}`
		fName=`basename ${file} | awk -F. '{print $1}'`
		newFile=${fDir}/${fName}.nds

		if [ ! -s ${newFile} ]; then
			rm -f ${newFile} > /dev/null
			mv ${file} ${newFile}
		fi
	done
}


restoreDIB()
{
	# Setup restore environment
	if [ -f "$log_file_path"/"$restore_log" ]; then
		rm -f "$log_file_path"/"$restore_log"
	fi

	if [ ! -f /etc/dsbk.conf ]
	then
		echo "/tmp/dsbk.command">/etc/dsbk.conf
		> /tmp/dsbk.command
	fi

	if [ -f "${unix_default_cfgdir}/nds.conf" ]; then
		localIPAddress=`sed -n 's/n4u.server.interfaces=//p' ${unix_default_cfgdir}/nds.conf | awk -F\@ '{print $1}'`
	fi
	status=`cp -rf ${unix_default_cfgdir}/nds.conf ${unix_default_cfgdir}/nds.conf_bak`
        if [ $? -ne 0 ]; then
        	str=`gettext migrate "WARNING: Unable take backup for default nds.conf."`
        	printToScreen "$str"
        	logToFile $FATAL "$status"
		exit 1
        fi

	> ${unix_default_cfgdir}/nds.conf > /dev/null
	if [ $? -ne 0 ]; then
		str=`gettext migrate "ERROR $?: Unable to create the eDirectory configuration file, '${unix_default_cfgdir}/nds.conf'"`
		logToFile $FATAL "$str"
		exit 1
	fi

	if [ $? -ne 0 ]; then
		str=`gettext migrate "ERROR $?: Unable to update the eDirectory configuration file, '${unix_default_cfgdir}/.edir/instances.0'"`
		logToFile $FATAL "$str"
		exit 1
	fi

##create-systemd-templates expects vardir to be present in the conf file
	echo "n4u.server.vardir=/var/opt/novell/eDirectory/data" >> /etc/opt/novell/eDirectory/conf/nds.conf
##Creating systemd templates
	$unix_default_bindir/create-systemd-templates $unix_default_cfgdir/nds.conf
	mv $unix_default_cfgdir/ndsd.service /usr/lib/systemd/system/
	restartServer
	systemctl enable ndsd.service
	if [ $? -ne 0 ]
	then
		str=`gettext migrate "Unable to run 'systemctl enable ndsd.service'"`
		logToFile $FATAL "$str"
		exit 1
	fi
	echo "${unix_default_cfgdir}/nds.conf" > ${unix_default_cfgdir}/.edir/instances.0
	 if [ $doRestoreOnly -eq 1 ]; then
                rm -rf "${unix_default_dibdir}"/*
        fi
	restartServer
	
	#Perform ndsstat and check if server is ready. Time out after 30 seconds
	count=30
	while true
	do
		ndsstat >/dev/null 2>/dev/null
		if [ $? -eq 0 -o $count -eq 0 ]
		then
			break
		fi
		sleep 1
		count=`expr $count - 1`
	done

	str=`gettext migrate "Restoring DIB in target..."`
	logToFile $FATAL "$str"

	if [ -f "$target_backup_dir"/"$backup_file" ]; then
		dsbk restore -f $target_backup_dir/${backup_file} -l "$log_file_path"/"$restore_log" -r -o -a >/dev/null
		sleep 4

		while test 1; do
			rstatus=`cat "$log_file_path"/"$restore_log" 2>/dev/null | grep -i "Restore completed"`
			rerror=`cat "$log_file_path"/"$restore_log" 2>/dev/null | grep -i "error"`
			if [ ! -z "$rerror" ]; then
				str=`gettext migrate "ERROR: Restore failed in target. Refer to '$log_file_path/$restore_log' for more details."`
				logToFile $FATAL "$str"
				status=`cp -rf ${unix_default_cfgdir}/nds.conf_bak ${unix_default_cfgdir}/nds.conf`	
        			if [ $? -ne 0 ]; then
		       			str=`gettext migrate "WARNING: Unable to restore the default nds.conf. Restore the file from ${unix_default_cfgdir}/nds.conf_bak."`
					printToScreen "$str"
					logToFile $FATAL "$status"
        			fi
				exit 1
			fi
			status=`rm -fr ${unix_default_cfgdir}/nds.conf_bak`
        		if [ $? -ne 0 ]; then
				str=`gettext migrate "WARNING: Unable to delete the ${unix_default_cfgdir}/nds.conf_bak file, Manually delete the file"`
				printToScreen "$str"
				logToFile $FATAL "$status"
			fi

			if [ ! -z "$rstatus" ]; then
				str=`gettext migrate "Restore successful in target server."`
				logToFile $INFO "$str"

				echo "n4u.server.dibdir=/var/opt/novell/eDirectory/data/dib" >> /etc/opt/novell/eDirectory/conf/nds.conf
				# Check the stream files under the DIB directory
				ChangeStreamFileExtension
				return
			fi
		done
	else
		str=`gettext migrate "DIB backup file, ${target_backup_dir}/${backup_file} not found on the target server to restore."`
		logToFile $FATAL "$str"
		exit 1
	fi
}


# Update the eDirectory configuration file
updateConf()
{
	str=`gettext migrate "Updating target eDirectory server instance configuration file, '${unix_default_cfgdir}/nds.conf'..."`
	logToFile $FATAL "$str"

	# Pause for sometime so that eDirectory server updates the configuration file
	sleep 20

	# This file would trigger DIB upgrade during 'ndsconfig upgrade' execution
	> ${unix_default_dibdir}/upgradeDIB

	#Set NCP interface parameter
	grep "n4u.server.interfaces" ${unix_default_cfgdir}/nds.conf > /dev/null
	if [ $? -ne 0 ]; then
		ndsconfig set n4u.server.interfaces=$localIPAddress@524 --config-file ${unix_default_cfgdir}/nds.conf > /dev/null
	fi

	if [ $? -ne 0 ]; then
		str=`gettext migrate "INFO $?: Unable to set the server interface parameter."`
		logToFile $INFO "$str"
	fi

	if [ $? -ne 0 ]; then
		str=`gettext migrate "INFO $?: Unable to set the preferred server name parameter."`
		logToFile $INFO "$str"
	fi

	#Set the HTTP module base directory
	ndsconfig set http.server.module-base=/var/opt/novell/eDirectory/data/nds-http/ --config-file ${unix_default_cfgdir}/nds.conf > /dev/null

	if [ $? -ne 0 ]; then
		str=`gettext migrate "INFO $?: Unable to set the default HTTP module base parameter. \nUpdate the parameter 'http.server.module-base=/var/opt/novell/eDirectory/data/nds-http/' in '${unix_default_cfgdir}/nds.conf' and execute 'ndsconfig upgrade' to complete the eDirectory migration."`
		logToFile $INFO "$str"
	fi
}


#Set the preferred server name parameter
setPreferredServer()
{
	srvName=`sed -n s/n4u.nds.server-name=//p ${unix_default_cfgdir}/nds.conf`
	if [ "$srvName" != "" ]; then
		ndsconfig set n4u.nds.preferred-server=$srvName --config-file ${unix_default_cfgdir}/nds.conf > /dev/null
	fi
}


restartServer()
{
	str=`gettext migrate "Restarting eDirectory server on the target server..."`
	logToFile $FATAL "$str"

	log=`systemctl restart ndsd.service`
	if [ $? -eq 0 ]
	then
		str=`gettext migrate "Server restarted successfully."`
		logToFile $INFO "$str"
		systemctl daemon-reload
		return 0
	else
		str=`gettext migrate "Failed to restart eDirectory server. Refer /var/opt/novell/eDirectory/log/ndsd.log for more details. Exiting."`
		printToScreen "$str"
		exit 1
	fi
}

upgradeServer()
{
	str=`gettext migrate "Updating eDirectory server on the target server..."`
	logToFile $FATAL "$str"

	#if the DS Version is above 10552.60 for eDir 8.7.3.6 do not run ndsrepair -R
	if [ "$srcDSVer" -ge "$DS862Ver" -a "$srcDSVer" -lt "$DS873Ver" ]; then
		ndsrepair -R -o yes -i no -d no -t no -r no -v no -c no -F "$log_file_path"/schema_repair.log --config-file "$conf_file_path"/"$conf_file" 2>&1 > /dev/null
		if [ $? -eq 0 ]; then
			str=`gettext migrate "Repairing operational schema completed successfully."`
			logToFile $INFO "$str"
		else
			str=`gettext migrate "ERROR: Unable to repair operational schema. Execute the following commands manually in the mentioned sequence to complete the migration process, 'ndsrepair -R', 'ndsconfig upgrade' and 'ndsrepair -N' to complete the migration process."`
			logToFile $FATAL "$str"
			exit 1
		fi
	fi

	ndsconfig upgrade -a "$user" -j --config-file "$conf_file_path"/"$conf_file" --configure-eba-now no 2>&1 >"$log_file_path"/upgrade.log
	if [ $? -eq 0 ]; then
		str=`gettext migrate "eDirectory server updated successfully."`
		logToFile $INFO "$str"
	else
		str=`gettext migrate "INFO: Error was detected during the execution of eDirectory configuration, 'ndsconfig upgrade'. Check the log file '${log_file_path}/upgrade.log' for more details. Continuing with the migration..."`
		logToFile $INFO "$str"
	fi
}


# Perform Network address repair
performAddressRepair()
{
	str=`gettext migrate "Checking eDirectory server network address..."`
	logToFile $FATAL "$str"

	if [ -f "$log_file_path"/address_repair.log ]; then
		rm -f "$log_file_path"/address_repair.log
	fi

	# Create unattended repair options file
	echo "1" > migndsopts
	echo "2" >> migndsopts
	result=`ndsrepair -N -F "$log_file_path"/address_repair.log < migndsopts`

	if [ $? -eq 0 ]; then
		str=`gettext migrate "eDirectory network address repair completed successfully. Refer ${log_file_path}/address_repair.log for more details."`
		logToFile "$INFO" "$str"
	else
		str=`gettext migrate "ERROR: Could not perform network address repair. Refer ${log_file_path}/address_repair.log for more details. Execute 'ndsrepair -N.' to complete the migration process."`
		printToScreen "$str"
		logToFile "$FATAL" "$str"
	fi

	rm -f migndsopts > /dev/null
}

cleanUp()
{
	str=`gettext migrate "Performing clean up of temporary files created during migration..."`
	logToFile $INFO "$str"

	# Delete files created by ncpshell during secure copy
	currDir=`pwd`
	str=`gettext migrate "Removing ${currDir}/TMP* files created by ncpshell utility..."`
	logToFile $INFO "$str"
	rm -f ${currDir}/TMP* > /dev/null

	# Delete DIB backup copy
	# rm -f ${target_backup_dir}/backup.nds
	# rm -rf ${target_backup_dir}/nici/
}


isSrcSrvUp()
{
	str=`gettext migrate "Checking that the eDirectory server on the source server is down..."`
	logToFile $FATAL "$str"

	if [ -f  ${source_backup_dir}/migedir.conf ]; then
		srcIP=`sed -n 's/SOURCE_IP://p' ${source_backup_dir}/migedir.conf 2> /dev/null`
	else
		srcIP=
	fi

	while [ 1 ]; do
		if [ "$srcIP" = "" ]; then
			str=`gettext migrate "INFO: Unable to determine the IP address of the source server. Please check and ensure that the eDirectory server on the source server is shutdown. Proceeding with the migration..."`
			logToFile $FATAL "$str"
			break
		else
			ndsstat -h ${srcIP} > /dev/null 2>&1
			if [ $? -ne 0 ]; then
				break
			else
				str=`gettext migrate "Please shutdown the eDirectory server on the source server, ${srcIP} ..."`
				logToFile $FATAL "$str"
				sleep 10
			fi
		fi
	done
}

##########################################################################

main()
{
	str=`gettext migrate "Performing eDirectory migration..."`
	printToScreen "$str"
	str=`gettext migrate "This may take time depending on the size of the data on the server that is being migrated."`
	printToScreen "$str"

	# Read DS version
	readDSVer

	if [ $doRestoreOnly -ne 1 ]; then
		setup${SRC_PLATFORM}Migration
		migrate${SRC_PLATFORM}NICI
		backup${SRC_PLATFORM}DIB
	fi

	if [ $doBackupOnly -eq 1 ]; then
		str=`gettext migrate "eDirectory backup operation completed."`
		printToScreen "$str"
		cleanUp
		return 0
	fi

	isSrcSrvUp
	restoreNICI
	restoreDIB
	restartServer
	updateConf
	upgradeServer
	setPreferredServer

	str=`gettext migrate "Performing post-migration tasks..."`
	printToScreen "$str"
	performAddressRepair
	cleanUp
	str=`gettext migrate "Completing migration... Done"`
	printToScreen "$str"
	return 0
}

main

