#!/bin/bash
set -e

DICTIONARIES="/Users/fschmidt/hg/editor/build/luan_editor/dictionaries/"

for i in "/Users/fschmidt/hg/editor/build/luan_editor"/jars/* ; do CLASSPATH="$CLASSPATH:$i" ; done
CLASSPATH="$(echo -n "$CLASSPATH" | sed 's/^://')"

mkdir -p ~/.luan_editor

set +m

if [[ "$(uname)" == "Darwin" ]]; then  # Mac crap
	function runJava() {
		java -Ddictionaries="$DICTIONARIES" -Xdock:name="Luan Editor" -Duser.home="$HOME" -classpath "$CLASSPATH" luan.Luan classpath:luan_editor/editor.luan "$@" 2>&1 | grep --line-buffered -v 'NSRemoteView\|NSSavePanel' | tee ~/.luan_editor/error.log &
	}
	if [[ $# == 0 ]]; then
		runJava
	else
		for file in "$@"; do
			runJava "$file"
			sleep 0.1
		done
	fi
else
	java -Duser.home="$HOME" -Ddictionaries="$DICTIONARIES" -classpath "$CLASSPATH" luan.Luan classpath:luan_editor/editor.luan "$@" 2>&1 | tee ~/.luan_editor/error.log &
fi
