#!/bin/sh
# ####################################################################
# Exceed Connection Server 13.7
# Copyright (c) 1997-2009 Open Text Corporation. All Rights Reserved.
# ####################################################################
# Migration Tool
# $Revision: 10408 $
# #####################################################################

. ./sys/msg.txt

arg0=$0

start_over()
{
  exec sh ${arg0} ${logfile} ${InstallDir}
}

update_config_file()
{
	cat >> $config_file << EOF
EoDFirstDisplay=${num}

[CLUSTER_MANAGER]
EoDCMPort=${cmport}

EOF
}

init_and_export_variables()
{
	pre="    "
	pre2="  "

	defaultOldDirName45="Exceed_onDemand_4.5"
	defaultOldDirName5="Exceed_onDemand_5"
	defaultOldDirName51="Exceed_onDemand_5.1"
	userSubDir="/conf/users/"
	profSubDir="/conf/admin/profiles/"
	xconfigSubDir="/conf/admin/Xconfig/"
	xstartSubDir="/conf/admin/Xstart/"
	xinitFile="/conf/admin/GlobalXinitrc"
	defUserSettingsFile="/conf/admin/usrstngs.cfg"
	keysSubDir="/conf/home/keys/"
	backupExt=".backup"
	fontsPath="/conf/home/font"
	userFontsDirName="user"

	if [ ${OSTYPE:="UNKNOWN"} = "Linux" -o ${OSTYPE} = "linux-gnu" -o ${OSTYPE} = "linux" ]; then
	  ECHO="echo -e"
	else
	  ECHO="echo"
	fi

	export ECHO
}

check_command_line_options()
{
	if [ "${1}xxxx" = "xxxx" -o "${2}xxx" = "xxx" ]; then
	  ${ECHO} ${pre}${USAGE}: $0 ${LOGFILE_ARG}
	  exit 1
	else
	  logfile=$1
	  InstallDir=$2
	fi
}

get_user_confirmation()
{
	${ECHO} "${pre}${MIG_DO_MIGRATION_Q1}"
	${ECHO} "${pre}${MIG_DO_MIGRATION_Q2}"

	read answer
	
	if [ ! "${answer}x" = "yx" -a ! "${answer}x" = "Yx" ] ; then
  	# doesn't want to migrate
  	${ECHO} "${INFO}${MIG_NO_MIGRATION}" >> $logfile
  	exit 1
	fi
}

set_default_old_installation_directory()
{
  wd=`pwd`
  cd ../../..
  
  eod45dir=`pwd`/${defaultOldDirName45}
  eod5dir=`pwd`/${defaultOldDirName5}
  eod51dir=`pwd`/${defaultOldDirName51}
  serverDirName="/Server"
  
  if [ -r "$eod51dir" ] ; then
  	defaultOldDir=$eod51dir
  	serverDir="Server/"
	elif [ -r "$eod5dir" ] ; then
  	defaultOldDir=$eod5dir
  	serverDirName=""
  elif [ -r "$eod45dir" ] ; then
  	defaultOldDir=$eod45dir
  	serverDirName=""
  else
  	defaultOldDir=$eod51dir
  fi

  cd ${wd}
}

get_old_installation_directory()
{
  ${ECHO}
  
  if [ -x ${defaultOldDir} ] ; then    
    ${ECHO} "${pre}${MIG_WHERE_Q1}"
    ${ECHO} "${pre}${MIG_WHERE_Q2}${defaultOldDir}${MIG_WHERE_Q3}"
    read oldDir
    
    if [ "${oldDir}xx" = "xx" ] ; then
      oldDir="${defaultOldDir}"
    fi
  else
    ${ECHO} "${pre}${MIG_WHERE_NODEFAULT_Q}"
    read oldDir
  fi
  
  if [ ! -r "${oldDir}" ] ; then
    ${ECHO}
    ${ECHO} "${pre}${MIG_NO_SUCH_DIR1}${oldDir}${MIG_NO_SUCH_DIR2}"
    ${ECHO}
    # start over 
#    exec sh $0 $1 $2
		start_over
  fi
  
  if [ "${oldDir}" = "${InstallDir}" -o "${oldDir}/" = "${InstallDir}" -o "${oldDir}" = "${InstallDir}/" ] ; then
    ${ECHO} "${oldDir}${MIG_IS_CURRENT}"
    ${ECHO}
    # start over
#    exec sh $0 $1 $2
		start_over
  fi

  if [ -x ${oldDir}${serverDirName} ] ; then    
  	oldDir=${oldDir}${serverDirName}
  fi
}

#######################
# Build dirs and check
#######################
init_more_variables()
{
  oldXinit="${oldDir}${xinitFile}"
  newXinit="${InstallDir}${xinitFile}"
  oldGlobalProfDir="${oldDir}${profSubDir}"
  newGlobalProfDir="${InstallDir}${profSubDir}"
  oldUsersDir="${oldDir}${userSubDir}"
  newUsersDir="${InstallDir}${userSubDir}"
  oldKeyDir="${oldDir}${keysSubDir}"
  newKeyDir="${InstallDir}${keysSubDir}"
  oldDefUserSettingsFile="${oldDir}${defUserSettingsFile}"
#  newDefUserSettingsFile="${InstallDir}/${defUserSettingsFile}"
  newDefUserSettingsFile="${InstallDir}/conf/admin/Templates/User/settings.cfg"
  oldFontDir="${oldDir}${fontsPath}"
  oldUserFontDir="${oldFontDir}/${userFontsDirName}"
  newFontDir="${InstallDir}${fontsPath}"
  newUserFontDir="${newFontDir}/${userFontsDirName}"
}

# make sure user dir exists 
validate_old_user_dir()
{
  if [ ! -x "${oldUsersDir}" ] ; then 
    ${ECHO} "${pre}${MIG_DIR_NO_GOOD}${oldDir}."
    ${ECHO}
    # start over 
#    exec sh $0 $1 $2
		start_over
  fi
}

#  Obsolete for 5.1  - hph
validate_old_global_profile_dir()
{
  if [ ! -x "${oldGlobalProfDir}" ] ; then
  	newGlobalXcobfigDir="${InstallDir}${xconfigSubDir}"
  	newGlobalXstartDwir="${InstallDir}${xstartSubDir}" 
   	${ECHO} "${pre}${oldDir}${MIG_DIR_NO_GOOD}"
    ${ECHO}
    # start over 
#    exec sh $0 $1 $2
		start_over
	else
  	newGlobalProfDir="${InstallDir}${profSubDir}"
  fi
}

migrate_files()
{
  ${ECHO}
  ${ECHO} "${pre}${MIG_COPY_USERSTNGS}"
  
  if [ -r "${newDefUserSettingsFile}" ] ; then 
    mv "${newDefUserSettingsFile}" "${newDefUserSettingsFile}${backupExt}" > /dev/null 2>&1
    ${ECHO} "${INFO}${MIG_BUP_USERSTNGS}${newDefUserSettingsFile}${backupExt}" >> $logfile
  fi
  
  cp -pf "${oldDefUserSettingsFile}" "${newDefUserSettingsFile}" > /dev/null 2>&1

}

migrate_directories()
{
  bupWarn=0
  once=0

  if [ -x ${newKeyDir} ] ; then 
    for i in `ls ${oldKeyDir}` ; do
      if [ ${once} = "0" ] ; then
        ${ECHO} 
        ${ECHO} "${pre}${MIG_COPY_KEYS}"
        once=1
      fi
    
      if [ -r "${newKeyDir}${i}" ] ; then
        ${ECHO} "${INFO}${MIG_BUP_SSL}${i}${backupExt}" >> $logfile
        mv "${newKeyDir}${i}" "${newKeyDir}${i}${backupExt}" > /dev/null 2>&1
      fi

      ${ECHO} "${pre}${pre2}${i}"
      cp -pf "${oldKeyDir}${i}" "${newKeyDir}" > /dev/null 2>&1
    done
  fi

	if [ ${bupWarn} = "1" ] ; then
	  ${ECHO} "${WARN}${MIG_BUP_USERS_WARNING}" >> $logfile
	fi
}

migrate_fonts()
{
	${ECHO} 
	${ECHO} "${pre}${MIG_COPY_FONTS}"

	# Copy any fonts from old dir that do not exist in new dir

	obsoleteFonts="100dpi_15 100dpi_2 75dpi_15 75dpi_2 japanese_1 unicode"
	currentFonts="100dpi 75dpi andrew big5 chinese cyrillic dec100 dec75 extra hp japanese korean misc pc siemens TTF Type1"

	excludedFonts="${currentFonts} ${obsoleteFonts}"

	for i in `ls ${oldFontDir}` ; do
	  excluded="0"

	  for e in ${excludedFonts} ; do
	    if [ ${i} = ${e} ] ; then
	      excluded="1" 
	      break
	    fi
	  done

		if [ ${excluded} = "0" ] ; then
		  if [ ! ${i} = ${userFontsDirName} ] ; then
		    if [ ! -r ${newFontDir}/${i} ] ; then
		      ${ECHO} "${pre}${pre2}${i}"
		      cp -pf -R "${oldFontDir}/${i}" "${newFontDir}/${i}" > /dev/null 2>&1
		    fi
		  fi
		fi

	done

	# Copy any fonts from the old user dir
	${ECHO} "${pre}${pre2}User fonts"
	
	for i in `ls ${oldUserFontDir}` ; do
		cp -pf -R "${oldUserFontDir}/${i}" "${newUserFontDir}/${i}" > /dev/null 2>&1
	done
}


init_and_export_variables
check_command_line_options $1 $2
get_user_confirmation

set_default_old_installation_directory
get_old_installation_directory 
  
init_more_variables
validate_old_user_dir 
  
#  Obsolete for 5.1  - hph
#	validate_old_global_profile_dir  

${ECHO} "${INFO}${MIG_FROM}${oldDir}" >> $logfile

migrate_files
migrate_directories
migrate_fonts

./sys/mig5.sh ${oldDir} ${InstallDir} ${logfile} ${newFontDir} "${excludedFonts}" 

${ECHO}

exit 0
