#!/bin/sh

# Do not consider environment variable
unset SUNFLOW_JAVA
unset SUNFLOW_OPTIONS

# System-wide configuration
if [ -f /etc/sunflowrc ]; then
	. /etc/sunflowrc
fi

# Per-user configuration
if [ -f ~/.sunflowrc ]; then
	. ~/.sunflowrc
fi

# Default: use the alternatives system
if [ -z "$SUNFLOW_JAVA" ]; then
	SUNFLOW_JAVA=java
fi

# Sort java- and sunflow-related options since their relative position
# (with respect to the -jar option) is important. Also check whether
# some arguments should be passed to java rather that to sunflow.
SUNFLOW_OPTIONS="$SUNFLOW_OPTIONS $*"
java_opt=
prog_opt=
for item in $SUNFLOW_OPTIONS; do
	case $item in
		-server | -Xmx*)
			java_opt="$java_opt $item"
			;;
		*)
			prog_opt="$prog_opt $item"
			;;
	esac
done

# Be verbose a bit
command="$SUNFLOW_JAVA $java_opt -jar /usr/share/java/sunflow.jar $prog_opt"
echo "Running: $command"
$command
