#!/bin/bash
#
# This script configures the start information for this server.
#
# The following variables may be used to override the defaults.
# For one-time overrides the variable can be set as part of the command-line; e.g.,
#
#     % CQ_PORT=1234 ./start
#

# TCP port used for stop and status scripts
if [ -z "$CQ_PORT" ]; then
	CQ_PORT=4502
fi

# hostname of the interface that this server should listen to
#if [ -z "$CQ_HOST" ]; then
#	CQ_HOST=
#fi

# runmode(s)
# will not be used if repository is already present
if [ -z "$CQ_RUNMODE" ]; then
	CQ_RUNMODE='author'
fi

# name of the jarfile
#if [ -z "$CQ_JARFILE" ]; then
#	CQ_JARFILE=''
#fi

# default JVM options
if [ -z "$CQ_JVM_OPTS" ]; then
	CQ_JVM_OPTS='-server -Xmx1024m -XX:MaxPermSize=256M -Djava.awt.headless=true'
	# add the required JPMS modules, if needed
	java --add-modules java.se.ee -version >/dev/null 2>&1
	if [ $? -eq 0 ]; then
	       CQ_JVM_OPTS="${CQ_JVM_OPTS} --add-modules java.se.ee"
	fi
fi

# file size limit (ulimit)
if [ -z "$CQ_FILE_SIZE_LIMIT" ]; then
	CQ_FILE_SIZE_LIMIT=8192
fi

# ------------------------------------------------------------------------------
# authentication
# ------------------------------------------------------------------------------
# when using oak (crx3) authentication must be configured using the
# Apache Felix JAAS Configuration Factory service via the Web Console
# see http://jackrabbit.apache.org/oak/docs/security/authentication/externalloginmodule.html

# use jaas.config (legacy: only used for crx2 persistence)
#if [ -z "$CQ_USE_JAAS" ]; then
#	CQ_USE_JAAS='true'
#fi

# config for jaas (legacy: only used for crx2 persistence)
#if [ -z "$CQ_JAAS_CONFIG" ]; then
#	CQ_JAAS_CONFIG='etc/jaas.config'
#fi

# ------------------------------------------------------------------------------
# persistence mode
# ------------------------------------------------------------------------------
# the persistence mode can not be switched for an existing repository
CQ_RUNMODE="${CQ_RUNMODE},crx3,crx3tar"
#CQ_RUNMODE="${CQ_RUNMODE},crx3,crx3mongo"

# settings for mongo db
#if [ -z "$CQ_MONGO_HOST" ]; then
#	CQ_MONGO_HOST=127.0.0.1
#fi
#if [ -z "$CQ_MONGO_PORT" ]; then
#	CQ_MONGO_PORT=27017
#fi
#if [ -z "$CQ_MONGO_DB" ]; then
#	CQ_MONGO_DB=aem6
#fi

# ------------------------------------------------------------------------------
# CLDR
# ------------------------------------------------------------------------------
# https://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html#cldr
CQ_USE_CLDR='true'

# ------------------------------------------------------------------------------
# do not configure below this point
# ------------------------------------------------------------------------------

if [ $CQ_FILE_SIZE_LIMIT ]; then
	CURRENT_ULIMIT=`ulimit`
	if [ $CURRENT_ULIMIT != "unlimited" ]; then
		if [ $CURRENT_ULIMIT -lt $CQ_FILE_SIZE_LIMIT ]; then
			echo "ulimit ${CURRENT_ULIMIT} is too small (must be at least ${CQ_FILE_SIZE_LIMIT})"
			exit 1
		fi
	fi
fi

BIN_PATH=$(dirname $0)
cd $BIN_PATH/..
if [ -z $CQ_JARFILE ]; then
	CQ_JARFILE=`ls app/*.jar | head -1`
fi
CURR_DIR=$(basename $(pwd))
cd ..
START_OPTS="start -c ${CURR_DIR} -i launchpad"
if [ $CQ_PORT ]; then
	START_OPTS="${START_OPTS} -p ${CQ_PORT}"
fi
if [ $CQ_RUNMODE ]; then
	CQ_JVM_OPTS="${CQ_JVM_OPTS} -Dsling.run.modes=${CQ_RUNMODE}"
fi
if [ $CQ_HOST ]; then
	CQ_JVM_OPTS="${CQ_JVM_OPTS} -Dorg.apache.felix.http.host=${CQ_HOST}"
    START_OPTS="${START_OPTS} -a ${CQ_HOST}"
fi
if [ $CQ_MONGO_HOST ]; then
    START_OPTS="${START_OPTS} -Doak.mongo.host=${CQ_MONGO_HOST}"
fi
if [ $CQ_MONGO_PORT ]; then
    START_OPTS="${START_OPTS} -Doak.mongo.port=${CQ_MONGO_PORT}"
fi
if [ $CQ_MONGO_DB ]; then
    START_OPTS="${START_OPTS} -Doak.mongo.db=${CQ_MONGO_DB}"
fi
if [ $CQ_USE_JAAS ]; then
    CQ_JVM_OPTS="${CQ_JVM_OPTS} -Djava.security.auth.login.config=${CQ_JAAS_CONFIG}"
fi
if [ $CQ_USE_CLDR ]; then
    CQ_JVM_OPTS="${CQ_JVM_OPTS} -Djava.locale.providers=CLDR,JRE,SPI"
fi
START_OPTS="${START_OPTS} -Dsling.properties=conf/sling.properties"

(
  (
    java $CQ_JVM_OPTS -jar $CURR_DIR/$CQ_JARFILE $START_OPTS &
    echo $! > $CURR_DIR/conf/cq.pid
  ) >> $CURR_DIR/logs/stdout.log 2>&1
) &
