# #####################################################################
# Open Text Exceed onDemand Client 8
# Copyright (c) 1997-2011 Open Text Corporation. All Rights Reserved.
# #####################################################################
# xdg_setup
# Post install setup for XDG 
# $Revision: 7613 $
# #####################################################################

. utils/xdg_utils

handle_xdg_setup_icon_files()
{ 
  for thisIconFileName in ${stockIconFileNames} ; do  
    dstFile="${thisIconFileName}-${thisVer}.${iconFileExt}"
    srcFile="${thisIconFileName}.${iconFileExt}"   

    cp -f ${srcFile} ${dstFile}	# don't move because we use same file multiple times
  done
}

xdg_install_icons()
{

  handle_xdg_setup_icon_files
 
  
  # Menu icon
  try ${xdg_icon_resource} install ${thisIconTheme} ${sizeOption} ${eod_app_icon_file} ${eod_app_icon_name}

}

xdg_create_eod_desktop_file()
{
	cat > ${eod_desktop_file} << __EOD_DESKTOP_FILE__
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=${prodName}
Type=Application
TryExec=${eodxc}
Exec=${eodxc} %f
StartupNotify=false
Icon=${eod_app_icon_name}
Terminal=false
Comment=${eod_description}
Categories=Application;Network;X-Red-Hat-Base;X-SuSE-Core-Internet;X-Ximian-Main;
MimeType=${supportedMimeTypes}

__EOD_DESKTOP_FILE__
}

xdg_create_keys_file()
{
	cat > ${xdg_keys_file} << __XDG_KEYS_FILE__
${supportedMimeTypes}
        category=Packages
        description=${prodName}
        icon_filename=${OTxeodVer}
        default_action_type=application
        default_application_id=eodxc
        short_list_application_user_additions=eodxc
        use_category_default=no

__XDG_KEYS_FILE__
}

xdg_create_mime_file()
{
	cat > ${xdg_mime_file} << __XDG_MIME_FILE__
${supportedMimeTypes}
        ext: eod8

__XDG_MIME_FILE__
}

xdg_create_applications_file()
{
	cat > ${xdg_applications_file} << __XDG_APPLICATIONS_FILE__
eodxc
        command=${eodxc}
        name=${prodName}
        can_open_multiple_files=false
        requires_terminal=false
        mime_types=${supportedMimeTypes}

__XDG_APPLICATIONS_FILE__
}

xdg_start_eod_mime_file()
{
	cat > ${eod_mime_file} << _START_EOD_MIME_FILE_
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
_START_EOD_MIME_FILE_
}

xdg_end_eod_mime_file()
{
  echo "</mime-info>" >> ${eod_mime_file}
}

xdg_create_top_eod_mime_file()
{
	cat > ${eod_mime_file} << _TOP_EOD_MIME_FILE_
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="${eod_mime_type}">
    <glob pattern="*.${eodExt}"/>
    <comment>${docDescription}</comment>   
_TOP_EOD_MIME_FILE_
}

xdg_create_bottom_eod_mime_file()
{
	cat >> ${eod_mime_file} << _BOTTOM_EOD_MIME_FILE_
  </mime-type>
</mime-info>

_BOTTOM_EOD_MIME_FILE_
}

add_match_string_at_priority_to_eod_mime_file()
{
#  offsetString="0:${maxOffset}"
  offsetString="0"

	cat >> ${eod_mime_file} << _ADD_MATCH_STRING_
    <magic priority="${2}">
      <match value="${1}" type="string" offset="${offsetString}"/>
    </magic>
_ADD_MATCH_STRING_
}

add_alias_to_eod_mime_file()
{
	cat >> ${eod_mime_file} << _ADD_ALIAS_
      <alias type="${1}"/>
_ADD_ALIAS_
}

initPriority()
{
	priorityGap=0
	priorityIncrement=0
	priority=50
	incrementOp="+" 
	if [ ! "${higherPriorityForLaterVersion}" = "" ] ; then
		priorityIncrement=2
		if [ "${higherPriorityForLaterVersion}" = "-1" ] ; then
			incrementOp="-"
		fi
	fi
}

add_supported_ext_sig_to_eod_mime_file()
{
	if [ "$1" = "0" ]; then
		firstmimeonly=1
	else
		firstmimeonly=0
	fi

	allowaddsignature=1
	initPriority
	supportedVerXDG=${thisVer}
	supportedMimeTypes=${eod_mime_type}

	while [ "${supportedVerXDG}" -ge "${minSupportedVer}" ]
	do
		verStr="${supportedVerXDG}"
		if [ "${supportedVerXDG}" -lt "${minVerExt}" ] ; then
			verStr=""
			thisSupportedMimeType="${legacyMimeType}"
		else
			thisSupportedMimeType="application/x-eodclient.connectiondocument.${verStr}"
		fi
		xdgVersionSig="[EOD${verStr}CONNECTION]"
		if [ $allowaddsignature -gt 0 ] ; then
			add_match_string_at_priority_to_eod_mime_file "${xdgVersionSig}" "${priority}" 
		fi
		# only add an alias for older versions if support is enabled
		# alias will treat old mimes exactly the same as new ones and vice versa
		# this can have bad consequences if multiple client versions are installed
		if [ "${non_multimime_use_aliases}" = "1" ] ; then
			if [ "${supportedVerXDG}" -lt "${thisVer}" ] ; then
				add_alias_to_eod_mime_file "${thisSupportedMimeType}"
			fi
		fi
		supportedVerXDG=`expr ${supportedVerXDG} - 1`
		priority=`expr ${priority} ${incrementOp} ${priorityIncrement}`

		# if we only want to support one mime then we stop adding signature sections after the first
		# this is disabled if we want to have single mime support multiple extensions
		if [ $firstmimeonly -gt 0 ] ; then
			allowaddsignature=0
		fi
	done
}

xdg_create_this_mimetype_section_top()
{
#  thisMimeType="${1}"
#  thisExtension="${2}"
#  thisDescription="${3}"

	# Connection Document icon -- for each mime type we need to copy new icon file from same source
	# replace first / with -
	this_eod_mimetype_icon_name=`expr ${1/\//-}`
	echo "${this_eod_mimetype_icon_name}...."
	try ${xdg_icon_resource} install ${thisIconTheme} --context mimetypes ${sizeOption} ${eod_mimetype_icon_file} ${this_eod_mimetype_icon_name}

	cat >> ${eod_mime_file} << _MIMETYPE_SECTION_TOP_
  <mime-type type="${1}">
    <glob pattern="*.${2}"/>
    <comment>${3}</comment>
_MIMETYPE_SECTION_TOP_
}

xdg_add_this_mimetype_section_sig()
{
#  thispriority="${1}"
#  thisMatchValue="${2}"
#  thisOffset="${3}"
	cat >> ${eod_mime_file} << _MIMETYPE_SECTION_SIG_
    <magic priority="${1}">
      <match value="${2}" type="string" offset="${3}"/>
    </magic>
_MIMETYPE_SECTION_SIG_
}

xdg_close_this_mimetype_section()
{
  echo "  </mime-type>" >> ${eod_mime_file}
}

xdg_create_this_mimetype_section()
{

#  thisMimeType="${1}"
#  thisExtension="${2}"
#  thisDescription="${3}"
#  thispriority="${4}"
#  thisMatchValue="${5}"
#  thisOffset="${6}"

  xdg_create_this_mimetype_section_top "${1}" "${2}" "${3}"
  xdg_add_this_mimetype_section_sig "${4}" "${5}" "${6}"
  xdg_close_this_mimetype_section 
}

xdg_add_supported_mimetypes()
{
  add_supported_ext_sig_to_eod_mime_file "${non_multimime_use_multisigs}"
  our_version_mime_icon_name="application-x-eodclient.connectiondocument.${thisVer}"
  try ${xdg_icon_resource} install ${thisIconTheme} --context mimetypes ${sizeOption} ${eod_mimetype_icon_file} ${our_version_mime_icon_name}
}

xdg_create_eod_mime_file()
{
  xdg_create_top_eod_mime_file
  xdg_add_supported_mimetypes
  xdg_create_bottom_eod_mime_file
}

xdg_create_eod_multimime_file()
{
  xdg_start_eod_mime_file
  xdg_create_this_mimetype_sections
  xdg_end_eod_mime_file
}

xdg_create_this_mimetype_sections()
{  
  initPriority
  supportedVer=${thisVer}

  while [ "${supportedVer}" -ge "${minSupportedVer}" ]
  do
    verStr="${supportedVer}"
        
    if [ "${supportedVer}" -lt "${minVerExt}" ] ; then
      verStr=""
      thisSupportedMimeType="${legacyMimeType}"
      thisDoc="${legacyDoc}"
      
    else
      thisSupportedMimeType="application/x-eodclient.connectiondocument.${verStr}"
      thisDoc="Exceed onDemand ${verStr} Connection Document"
    fi

    if [ "${supportedVer}" = "${thisVer}" ] ; then
      supportedMimeTypes="${thisSupportedMimeType}"
    else
      supportedMimeTypes="${supportedMimeTypes};${thisSupportedMimeType}"
    fi

    thisExt="eod${verStr}"    
    thisSig="[EOD${verStr}CONNECTION]"
    supportedVer=`expr ${supportedVer} - 1`
    xdg_create_this_mimetype_section "${thisSupportedMimeType}" "${thisExt}" "${thisDoc}" "${priority}" "${thisSig}" "0"

    priority=`expr ${priority} ${incrementOp} ${priorityIncrement}`
  done  
}

xdg_install_menu_entry()
{
	xdg_create_eod_desktop_file

	try ${xdg_desktop_menu} install ${eod_desktop_file}
}

xdg_install_document_associations()
{
	xdg_create_keys_file
	xdg_create_mime_file
	xdg_create_applications_file
	mv ${xdg_keys_file} ${xdg_mime_info_dir}${xdg_keys_file}
	mv ${xdg_mime_file} ${xdg_mime_info_dir}${xdg_mime_file}
	mv ${xdg_applications_file} ${xdg_application_registry_dir}${xdg_applications_file}
}

xdg_install_mimetype_association()
{
	# ignore return code for AIX, since normally there is a really ancient version of gnome installed
	[ "${thisUname}" = "AIX" ] && SAVE_ERROR_FLAG=${DI_ERROR_OCCURED}

	if [ "${multimime}" = "1" ] ; then
		xdg_create_eod_multimime_file $1
	else
		xdg_create_eod_mime_file
	fi

	try ${xdg_mime} install ${eod_mime_file}

	# Dispensable
	try ${xdg_mime} default ${eod_desktop_file} ${eod_mime_type}

	[ "${thisUname}" = "AIX" ] && DI_ERROR_OCCURED=${SAVE_ERROR_FLAG}
}

xdg_install()
{
	if [ "$1" = "--theme" ]; then
		if [ -n "$2" ]; then
			thisIconTheme="--theme $2"
			shift
		fi
		shift
	fi
	if [ "$1" = "--iconSize" ]; then
		if [ -n "$2" ]; then
			sizeOption="--size ${2}"
			shift
		fi
		shift
	fi
	if [ "${thisIconTheme}" = "" ]; then
		thisIconTheme=${theme}
	fi
	if [ "${sizeOption}" = "" ]; then
		sizeOption="--size ${ourSize}"
	fi
	xdg_install_icons
	xdg_install_mimetype_association
	xdg_install_menu_entry
    if [ "${os_solaris}" = "true" ] ; then
	xdg_install_document_associations
	fi

}
