#!/bin/sh

CXX=${CXX:-g++}

if [ "$1" = "clean" ]
then
	( cd `dirname $0`; rm -rf *.cxx *.so *.o mlt.i ../.java *.class src_swig )
	exit 0
fi

path=`which java 2> /dev/null`

if [ $? = 0 ]
then
	# Locate the path for the include
	path=`dirname $path`
	path=`dirname $path`

	# Change this as needed
	# export JAVA_INCLUDE="-I$path/include -I$path/include/linux"

	ln -sf ../mlt.i

	# Invoke swig
	mkdir -p src_swig/org/mltframework
	swig -c++ -I../../mlt++ -I../.. -java -outdir src_swig/org/mltframework -package org.mltframework mlt.i || exit $?

	# Compile the wrapper
	${CXX} -fPIC -D_GNU_SOURCE ${CXXFLAGS} -c -rdynamic -pthread -I../.. mlt_wrap.cxx $JAVA_INCLUDE || exit $?
	
	# Create the module
	${CXX} ${CXXFLAGS} -shared mlt_wrap.o -L../../mlt++ -lmlt++ -o libmlt_java.so || exit $?

	# Compile the test
	javac `find src_swig -name '*.java'` || exit $?

	export CLASSPATH=`pwd`/src_swig
	javac Play.java
else
	echo "Java command not found"
	exit 1
fi
