#!/bin/sh
### ====================================================================== ###
##                                                                          ##
##  Serviio start Script                                                    ##
##                                                                          ##
### ====================================================================== ###

PATH=$PATH:%%PREFIX%%/bin
HOME=/var/db/serviio/home
: ${SERVIIO_MEM="-Xmx512M -Xms20M"}
SERVIIO_HOME=%%JAVAJARDIR%%/serviio
SERVIIO_CLASS_PATH="$SERVIIO_HOME/serviio.jar"
# Setup the classpath
for j in $SERVIIO_HOME/*.jar
do
	SERVIIO_CLASS_PATH="$SERVIIO_CLASS_PATH:$j"
done
SERVIIO_CLASS_PATH="$SERVIIO_CLASS_PATH:%%ETCDIR%%"

# Setup Serviio specific properties
JAVA_OPTS="-Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -Dorg.restlet.engine.loggerFacadeClass=org.restlet.ext.slf4j.Slf4jLoggerFacade -Dderby.system.home=/var/db/serviio -Dserviio.home=$HOME -Dffmpeg.location=%%LOCALBASE%%/bin/ffmpeg -Ddcraw.location=%%LOCALBASE%%/bin/dcraw"
JAVA_OPTS="${JAVA_OPTS} -Djcs.auxiliary.DC.attributes.DiskPath=/var/db/serviio"
JAVA_VERSION="21+"

# A kludge to get the -D... flags to Java, rather than to Serviio itself:
for o in "$@"
do
	case $o in
	-D*)
		JAVA_OPTS="$JAVA_OPTS $o"
		;;
	esac
done

# Execute the JVM in the foreground
export HOME PATH JAVA_VERSION
exec java ${SERVIIO_MEM} -XX:+UseG1GC -XX:GCTimeRatio=1 -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 $JAVA_OPTS -classpath "$SERVIIO_CLASS_PATH" org.serviio.MediaServer "$@" 2>&1 >/dev/null
