#install basename
#----------------------------------------------------------------------
#Libraries have been installed in:
#   /home/erik/dev/cq2/zandbak/lucene-gcj-jvm-memleak-tests/lucene3-gcj46/gccinstall/lib
#
#If you ever happen to want to link against installed libraries
#in a given directory, LIBDIR, you must either use libtool, and
#specify the full pathname of the library, or use the `-LLIBDIR'
#flag during linking and do at least one of the following:
#   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
#     during execution
#   - add LIBDIR to the `LD_RUN_PATH' environment variable
#     during linking
#   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
#   - have your system administrator add LIBDIR to `/etc/ld.so.conf'
#
#See any operating system documentation about shared libraries for
#more information, such as the ld(1) and ld.so(8) manual pages.
#----------------------------------------------------------------------

PATH=gccinstall/bin:$PATH

if [ ! `which unzip` ] ; then
    echo "unzip not found: install unzip package"
    exit 1
fi

if [ ! `which gcj` ] ; then
    echo "gcj not found."
    exit 1
fi

JARFILE=lucene-core-3.1.0.jar
LUCENE_JAR_URL="http://apache.proserve.nl//lucene/java/3.1.0/lucene-3.1.0.tar.gz"

if [ ! -f $JARFILE ]; then
  echo "Downloading Lucene Jar from: $LUCENE_JAR_URL"
  wget --quiet -O- $LUCENE_JAR_URL | tar xzf - lucene-3.1.0/lucene-core-3.1.0.jar -O > $JARFILE
fi

gcj -shared -fPIC $JARFILE -o liblucene-core.so -Lgccinstall/lib -lgcj -findirect-dispatch -fno-indirect-classes
#-gi

TMPDIR=`mktemp --directory`

unzip $JARFILE -d$TMPDIR

for classFile in $(find $TMPDIR -name *.class)
do
    targetDir="include/lucene/$(dirname $classFile | sed s,$TMPDIR/,,)"
    filename=$(basename $classFile .class)
    targetFile=$targetDir/$filename.h

    mkdir --parents $targetDir

    className=$(echo $classFile| sed s,.class,, | sed s,$TMPDIR/,, | sed s,/,.,g)
    gcjh -I $TMPDIR -o $targetFile $className
    echo $className
done

rm $TMPDIR -r
