# #####################################################################
# Open Text Exceed onDemand Client 8
# Copyright (c) 1997-2011 Open Text Corporation. All Rights Reserved.
# #####################################################################
# common_utils
# Utility functions for setup/cleanup for both CDE and XDG
# $Revision: 7613 $
# #####################################################################

message()
{
	echo "`date`: $*"
}

try()
{
	cmd="$*"
	eval "${cmd}"
	errorcode=$?
	
	if [ ${errorcode} != 0 ]; then
		DI_ERROR_OCCURED=true
		echo "ERROR: Desktop Integration command failed:" >&2
		echo "   Error code: ${errorcode}" >&2
		echo "   Command: '${cmd}'" >&2
		return ${errorcode}
	fi
}

setupDirNames()
{
	setupDir=`pwd`
	utils_dir="${setupDir}/utils"
	oteodHome=`cd "${setupDir}/.."; pwd`
}

setupLogFiles()
{
	install_log=${setupDir}/${oteodInitial}_setup.log
	uninstall_log=/tmp/${oteodInitial}_cleanup_v${thisVer}.log
}

setEoDExtension()
{
  eodExt="eod${thisVer}"
  
  if [ "${thisVer}" -lt "${minVerExt}" ] ; then
    eodExt="eod"
  fi
}

setupMimeType()
{
	eod_mime_file=${OTeodVer}.xml
	#  xeodVer="x-eodclient.connectiondocument.${thisVer}"
	#  thisMimeType=${xeodVer}
	thisMimeType="${mimetypeVer}"
	legacyMimeType="application/x-exceedondemand"
	legacyDoc="Exceed onDemand Connection Document"

	minSupportedVer=6
	minVerExt=7
	higherPriorityForLaterVersion="-1"


	case $support_oldfiletypes_method in
	  oldfiletypes)
		# enable/disable creation of unique mime sections in our mime XML file
		multimime=0
		# we want multiple signatures in our XML in this case
		non_multimime_use_multisigs=1
		# we might want to enable aliases for old mimetypes as well
		if [ -n "${support_alias_mode}" ] ; then
			non_multimime_use_aliases=${support_alias_mode}
		else
			non_multimime_use_aliases=0
		fi
		if [ -n "${support_increase_priority}" ] ; then
			higherPriorityForLaterVersion="1"
		fi
	  ;;
	  oldfileandmimetypes)
		# enable/disable creation of unique mime sections in our mime XML file
		multimime=1
		non_multimime_use_aliases=0
		non_multimime_use_multisigs=0
	  ;;
	  default)
		# enable/disable creation of unique mime sections in our mime XML file
		multimime=0
		# by default we don't want aliase or multiple signature support enabled
		non_multimime_use_aliases=0
		non_multimime_use_multisigs=0
	  ;;
	  *)
		DI_ERROR_OCCURED=true
		echo "ERROR: Desktop Integration command failed - invalid oldfiletypes_method used." >&2
		return
	  ;;
	esac

	setEoDExtension

	if [ "${thisVer}" -lt "${minVerExt}" ] ; then
		eodExt="eod"
	fi

	docDescription="Exceed onDemand ${thisVer} Connection Document"

	eod_mime_type=application/${thisMimeType}
	maxOffset=512
}

thisIsValidVersionFile()
{
	GREPOPTIONS="-c -e"
	THISGREP="grep ${GREPOPTIONS} [^0123456789]"

	if [ "${thisUname}" = "SunOS" ] ; then
		SUNGREP=/usr/xpg4/bin/grep
		
		if [ -x ${SUNGREP} ] ; then
			THISGREP="${SUNGREP} ${GREPOPT} [^0123456789]"
		else
			return 0
		fi
		THISGREP="/usr/xpg4/bin/grep ${GREPOPTIONS} [^0123456789]"
	fi

	retVal=`cat ${1} | ${THISGREP}`

	if [ "${retVal}" -gt "0" ] ; then
	return 1
	fi

	return 0
}

getVersion()
{
	verFile=${setupDir}/${oteodInitial}.ver
	getVerMsg=""

	if [ -r ${verFile} ] ; then
		if thisIsValidVersionFile "${verFile}" ; then
			thisVer=`cat ${verFile}`
		else
			getVerMsg="invalid"
		fi
	else
		getVerMsg="missing"
	fi
	if [ ! "${getVerMsg}" = "" ] ; then
		echo "${verFile} is ${getVerMsg}."
		exit
	fi
}

setupVersionSpecificNames()
{
	prodName="${EoDC} ${thisVer}"
	x_e_o_dVer=${x_e_o_d}-${thisVer}
	eod_name_ver=${eod_name}-${thisVer}

	#eodVer="eod-${thisVer}"
	#eodVer="eodclient.connectiondocument.${thisVer}"
	#xeodVer="x-${eodVer}"
	#xeodVer="x-eodclient.connectiondocument.${thisVer}"
	mimetypeVer="x-eodclient.connectiondocument.${thisVer}"

	OTeodVer=${vendor}-eod-${thisVer}
	OTxeodVer=${vendor}-x-eod-${thisVer}
}

setupVendorProdNames()
{
	Vendor="Open Text"
	vendor="opentext"

	EoDC="Exceed onDemand Client"
	oteodName="${Vendor} ${EoDC}"
	oteodInitial="oteod"
  ot_x_eod="ot-x-eod"
	thisUname=`uname`
	export thisUname

	e_o_d=exceedondemand
	x_e_o_d="x-${e_o_d}"
	eod_name=${vendor}-${e_o_d} 
}

setupVarNames()
{
	setupDirNames
	setupVendorProdNames
	getVersion
	setupVersionSpecificNames
	setup_xdgDesktopFile
	setup_xdgDocumentAssociationsFiles
	setupMimeType
	setup_xdgMimeFile
	setup_xdgStockIconFileNames
	setupLogFiles
	eod_description="Run X Window applications from an Exceed Connection Server."
}

setupForAIX()
{
	support_cde=true

	# IBM's gnome installation on AIX uses a "gnome" theme name
	theme="--theme gnome"

	# AIX 5.x puts gnome/kde under /opt/freeware by default
	dataDirs=/usr/local/share:/usr/share:/opt/freeware/share

	XDG_DATA_DIRS=${dataDirs}
	export XDG_DATA_DIRS
	GNOME_DATA_DIRS=${dataDirs}
	export GNOME_DATA_DIRS
	kdeBinDir=/opt/freeware/kde/bin

	if [ -x ${kdeBinDir}/kde${KDE_SESSION_VERSION}-config ] ; then
		PATH=${PATH}:${kdeBinDir}
	fi
}

setupForSolaris()
{
	support_cde="true"
	support_kde="false"
	os_solaris="true"
	PATH=${PATH}:/usr/ucb
	XDG_DATA_DIRS=/usr/share
	export XDG_DATA_DIRS
}

setup()
{
	thisOp=${1}
	shift

	# default to 48x48
	ourSize=48

	setupVarNames

	# set by try() if an error occurs
	DI_ERROR_OCCURED=

	# cde is only enabled on AIX and Solaris, for now
	support_cde="false"

	# xdg means KDE/Gnome/Xfce support
	support_xdg="true"
	XDG_UTILS_DEBUG_LEVEL=9
	export XDG_UTILS_DEBUG_LEVEL

	if [ "${thisUname}" = "AIX" ]; then
		setupForAIX
	fi

	if [ "${thisUname}" = "SunOS" ] ; then
		setupForSolaris
		WHOAMI=`/usr/ucb/whoami`
	else
		WHOAMI=`whoami`
	fi

	export WHOAMI
	
	# prefer system-installed xdg-utils; use ours only as a fallback
	PATH=${PATH}:${setupDir}
	export PATH
	
	try cd "${setupDir}" || exit 1
}

wrapup()
{
	try cd ${startingDir}

	if [ "${DI_ERROR_OCCURED}" = "true" ]; then
		echo "${fail_msg}" >&2 
		exit 1

	else
		# only leave uninstall log around if we had an error
		[ "$1" = "uninstall" ] && rm -f "${uninstall_log}"
		exit 0
	fi
}
