#!/bin/bash
#
#
#
# @copyright Copyright (c) OX Software 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.
#
#

if [ "$(id -un)" != "open-xchange" ]; then
    echo "The Open-Xchange backend 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/mobile-api-facade
OXCONFIG=${OXPREFIX}/etc/process-conf.sh
CONSOLELOG=/var/log/open-xchange/mobile-api-facade/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

# Get existing variables that match our pattern
get_jopts() {
  echo $(compgen -A variable | grep JAVA_OPTS_)
}

# unset JAVA_XTRAOPTS and JAVA_OPTS_* to start with a clean environment
for x in JAVA_XTRAOPTS $(get_jopts)
do
  unset $x
done

test -f $OXCONFIG && . $OXCONFIG

# Use old JAVA_XTRAOPTS if declared otherwise build it from JAVA_OPTS_ fragments
GATHERED_OPTS=$(eval echo "$(sed -e 's/\(\<\)/$\1/g' <<<$(get_jopts))")
JAVA_XTRAOPTS=${JAVA_XTRAOPTS:=${GATHERED_OPTS}}

ox_set_JAVA_BIN

ox_update_config_init \
  /opt/open-xchange/mobile-api-facade/osgi/config.ini \
  /opt/open-xchange/mobile-api-facade/osgi/config.ini.template \
  /opt/open-xchange/mobile-api-facade/osgi/bundle.d \
  ${OSGI_JAR}

test -z "$JAVA_XTRAOPTS" && JAVA_XTRAOPTS="-Xmx512M"

# 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} \
-Dopenexchange.propdir=/opt/open-xchange/etc \
-Dopenexchange.propdir2=/opt/open-xchange/mobile-api-facade/etc"

LOGDIR=$(dirname $CONSOLELOG)

if [ ! -d $LOGDIR ]; then
    mkdir -p $LOGDIR
    chmod 0750 $LOGDIR
fi

ox_save_backup $CONSOLELOG

umask 066

# On distros without systemd we might get called via su which might have
# applied limits from /etc/security/limits.* depending on the distro. Assure
# that user configured limits are used
read the_init < /proc/1/comm
if [[ -n "${the_init}" && "${the_init}" != systemd ]]
then
  NRFILES=${NRFILES:-65536}
  NPROC=${NPROC:-65536}
  ulimit -n ${NRFILES} 2>> ${CONSOLELOG}
  ulimit -u ${NPROC} 2>> ${CONSOLELOG}
fi

# Bug 51074: wrong locale will break encoding of started processes
charmap=$(locale charmap)
if ! [[ "${charmap}" == "UTF-8" ]]
then
  echo "WARNING: Couldn't detect proper unicode charmap in locale category LC_CTYPE." >> ${CONSOLELOG}
  echo "WARNING: Current charmap: \"${charmap}\" might cause encoding problems."  >> ${CONSOLELOG}
fi

exec $JAVA_BIN $JAVA_OPTS $CLASSPATH -jar ${OSGI_JAR} -configuration file:/opt/open-xchange/mobile-api-facade/osgi >> $CONSOLELOG 2>&1
