#! /bin/bash
# postinst script for open-xchange
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

. /opt/open-xchange/etc/oxfunctions.sh


postFix() {
  local version=${1%-*}
  version=${version//[-.]/}

  # prevent bash from expanding, see bug 13316
  GLOBIGNORE='*'
  
  # SoftwareChange_Request-618
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/imap.properties
  if ! ox_exists_property com.openexchange.imap.propagateHostNames $pfile; then
      ox_set_property com.openexchange.imap.propagateHostNames '' $pfile
  fi

  # SoftwareChange_Request-614
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/mail.properties
  if ! ox_exists_property com.openexchange.mail.hidePOP3StorageFolders $pfile; then
      ox_set_property com.openexchange.mail.hidePOP3StorageFolders false $pfile
  fi

  # SoftwareChange_Request-602
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/servletmappings/servletmapping.properties
  local ptmp=${pfile}.$$
  if grep -E "^/ajax/login" $pfile > /dev/null; then
      grep -vE "^/ajax/login" $pfile > $ptmp
      if [ -s $ptmp ]; then
	  cp $ptmp $pfile
      fi
      rm -f $ptmp
  fi

  # SoftwareChange_Request-568
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/server.properties
  if ox_exists_property FileStorageImpl $pfile; then
      ox_remove_property FileStorageImpl $pfile
  fi

  # SoftwareChange_Request-570
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/server.properties
  if ! ox_exists_property com.openexchange.cookie.hash $pfile; then
      ox_set_property com.openexchange.cookie.hash calculate $pfile
  fi

  # SoftwareChange_Request-565
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/whitelist.properties
  if ! ox_exists_property html.tag.i $pfile; then
      ox_set_property html.tag.i '""' $pfile
  fi

  # SoftwareChange_Request-561
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/mail.properties
  if ox_exists_property com.openexchange.mail.maxNumOfConnections $pfile; then
      ox_remove_property com.openexchange.mail.maxNumOfConnections $pfile
  fi

  # SoftwareChange_Request-537
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/sessiond.properties
  if ! ox_exists_property com.openexchange.sessiond.encryptionKey $pfile; then
      ox_set_property com.openexchange.sessiond.encryptionKey "auw948cz,spdfgibcsp9e8ri+<#qawcghgifzign7c6gnrns9oysoeivn" $pfile
  fi

  # SoftwareChange_Request-532
  # -----------------------------------------------------------------------
  local smtpc=/opt/open-xchange/etc/groupware/smtp.properties
  local mailc=/opt/open-xchange/etc/groupware/mail.properties
  local oval=0
  if ox_exists_property com.openexchange.smtp.smtpRateLimit $smtpc; then
      local oval=$(ox_read_property com.openexchange.smtp.smtpRateLimit $smtpc)
      ox_remove_property com.openexchange.smtp.smtpRateLimit $smtpc
  fi
  if ! ox_exists_property com.openexchange.mail.rateLimit $mailc; then
      ox_set_property com.openexchange.mail.rateLimit $oval $mailc
  fi
  if ! ox_exists_property com.openexchange.mail.rateLimitPrimaryOnly $mailc; then
      ox_set_property com.openexchange.mail.rateLimitPrimaryOnly true $mailc
  fi
  if ! ox_exists_property com.openexchange.mail.maxToCcBcc $mailc; then
      ox_set_property com.openexchange.mail.maxToCcBcc 0 $mailc
  fi

  # SoftwareChange_Request-519
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/file-logging.properties
  if ! ox_exists_property com.openexchange.login.internal.LoginPerformer.level $pfile; then
      ox_set_property com.openexchange.login.internal.LoginPerformer.level INFO $pfile
  fi
  if ! ox_exists_property com.openexchange.sessiond.impl.SessionHandler.level $pfile; then
      ox_set_property com.openexchange.sessiond.impl.SessionHandler.level INFO $pfile
  fi

  # SoftwareChange_Request-515
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/server.properties
  if ! ox_exists_property com.openexchange.cookie.httpOnly $pfile; then
      ox_set_property com.openexchange.cookie.httpOnly true $pfile
  fi

  # SoftwareChange_Request-511 / Bugfix #17523
  # -----------------------------------------------------------------------
  if [ -e /opt/open-xchange/etc/groupware/excludedupdatetasks.properties ]; then
      if ! cmp /opt/open-xchange/etc/groupware/excludedupdatetasks.properties /opt/open-xchange/etc/common/excludedupdatetasks.properties >/dev/null; then
	  mv /opt/open-xchange/etc/groupware/excludedupdatetasks.properties /opt/open-xchange/etc/common/excludedupdatetasks.properties
      else
	  rm -f /opt/open-xchange/etc/groupware/excludedupdatetasks.properties
      fi
  fi

  # SoftwareChange_Request-505
  # -----------------------------------------------------------------------
  local sessionc=/opt/open-xchange/etc/groupware/sessiond.properties
  local serverc=/opt/open-xchange/etc/groupware/server.properties
  local ajpc=/opt/open-xchange/etc/groupware/ajp.properties
  local oval=1W
  if ox_exists_property com.openexchange.sessiond.cookie.ttl $sessionc; then
      local oval=$(ox_read_property com.openexchange.sessiond.cookie.ttl $sessionc)
      ox_remove_property com.openexchange.sessiond.cookie.ttl $sessionc
  fi
  if ! ox_exists_property com.openexchange.cookie.ttl $serverc; then
      ox_set_property com.openexchange.cookie.ttl $oval $serverc
  fi
  if ox_exists_property AJP_JSESSIONID_TTL $ajpc; then
      ox_remove_property AJP_JSESSIONID_TTL $ajpc
  fi

  # SoftwareChange_Request-490
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/servletmappings/servletmapping.properties
  local ptmp=${pfile}.$$
  if grep -E "^/ajax/infostore" $pfile > /dev/null; then
      grep -vE "^/ajax/infostore" $pfile > $ptmp
      if [ -s $ptmp ]; then
	  cp $ptmp $pfile
      fi
      rm -f $ptmp
  fi

  # SoftwareChange_Request-499
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/ox-scriptconf.sh
  local jopts=$(eval ox_read_property JAVA_XTRAOPTS $pfile)
  jopts=${jopts//\"/}
  if ! echo $jopts | grep "sun.net.inetaddr.ttl" > /dev/null; then
      ox_set_property JAVA_XTRAOPTS \""$jopts -Dsun.net.inetaddr.ttl=3600 -Dnetworkaddress.cache.ttl=3600 -Dnetworkaddress.cache.negative.ttl=10"\" $pfile
  fi

  # SoftwareChange_Request-498
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/mime.types
  if ! grep "openxmlformats-officedocument" >/dev/null $pfile; then
      echo "application/vnd.openxmlformats-officedocument.wordprocessingml.document docx" >> $pfile
  fi

  # SoftwareChange_Request-479
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/smtp.properties
  if ! ox_exists_property com.openexchange.smtp.smtpRateLimit $pfile; then
      ox_set_property com.openexchange.smtp.smtpRateLimit 0 $pfile
  fi

  # SoftwareChange_Request-473
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/sessiond.properties
  for prop in com.openexchange.sessiond.isServerSocketEnabled com.openexchange.sessiond.isServerObjectStreamSocketEnabled com.openexchange.sessiond.serverPort com.openexchange.sessiond.serverObjectStreamPort com.openexchange.sessiond.isTcpClientSocketEnabled com.openexchange.sessiond.serverBindAddress com.openexchange.sessiond.isDoubleLoginPermitted com.openexchange.sessiond.sessionAuthUser com.openexchange.sessiond.isSecureSocketConnectionEnabled com.openexchange.sessiond.caFile com.openexchange.sessiond.certFile com.openexchange.sessiond.keyFile com.openexchange.sessiond.sessionContainerTimeout com.openexchange.sessiond.numberOfSessionContainers; do
      if ox_exists_property $prop $pfile; then
	  ox_remove_property $prop $pfile
      fi
  done
  if ! ox_exists_property com.openexchange.sessiond.randomTokenTimeout $pfile; then
      ox_set_property com.openexchange.sessiond.randomTokenTimeout 1M $pfile
  fi
  if ! ox_exists_property com.openexchange.sessiond.sessionLongLifeTime $pfile; then
      ox_set_property com.openexchange.sessiond.sessionLongLifeTime 1W $pfile
  fi
  local pfile=/opt/open-xchange/etc/groupware/system.properties
  if ox_exists_property SESSIONDPROPERTIES $pfile; then
      ox_remove_property SESSIONDPROPERTIES $pfile
  fi

  # SoftwareChange_Request-378
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/imap.properties
  if ! ox_exists_property com.openexchange.imap.propagateClientIPAddress $pfile; then
      ox_set_property com.openexchange.imap.propagateClientIPAddress false $pfile
  fi

  # SoftwareChange_Request-371
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/ox-scriptconf.sh
  local jopts=$(eval ox_read_property JAVA_XTRAOPTS $pfile)
  jopts=${jopts//\"/}
  if ! echo $jopts | grep "MaxPermSize" > /dev/null; then
      ox_set_property JAVA_XTRAOPTS \""$jopts -XX:MaxPermSize=128M"\" $pfile
  fi

  # SoftwareChange_Request-354
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/mail.properties
  if ! ox_exists_property com.openexchange.mail.addClientIPAddress $pfile; then
      ox_set_property com.openexchange.mail.addClientIPAddress false $pfile
  fi

  # SoftwareChange_Request-334
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/sessiond.properties
  if ! ox_exists_property com.openexchange.sessiond.autologin $pfile; then
      ox_set_property com.openexchange.sessiond.autologin false $pfile
  fi
  # obsoleted by SoftwareChange_Request-505
  #  if ! ox_exists_property com.openexchange.sessiond.cookie.ttl $pfile; then
  #      ox_set_property com.openexchange.sessiond.cookie.ttl 1W $pfile
  #  fi

  # SoftwareChange_Request-341
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/contact.properties
  if ! ox_exists_property com.openexchange.contacts.allFoldersForAutoComplete $pfile; then
      ox_set_property com.openexchange.contacts.allFoldersForAutoComplete true $pfile
  fi

  # SoftwareChange_Request-308
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/whitelist.properties
  for prop in html.tag.dd html.tag.dt; do
      if ! ox_exists_property $prop $pfile; then
	  ox_set_property $prop '""' $pfile
      fi
  done

  # SoftwareChange_Request-294
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/mailcache.ccf
  local ptmp=${pfile}.$$
  if grep -E "^jcs.default" $pfile > /dev/null; then
      grep -vE "^jcs.default" $pfile > $ptmp
      if [ -s $ptmp ]; then
	  cp $ptmp $pfile
      fi
      rm -f $ptmp
  fi

  # SoftwareChange_Request-293
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/notification.properties
  if ! ox_exists_property com.openexchange.notification.fromSource $pfile; then
      ox_set_property com.openexchange.notification.fromSource "primaryMail" $pfile
  fi

  # SoftwareChange_Request-285
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/server.properties
  if ! ox_exists_property com.openexchange.UIWebPath $pfile; then
      ox_set_property com.openexchange.UIWebPath "/ox6/index.html" $pfile
  fi

  # SoftwareChange_Request-266
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/user.properties
  if ! ox_exists_property com.openexchange.folder.tree $pfile; then
      ox_set_property com.openexchange.folder.tree 0 $pfile
  fi

  # Property to disable iCal attachment for iMIP mail messages to internal users.
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/notification.properties
  if ! ox_exists_property imipForInternalUser $pfile; then
      ox_set_property imipForInternalUser false $pfile
  fi

  # SoftwareChange_Request-194
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/cache.ccf
  local val=0$(ox_read_property jcs.region.User.cacheattributes.MaxObjects $pfile)
  if [ $val -lt 40000 ]; then
      ox_set_property jcs.region.User.cacheattributes.MaxObjects 40000 $pfile
  fi
  local val=0$(ox_read_property jcs.region.UserConfiguration.cacheattributes.MaxObjects $pfile)
  if [ $val -lt 20000 ]; then
      ox_set_property jcs.region.UserConfiguration.cacheattributes.MaxObjects 20000 $pfile
  fi
  local val=0$(ox_read_property jcs.region.UserSettingMail.cacheattributes.MaxObjects $pfile)
  if [ $val -lt 20000 ]; then
      ox_set_property jcs.region.UserSettingMail.cacheattributes.MaxObjects 20000 $pfile
  fi
  local val=0$(ox_read_property jcs.region.OXDBPoolCache.cacheattributes.MaxObjects $pfile)
  if [ $val -lt 20000 ]; then
      ox_set_property jcs.region.OXDBPoolCache.cacheattributes.MaxObjects 20000 $pfile
  fi
  local val=0$(ox_read_property jcs.region.MailAccount.cacheattributes.MaxObjects $pfile)
  if [ $val -lt 100000 ]; then
      ox_set_property jcs.region.MailAccount.cacheattributes.MaxObjects 100000 $pfile
  fi

  # SoftwareChange_Request-131
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/server.properties
  if ! ox_exists_property com.openexchange.IPCheck $pfile; then
      ox_set_property com.openexchange.IPCheck true $pfile
  fi

  # SoftwareChange_Request-124
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/cache.ccf
  grep jcs.region.GlobalFolderCache $pfile >/dev/null || {
cat<<EOF >> $pfile
# Pre-defined cache regions for global folder objects.
jcs.region.GlobalFolderCache=LTCP
jcs.region.GlobalFolderCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.GlobalFolderCache.cacheattributes.MaxObjects=10000000
jcs.region.GlobalFolderCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.GlobalFolderCache.cacheattributes.UseMemoryShrinker=true
# Disable MaxMemoryIdleTimeSeconds cause some entries can be eternal
# Shrinker removal works as follows:
# 1. Check 'Eternal', 'MaxLifeSeconds' AND 'IdleTime' for element-attribute-caused removal
# 2. Check 'MaxMemoryIdleTime' for cache-attribute-caused removal
jcs.region.GlobalFolderCache.cacheattributes.MaxMemoryIdleTimeSeconds=180
jcs.region.GlobalFolderCache.cacheattributes.ShrinkerIntervalSeconds=60
jcs.region.GlobalFolderCache.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.GlobalFolderCache.elementattributes.IsEternal=false
jcs.region.GlobalFolderCache.elementattributes.MaxLifeSeconds=300
jcs.region.GlobalFolderCache.elementattributes.IdleTime=180
jcs.region.GlobalFolderCache.elementattributes.IsSpool=false
jcs.region.GlobalFolderCache.elementattributes.IsRemote=false
jcs.region.GlobalFolderCache.elementattributes.IsLateral=false
EOF
}
  grep jcs.region.UserFolderCache $pfile >/dev/null || {
cat<<EOF >> $pfile
# Pre-defined cache regions for user-sensitive folder objects.
jcs.region.UserFolderCache=LTCP
jcs.region.UserFolderCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.UserFolderCache.cacheattributes.MaxObjects=10000000
jcs.region.UserFolderCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.UserFolderCache.cacheattributes.UseMemoryShrinker=true
# Disable MaxMemoryIdleTimeSeconds cause some entries can be eternal
# Shrinker removal works as follows:
# 1. Check 'Eternal', 'MaxLifeSeconds' AND 'IdleTime' for element-attribute-caused removal
# 2. Check 'MaxMemoryIdleTime' for cache-attribute-caused removal
jcs.region.UserFolderCache.cacheattributes.MaxMemoryIdleTimeSeconds=180
jcs.region.UserFolderCache.cacheattributes.ShrinkerIntervalSeconds=60
jcs.region.UserFolderCache.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.UserFolderCache.elementattributes.IsEternal=false
jcs.region.UserFolderCache.elementattributes.MaxLifeSeconds=300
jcs.region.UserFolderCache.elementattributes.IdleTime=180
jcs.region.UserFolderCache.elementattributes.IsSpool=false
jcs.region.UserFolderCache.elementattributes.IsRemote=false
jcs.region.UserFolderCache.elementattributes.IsLateral=false
EOF
}
  grep jcs.region.MailAccount $pfile >/dev/null || {
cat<<EOF >> $pfile
# Pre-defined cache region for mail account
jcs.region.MailAccount=LTCP
jcs.region.MailAccount.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.MailAccount.cacheattributes.MaxObjects=1000
jcs.region.MailAccount.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.MailAccount.cacheattributes.UseMemoryShrinker=true
jcs.region.MailAccount.cacheattributes.MaxMemoryIdleTimeSeconds=180
jcs.region.MailAccount.cacheattributes.ShrinkerIntervalSeconds=60
jcs.region.MailAccount.cacheattributes.MaxSpoolPerRun=500
jcs.region.MailAccount.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.MailAccount.elementattributes.IsEternal=false
jcs.region.MailAccount.elementattributes.MaxLifeSeconds=300
jcs.region.MailAccount.elementattributes.IdleTime=180
jcs.region.MailAccount.elementattributes.IsSpool=false
jcs.region.MailAccount.elementattributes.IsRemote=false
jcs.region.MailAccount.elementattributes.IsLateral=false
EOF
}

  # SoftwareChange_Request-125
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/server.properties
  if ! ox_exists_property PUBLISH_REVOKE $pfile; then
      ox_set_property PUBLISH_REVOKE "" $pfile
  fi

  # SoftwareChange_Request-109 / SoftwareChange_Request-104
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/calendar.properties
  if ! ox_exists_property com.openexchange.calendar.undefinedstatusconflict $pfile; then
      ox_set_property com.openexchange.calendar.undefinedstatusconflict true $pfile
  fi
  if ! ox_exists_property com.openexchange.calendar.seriesconflictlimit $pfile; then
      ox_set_property com.openexchange.calendar.seriesconflictlimit true $pfile
  fi

  # SoftwareChange_Request-84
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/TidyConfiguration.properties
  if ox_exists_property clean $pfile; then
      ox_remove_property clean $pfile
  fi

  # SoftwareChange_Request-70
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/system.properties
  if ox_exists_property configDB $pfile; then
      ox_remove_property configDB $pfile
  fi

  # SoftwareChange_Request-62 / Bugfix #13477
  # -----------------------------------------------------------------------
  if [ -e /opt/open-xchange/etc/groupware/foldercache.properties ]; then
      if ! cmp /opt/open-xchange/etc/groupware/foldercache.properties /opt/open-xchange/etc/common/foldercache.properties >/dev/null; then
	  mv /opt/open-xchange/etc/groupware/foldercache.properties /opt/open-xchange/etc/common/foldercache.properties
      else
	  rm -f /opt/open-xchange/etc/groupware/foldercache.properties
      fi
  fi
  
  # SoftwareChange_Request-55
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/mail.properties
  if ! ox_exists_property com.openexchange.mail.mailAccessCacheShrinkerSeconds $pfile; then
      ox_set_property com.openexchange.mail.mailAccessCacheShrinkerSeconds 3 $pfile
  fi
  if ! ox_exists_property com.openexchange.mail.mailAccessCacheIdleSeconds $pfile; then
      ox_set_property com.openexchange.mail.mailAccessCacheIdleSeconds 7 $pfile
  fi
  
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/ox-scriptconf.sh
  if ! ox_exists_property UMASK $pfile; then
      ox_set_property UMASK 066 $pfile
  fi
  # bugfix id#13928
  # -----------------------------------------------------------------------
  if ! ox_exists_property COMMONPROPERTIESDIR $pfile; then
      ox_set_property COMMONPROPERTIESDIR /opt/open-xchange/etc/common $pfile
  fi

  # bugfix id#13313
  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/whitelist.properties
  if ! ox_exists_property html.tag.base $pfile; then
      ox_set_property html.tag.base \"",href\"" $pfile
  fi

  # -----------------------------------------------------------------------
  local pfile=/opt/open-xchange/etc/groupware/contact.properties
  if ! ox_exists_property com.openexchange.contact.singleFolderSearch $pfile; then
      ox_set_property com.openexchange.contact.singleFolderSearch false $pfile
  fi

  # -----------------------------------------------------------------------
  # bugfix id#12517
  local pfile=/opt/open-xchange/etc/groupware/cache.ccf
  if ! ox_exists_property jcs.region.OXFolderCache.elementattributes.IsLateral $pfile; then
      ox_set_property jcs.region.OXFolderCache.elementattributes.IsLateral false $pfile
  else
      local oldval=$(ox_read_property jcs.region.OXFolderCache.elementattributes.IsLateral $pfile)
      if [ "$oldval" != "false" ]; then
	  ox_set_property jcs.region.OXFolderCache.elementattributes.IsLateral false $pfile
      fi
  fi

  # we're updating from pre sp4
  if [ $version -lt 650 ]; then

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/configdb.properties
      if ox_exists_property testThreads $pfile; then
	  local val=$(ox_read_property testThreads $pfile)
	  if [ "$val" != "false" ]; then
	      ox_set_property testThreads false $pfile
	  fi
      fi


      # did not exist in SP3
      chown -R open-xchange: /opt/open-xchange/etc/groupware/osgi || true

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/ox-scriptconf.sh
      ox_exists_property PROPERTIESDIR $pfile || {
	  ox_set_property PROPERTIESDIR /opt/open-xchange/etc/groupware $pfile
      }
      ox_exists_property OSGIPATH $pfile || {
	  ox_set_property OSGIPATH /opt/open-xchange/etc/groupware/osgi $pfile
      }
      
      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/system.properties
      ox_exists_property com.openexchange.caching.configfile $pfile || {
	  ox_set_property com.openexchange.caching.configfile /opt/open-xchange/etc/groupware/cache.ccf $pfile
      }
      ox_exists_property serviceUsageInspection $pfile || {
	  ox_set_property serviceUsageInspection false $pfile
      }
      ox_exists_property serviceUsageTimeout $pfile || {
	  ox_set_property serviceUsageTimeout 10000 $pfile
      }
      ox_exists_property MimeTypeFile $pfile || {
	  ox_set_property MimeTypeFile /opt/open-xchange/etc/groupware/mime.types $pfile
      }
      ox_exists_property MailCacheConfig $pfile || {
	  ox_set_property MailCacheConfig /opt/open-xchange/etc/groupware/mailcache.ccf $pfile
      }
      ox_exists_property TidyMessages $pfile || {
	  ox_set_property TidyMessages /opt/open-xchange/etc/groupware/TidyMessages.properties $pfile
      }
      ox_exists_property TidyConfiguration $pfile || {
	  ox_set_property TidyConfiguration /opt/open-xchange/etc/groupware/TidyConfiguration.properties $pfile
      }
      ox_exists_property Whitelist $pfile || {
	  ox_set_property Whitelist /opt/open-xchange/etc/groupware/whitelist.properties $pfile
      }

      ox_exists_property LoginInfo $pfile && {
	  ox_remove_property LoginInfo $pfile
      } || true
      ox_exists_property SetupLink $pfile && {
	  ox_remove_property SetupLink $pfile
      } || true
      ox_exists_property ConfigJumpConf $pfile && {
	  ox_remove_property ConfigJumpConf $pfile
      } || true
      ox_exists_property User2IMAPImpl $pfile && {
	  ox_remove_property User2IMAPImpl $pfile
      } || true
      ox_exists_property WEBDAVOVERRIDES $pfile && {
	  ox_remove_property WEBDAVOVERRIDES $pfile
      } || true
      ox_exists_property IMAPPROPERTIES $pfile && {
	  ox_remove_property IMAPPROPERTIES $pfile
      } || true
      ox_exists_property JAVAMAILPROPERTIES $pfile && {
	  ox_remove_property JAVAMAILPROPERTIES $pfile
      } || true

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/imap.properties
      local pftemp=${pfile}.$$
      local modprops="imapSort\|imapSearch\|imapFastFetch\|imapSupportsACL\|imapTimeout\|imapConnectionTimeout\|imapAuthEnc\|maxIMAPConnectionIdleTime"
      sed -e "s;^\($modprops\);com.openexchange.imap.\1;" < $pfile > $pftemp
      if [ ! -s $pftemp ]; then
	  echo "unable to append com.openexchange.imap. to $modprops in $pfile"
	  rm -f $pftemp
      else
	  mv $pftemp $pfile
      fi
      ox_exists_property com.openexchange.imap.User2ACLImpl $pfile || {
	  ox_set_property com.openexchange.imap.User2ACLImpl auto $pfile
      }
      ox_exists_property com.openexchange.imap.mboxEnabled $pfile || {
	  ox_set_property com.openexchange.imap.mboxEnabled false $pfile
      }
      ox_exists_property com.openexchange.imap.blockSize $pfile || {
	  ox_set_property com.openexchange.imap.blockSize 1000 $pfile
      }

      # -----------------------------------------------------------------------
      # move key/values from imap.properties to mail.properties
      local imap=/opt/open-xchange/etc/groupware/imap.properties
      local mail=/opt/open-xchange/etc/groupware/mail.properties
      local smtp=/opt/open-xchange/etc/groupware/smtp.properties
      ox_exists_property imapLoginType $imap && {
	  local val=$(ox_read_property imapLoginType $imap)
	  ox_remove_property imapLoginType $imap
	  ox_set_property com.openexchange.mail.loginType $val $mail
      } || true
      ox_exists_property imapMasterPassword $imap && {
	  local val=$(ox_read_property imapMasterPassword $imap)
	  ox_remove_property imapMasterPassword $imap
	  ox_set_property com.openexchange.mail.masterPassword $val $mail
      } || true
      ox_exists_property imapCredSrc $imap && {
	  local val=$(ox_read_property imapCredSrc $imap)
	  ox_remove_property imapCredSrc $imap
	  ox_set_property com.openexchange.mail.credSrc $val $mail
      } || true
      ox_exists_property imapServer $imap && {
	  local val=$(ox_read_property imapServer $imap)
	  ox_remove_property imapServer $imap
	  ox_set_property com.openexchange.mail.mailServer $val $mail
      } || true
      ox_exists_property smtpServer $imap && {
	  local val=$(ox_read_property smtpServer $imap)
	  ox_remove_property smtpServer $imap
	  ox_set_property com.openexchange.mail.transportServer $val $mail
      } || true
      ox_exists_property mail.mime.charset $imap && {
	  local val=$(ox_read_property mail.mime.charset $imap)
	  ox_remove_property mail.mime.charset $imap
	  ox_set_property mail.mime.charset $val $mail
      } || true
      ox_exists_property imapMessageFetchLimit $imap && {
	  local val=$(ox_read_property imapMessageFetchLimit $imap)
	  ox_remove_property imapMessageFetchLimit $imap
	  ox_set_property com.openexchange.mail.mailFetchLimit $val $mail
      } || true
      ox_exists_property imapAttachmentDisplaySizeLimit $imap && {
	  local val=$(ox_read_property imapAttachmentDisplaySizeLimit $imap)
	  ox_remove_property imapAttachmentDisplaySizeLimit $imap
	  ox_set_property com.openexchange.mail.attachmentDisplaySizeLimit $val $mail
      } || true
      ox_exists_property imapMaxNumOfConnections $imap && {
	  local val=$(ox_read_property imapMaxNumOfConnections $imap)
	  ox_remove_property imapMaxNumOfConnections $imap
	  ox_set_property com.openexchange.mail.maxNumOfConnections $val $mail
      } || true
      ox_exists_property imapQuoteLineColors $imap && {
	  local val=$(ox_read_property imapQuoteLineColors $imap)
	  ox_remove_property imapQuoteLineColors $imap
	  ox_set_property com.openexchange.mail.quoteLineColors $val $mail
      } || true
      for key in userFlagsEnabled allowNestedDefaultFolderOnAltNamespace \
	  ignoreSubscription watcherEnabled watcherFrequency watcherTime \
	  watcherShallClose; do
	  ox_exists_property $key $imap && {
	      local val=$(ox_read_property $key $imap)
	      ox_remove_property $key $imap
	      ox_set_property "com.openexchange.mail.${key}" $val $mail
	  } || true
      done
      for key in smtpLocalhost smtpAuthentication setSMTPEnvelopeFrom; do
	  ox_exists_property $key $imap && {
	      local val=$(ox_read_property $key $imap)
	      ox_remove_property $key $imap
	      ox_set_property "com.openexchange.smtp.${key}" $val $smtp
	  } || true
      done
      # delete non-existing params
      ox_exists_property partModifierImpl $imap && {
	  ox_remove_property partModifierImpl $imap
      } || true
      ox_exists_property imaps $imap && {
	  ox_remove_property imaps $imap
      } || true
      ox_exists_property smtps $imap && {
	  ox_remove_property smtps $imap
      } || true
      ox_exists_property spamEnabled $imap && {
	  ox_remove_property spamEnabled $imap
      } || true
      ox_exists_property imapMessageFetchLimit $imap && {
	  ox_remove_property imapMessageFetchLimit $imap
      } || true
      ox_exists_property imapAttachmentDisplaySizeLimit $imap && {
	  ox_remove_property imapAttachmentDisplaySizeLimit $imap
      } || true
      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/server.properties
      ox_exists_property JMXLogin $pfile || {
	  ox_set_property JMXLogin "" $pfile
      }
      ox_exists_property JMXPassword $pfile || {
	  ox_set_property JMXPassword "" $pfile
      }
  fi

  # -----------------------------------------------------------------------
  # bugfix id#12290
  local pfile=/opt/open-xchange/etc/groupware/ajp.properties
  if ! ox_exists_property AJP_LOG_FORWARD_REQUEST $pfile; then
      ox_set_property AJP_LOG_FORWARD_REQUEST FALSE $pfile
  fi

  # -----------------------------------------------------------------------
  # bugfix id#12291
  local pfile=/opt/open-xchange/etc/groupware/configdb.properties
  if ! ox_exists_property writeOnly $pfile; then
      ox_set_property writeOnly false $pfile
  fi

  # -----------------------------------------------------------------------
  # bugfix id#12292
  local pfile=/opt/open-xchange/etc/groupware/imap.properties
  if ! ox_exists_property com.openexchange.imap.imapTemporaryDown $pfile; then
      ox_set_property com.openexchange.imap.imapTemporaryDown 10000 $pfile
  fi
  for prop in imapsPort smtpsPort; do
      if ox_exists_property $prop $pfile; then
	  ox_remove_property $prop $pfile
      fi
  done
  if ! ox_exists_property com.openexchange.imap.spamHandler $pfile; then
      ox_set_property com.openexchange.imap.spamHandler DefaultSpamHandler $pfile
  fi

  # -----------------------------------------------------------------------
  # bugfix id#12296
  local pfile=/opt/open-xchange/etc/groupware/system.properties
  if ox_exists_property CACHECCF $pfile; then
      ox_remove_property CACHECCF $pfile
  fi

  # -----------------------------------------------------------------------
  # bugfix id#12295
  local pfile=/opt/open-xchange/etc/groupware/server.properties
  for prop in JMXPort JMXBindAddress; do
      if ox_exists_property "Monitor${prop}" $pfile; then
	  local oldval=$(ox_read_property "Monitor${prop}" $pfile)
	  ox_set_property $prop $oldval $pfile
	  ox_remove_property "Monitor${prop}" $pfile
      fi
  done

  # we're updating from pre sp5
  if [ $version -le 670 ]; then

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/mail.properties
      if ! ox_exists_property com.openexchange.mail.loginSource $pfile; then
	  local ltype=$(ox_read_property com.openexchange.mail.loginType $pfile)
	  local credsrc=$(ox_read_property com.openexchange.mail.credSrc $pfile)
	  if [ -n "$ltype" ] && [ -n "$credsrc" ]; then
	      if [ "$ltype" == "user" ] && [ "$credsrc" == "user.imapLogin" ]; then
		  ox_set_property com.openexchange.mail.loginSource "login" $pfile
		  ox_set_property com.openexchange.mail.passwordSource "session" $pfile
		  ox_set_property com.openexchange.mail.mailServerSource "user" $pfile
		  ox_set_property com.openexchange.mail.transportServerSource "user" $pfile
	      elif [ "$ltype" == "user" ] && [ "$credsrc" == "session" ]; then
		  ox_set_property com.openexchange.mail.loginSource "name" $pfile
		  ox_set_property com.openexchange.mail.passwordSource "session" $pfile
		  ox_set_property com.openexchange.mail.mailServerSource "user" $pfile
		  ox_set_property com.openexchange.mail.transportServerSource "user" $pfile
	      elif [ "$ltype" == "global" ]; then
		  ox_set_property com.openexchange.mail.loginSource "mail" $pfile
		  ox_set_property com.openexchange.mail.passwordSource "global" $pfile
		  ox_set_property com.openexchange.mail.mailServerSource "global" $pfile
		  ox_set_property com.openexchange.mail.transportServerSource "global" $pfile
	      elif [ "$ltype" == "config" ]; then
		  ox_set_property com.openexchange.mail.loginSource "mail" $pfile
		  ox_set_property com.openexchange.mail.passwordSource "session" $pfile
		  ox_set_property com.openexchange.mail.mailServerSource "global" $pfile
		  ox_set_property com.openexchange.mail.transportServerSource "global" $pfile
	      fi
	  else
	      # defaults
	      ox_set_property com.openexchange.mail.loginSource "login" $pfile
	      ox_set_property com.openexchange.mail.passwordSource "session" $pfile
	      ox_set_property com.openexchange.mail.mailServerSource "user" $pfile
	      ox_set_property com.openexchange.mail.transportServerSource "user" $pfile
	  fi
	  ox_remove_property com.openexchange.mail.loginType $pfile
	  ox_remove_property com.openexchange.mail.credSrc $pfile
      fi

      # -----------------------------------------------------------------------
      # obsoleted by SoftwareChange_Request-505
      # local pfile=/opt/open-xchange/etc/groupware/ajp.properties
      # if ! ox_exists_property AJP_JSESSIONID_TTL $pfile; then
      #  ox_set_property AJP_JSESSIONID_TTL 86400000 $pfile
      # fi

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/participant.properties
      if ! ox_exists_property com.openexchange.participant.ShowWithoutEmail $pfile; then
	  if ox_exists_property ShowWithoutEmail $pfile; then
	      local oldval=$(ox_read_property ShowWithoutEmail $pfile)
	      ox_set_property com.openexchange.participant.ShowWithoutEmail $oldval $pfile
	      ox_remove_property ShowWithoutEmail $pfile
	  else
	      ox_set_property com.openexchange.participant.ShowWithoutEmail true $pfile
	  fi
      fi
      if ! ox_exists_property com.openexchange.participant.autoSearch $pfile; then
	  ox_set_property com.openexchange.participant.autoSearch true $pfile
      fi
      if ! ox_exists_property com.openexchange.participant.MaximumNumberParticipants $pfile; then
	  ox_set_property com.openexchange.participant.MaximumNumberParticipants 0 $pfile
      fi

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/system.properties
      for prop in InitWorker Participant SPELLCHECKCFG Contact; do
	  if ox_exists_property $prop $pfile; then
	      ox_remove_property $prop $pfile
	  fi
      done

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/server.properties
      if ! ox_exists_property com.openexchange.MinimumSearchCharacters $pfile; then
	  ox_set_property com.openexchange.MinimumSearchCharacters 0 $pfile
      fi

      # -----------------------------------------------------------------------
      local pfile=/opt/open-xchange/etc/groupware/contact.properties
      if ! ox_exists_property com.openexchange.contact.mailAddressAutoSearch $pfile; then
	  ox_set_property com.openexchange.contact.mailAddressAutoSearch true $pfile
      fi
  fi

  # run checkconfigconsistency once
  /opt/open-xchange/sbin/checkconfigconsistency

}

setConfigFilePermissions() {
    ox_update_permissions "/opt/open-xchange/etc/groupware/mail.properties" root:open-xchange 640
    ox_update_permissions "/opt/open-xchange/etc/groupware/configdb.properties" root:open-xchange 640
    ox_update_permissions "/opt/open-xchange/etc/groupware/server.properties" root:open-xchange 640
    ox_update_permissions "/var/spool/open-xchange/uploads" open-xchange:root 750
}

case "$1" in
    configure)
	setConfigFilePermissions
        test -n "$2" && {
                # we are in update mode, run postFix to apply fixes
                postFix "$2"
                exit 0
        }

    ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

chmod 750 /var/log/open-xchange
chown open-xchange: /var/log/open-xchange
chown open-xchange: /opt/open-xchange/etc/groupware/osgi


update-rc.d open-xchange-groupware defaults > /dev/null

# if isrunning; then
#     invoke-rc.d open-xchange-groupware restart
# else
#     invoke-rc.d open-xchange-groupware start
# fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


