#!/bin/bash
# postinst script for open-xchange-imageconverter-server
#
# 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>
#        * <postinst> `abort-remove'
#        * <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/lib/oxfunctions.sh

initialConfigure() {
    # prevent bash from expanding, see bug 13316
    GLOBIGNORE='*'

    # SoftwareChange_Request-3773
    dropin_dir=/etc/systemd/system/open-xchange-imageconverter-server.service.d
    dropin_example=limits.conf
    examples_dir=/usr/share/doc/open-xchange-imageconverter-server/examples
    if [ ! -f ${dropin_dir}/${dropin_example} ]
    then
      install -D -m 644 ${examples_dir}/${dropin_example} ${dropin_dir}/${dropin_example}
    fi

    # Trigger a service definition/config reload
    systemctl daemon-reload &> /dev/null || :
}

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

    # prevent bash from expanding, see bug 13316
    GLOBIGNORE='*'

    # SoftwareChange_Request-3773
    dropin_dir=/etc/systemd/system/open-xchange-imageconverter-server.service.d
    dropin_example=limits.conf
    examples_dir=/usr/share/doc/open-xchange-imageconverter-server/examples
    if [ ! -f ${dropin_dir}/${dropin_example} ]
    then
      install -D -m 644 ${examples_dir}/${dropin_example} ${dropin_dir}/${dropin_example}
    fi

    drop_in=${dropin_dir}/${dropin_example}
    if [ -f ${drop_in} ] && grep -q "^#LimitNOFILE=16384$" ${drop_in}
    then
      sed -i 's/^#LimitNOFILE=16384$/#LimitNOFILE=65536/' ${drop_in}
    fi

    drop_in=${dropin_dir}/${dropin_example}
    if [ -f ${drop_in} ] && ! grep -q LimitNPROC ${drop_in}
    then
      sed -i '/^\[Service\]$/a #LimitNPROC=65536' ${drop_in}
    fi

    # Trigger a service definition/config reload
    systemctl daemon-reload &> /dev/null || :

    # SCR-438 & SCR-564
    SCR=SCR-438
    ox_scr_todo ${SCR} && {
      pfile=/opt/open-xchange/imageconverter/etc/imageconverter.properties
      ox_add_property com.openexchange.imageconverter.convertTimeoutMillis 20000 ${pfile}
      ox_scr_done ${SCR}
    }

    SCR=SCR-439
    ox_scr_todo ${SCR} && {
      pfile=/opt/open-xchange/imageconverter/etc/imageconverter.properties
      ox_add_property com.openexchange.imageconverter.errorPath "" ${pfile}
      ox_scr_done ${SCR}
    }

    SCR=SCR-458
    ox_scr_todo ${SCR} && {
      pfile=/opt/open-xchange/imageconverter/etc/imageconverter.properties
      pkey=com.openexchange.imageconverter.targetFormats
      new_val_part=auto:1920x1080~cover
      curr_formats=$(ox_read_property ${pkey} ${pfile})
      if [[ ! ${curr_formats} =~ ${new_val_part} ]]
      then
        new_val="${curr_formats}, ${new_val_part}"
        ox_set_property ${pkey} "${new_val}" ${pfile}
      fi
      ox_scr_done ${SCR}
    }

    SCR=SCR-476
    ox_scr_todo ${SCR} && {
      pfile=/opt/open-xchange/imageconverter/etc/overwrite.properties
      pkey=com.openexchange.hazelcast.enabled
      new_val=false
      ox_add_property ${pkey} "${new_val}" ${pfile}
      ox_scr_done ${SCR}
    }

    if ox_scr_todo SCR-486
    then
      pfile=/opt/open-xchange/imageconverter/etc/fileitem.properties
      keys=( com.openexchange.fileitem.readProperty.15 com.openexchange.fileitem.readProperty.16 com.openexchange.fileitem.readProperty.17 com.openexchange.fileitem.readProperty.18 com.openexchange.fileitem.readProperty.19 com.openexchange.fileitem.readProperty.20 com.openexchange.fileitem.readProperty.21 com.openexchange.fileitem.readProperty.22 com.openexchange.fileitem.readProperty.23 com.openexchange.fileitem.writeProperty.15 com.openexchange.fileitem.writeProperty.16 com.openexchange.fileitem.writeProperty.17 com.openexchange.fileitem.writeProperty.18 com.openexchange.fileitem.writeProperty.19 com.openexchange.fileitem.writeProperty.20 com.openexchange.fileitem.writeProperty.21 com.openexchange.fileitem.writeProperty.22 com.openexchange.fileitem.writeProperty.23 )
      values=( "requireSSL=false" "verifyServerCertificate=false" "enabledTLSProtocols=false" "clientCertificateKeyStoreUrl=" "clientCertificateKeyStorePassword=" "clientCertificateKeyStoreType=" "trustCertificateKeyStoreUrl=" "trustCertificateKeyStorePassword=" "trustCertificateKeyStoreType=" "requireSSL=false" "verifyServerCertificate=false" "enabledTLSProtocols=false" "clientCertificateKeyStoreUrl=" "clientCertificateKeyStorePassword=" "clientCertificateKeyStoreType=" "trustCertificateKeyStoreUrl=" "trustCertificateKeyStorePassword=" "trustCertificateKeyStoreType=" )
      for I in $(seq 0 $(expr ${#keys[@]} - 1))
      do
        ox_add_property ${keys[$I]} "${values[$I]}" ${pfile}
      done
      ox_scr_done SCR-486
    fi

    if ox_scr_todo SCR-564
    then
      pfile=/opt/open-xchange/imageconverter/etc/imageconverter.properties
      pkey=com.openexchange.imageconverter.convertTimeoutMillis
      value=$(ox_read_property ${pkey} ${pfile})
      if [ "15000" = "${value}" ]
      then
        ox_set_property ${pkey} 20000 ${pfile}
      fi
      ox_scr_done SCR-564
    fi
}

setConfigFilePermissions() {
    ox_update_permissions "/opt/open-xchange/imageconverter/osgi" open-xchange:root 750
    ox_update_permissions "/opt/open-xchange/imageconverter/sbin/imageconverter-admin" open-xchange:root 755
    ox_update_permissions "/opt/open-xchange/imageconverter/lib/com.openexchange.imageconverter.clt.jar" open-xchange:root 755
    ox_update_permissions "/opt/open-xchange/imageconverter/sbin/initfileitemdb" open-xchange:root 755
    ox_update_permissions "/var/log/open-xchange/imageconverter" open-xchange:root 750
    ox_update_permissions "/var/spool/open-xchange/imageconverter" open-xchange:root 750
    ox_update_permissions "/var/spool/open-xchange/fileitem" open-xchange:root 750
}

case "$1" in
    configure)
        if [ -n "$2" ]
        then
            # we are in update mode, run postFix to apply fixes
            postFix "$2"
        else
            # this is the initial install
            initialConfigure
        fi

        setConfigFilePermissions
    ;;

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

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


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

#DEBHELPER#

exit 0
