#!/bin/sh

# if CONFIGURED is set, then we don't attempt to run findjava
# (useful when building inside rJava)
if [ -z "$CONFIGURED" ]; then
    echo "Generate Windows-specific files (src/jvm-w32) ..."
    make -C src/win32

    if [ $? != 0 ]; then
	exit 1
    fi

    echo "Find Java..."
# findjava honors JAVA_HOME environment variable, so we can safely overwite it 
    if [ -e src/win32/findjava.exe ]; then
	JAVA_HOME=`src/win32/findjava -s -f`
	R_HOME=`src/win32/findjava -R -s -f`
    fi
fi

if [ x$JAVA_HOME = x ]; then
    echo "ERROR: cannot find Java Development Kit." >&2
    echo "       Please set JAVA_HOME to specify its location manually" >&2
    exit 1
fi

if [ x$R_HOME = x ]; then
    echo "ERROR: cannot find R. Please set R_HOME correspondingly." >&2
    exit 1
fi

echo "  JAVA_HOME=$JAVA_HOME"
echo "  R_HOME=$R_HOME"

echo "JAVAHOME:=$JAVA_HOME" > src/Makefile.wconf
echo "RHOME:=$R_HOME" >> src/Makefile.wconf

echo "Creating Makefiles ..."
cp Makefile.win Makefile
cp src/Makefile.win src/Makefile

echo "Configuration done."

