#!/bin/bash
#
#   @copyright Copyright (c) Open-Xchange GmbH, Germany <info@open-xchange.com>
#   @license AGPL-3.0
#
#   This code is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with OX App Suite. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
#
#   Any use of the work other than as authorized under this license or copyright law is prohibited.
#

OXSERVICENAME=imageconverter
VMMEMMAX=1024M

export MAGICK_CONFIGURE_PATH=/opt/open-xchange/${OXSERVICENAME}/etc/magick

if [ "$(id -un)" != "open-xchange" ]; then
    echo "The Open-Xchange ${OXSERVICENAME} start script must only be run as user open-xchange. Executing this script as root breaks the installation."
    exit 1
fi

OXFUNCTIONS=/opt/open-xchange/lib/oxfunctions.sh
OXPREFIX=/opt/open-xchange/${OXSERVICENAME}
OXCONFIG=${OXPREFIX}/etc/process-conf.sh
CONSOLELOG_FILE=/var/log/open-xchange/imageconverter/console.log
OSGI_JAR=/opt/open-xchange/bundles/org.eclipse.osgi_3.16.300.v20210525-1715.jar

test -f $OXFUNCTIONS || {
    echo "missing common shell functions file"
    exit 1
}

. $OXFUNCTIONS

test -f $OXCONFIG && . $OXCONFIG

ox_set_JAVA_BIN

ox_update_config_init /opt/open-xchange/imageconverter/osgi/config.ini /opt/open-xchange/imageconverter/osgi/config.ini.template /opt/open-xchange/imageconverter/osgi/bundle.d

test -z "$JAVA_XTRAOPTS" && JAVA_XTRAOPTS="-Xmx${VMMEMMAX}"

# Remove MaxPermSize to avoid warning with Java 8.
# TODO remove this code and MaxPermSize option from JAVA_XTRAOPTS once Java 8 is minimum requirement.
JAVA_VERSION=$($JAVA_BIN -version 2>&1 | grep 'version' | awk '{gsub(/\"/,""); print $3}' | awk '{ print substr($1, 1, 5); }')
JAVA_MAJOR=$(echo $JAVA_VERSION | awk '{ print substr($1, 1, 1); }')
JAVA_MINOR=$(echo $JAVA_VERSION | awk '{ print substr($1, 3, 1); }')
JAVA_PATCH=$(echo $JAVA_VERSION | awk '{ print substr($1, 5, 1); }')
if [ 1 -eq "$JAVA_MAJOR" -a 7 -lt "$JAVA_MINOR" ]; then
    JAVA_XTRAOPTS=$(echo "$JAVA_XTRAOPTS" | sed -e 's/-XX:MaxPermSize \?= \?[^ ]\+//')
fi
if [ 1 -eq "$JAVA_MAJOR" -a 8 -lt "$JAVA_MINOR" ]; then
    JAVA_XTRAOPTS=$(echo "$JAVA_XTRAOPTS" | sed -e 's/-XX:+UseParNewGC//')
fi

JAVA_OPTS="${JAVA_XTRAOPTS} \
-Djava.awt.headless=true \
-Dopenexchange.propdir=/opt/open-xchange/etc \
-Dopenexchange.propdir2=/opt/open-xchange/${OXSERVICENAME}/etc"

CLASSPATH=""

if [ -n "$CLASSPATH" ]; then
    CLASSPATH="-classpath $CLASSPATH"
fi

ox_save_backup $CONSOLELOG_FILE

umask 066

if [ "${CONSOLELOG}" = "/dev/tty" ];  then
    exec ${JAVA_BIN} ${JAVA_OPTS} ${CLASSPATH} -jar ${OSGI_JAR} -configuration file:/opt/open-xchange/imageconverter/osgi 2>&1
else
    if [ -n "${CONSOLELOG}" ]; then
        CONSOLELOG_FILE="${CONSOLELOG}"
    fi

    exec ${JAVA_BIN} ${JAVA_OPTS} ${CLASSPATH} -jar ${OSGI_JAR} -configuration file:/opt/open-xchange/imageconverter/osgi >> ${CONSOLELOG_FILE} 2>&1
fi

