#!/bin/bash

# Wrapper for LinearRecon.java, which performs a linear reconstruction
# in each voxel.
# 
# $Id: linrecon,v 1.1 2008/12/08 17:48:43 bennett Exp $


# Find the directory containing this script.
SCRIPTDIR=${0%/*}

if [ $# -lt 3 ]; then
    echo "linrecon <data file> <scheme file> <matrix file> [-log -normalize]"
    exit
fi

DATAFILE=$1
SCHEMEFILE=$2
MATRIXFILE=$3
shift; shift; shift

DATAOPT="-inputfile $DATAFILE"
if [ $DATAFILE == - ]; then
    DATAOPT=
fi

UNAME=`uname`
CYGWIN=`expr ${UNAME} : "CYGWIN"` 

if [ $CYGWIN == '0' ]; then
    export CLASSPATH=${SCRIPTDIR}/..
else 
    export CLASSPATH=`cygpath -w ${SCRIPTDIR}/..`
fi


# Default maximum heap size
MAXHEAPSIZE=120
# If we don't have enough RAM, decrease the heap size
`java -Xmx${MAXHEAPSIZE}M -version > /dev/null 2> /dev/null`

EXITCODE=$?

if [ $EXITCODE == '127' ]; then
    echo "java executable not found. Unable to run command"
    exit 127
fi

while [ $EXITCODE != 0 ] ; do
    MAXHEAPSIZE=$(($MAXHEAPSIZE / 2))
    `java -Xmx${MAXHEAPSIZE}M -version > /dev/null 2> /dev/null`
    EXITCODE=$?
done

exec java -Xmx${MAXHEAPSIZE}M -Djava.util.logging.config.file=${SCRIPTDIR}/../logging.properties apps/LinearRecon $DATAOPT -schemefile $SCHEMEFILE -matrix $MATRIXFILE $*



