#! /bin/bash
#
# Copyright (c) 2003, 2004 The University of Wroclaw.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#    1. Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#    3. The name of the University may not be used to endorse or promote
#       products derived from this software without specific prior
#       written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE UNIVERSITY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


############################################################
# VARIABLES
############################################################

result=

config_log="configure.log"

javavm=0
antlr=0

############################################################
# FUNCTIONS
############################################################

abort () {

    echo
    echo
    echo "*** $@ ***"
    if test -f $config_log; then
	echo "*** Check $config_log for details. ***"
    fi
    echo
    if [ "$ignore_errors" = yes ] ; then
      echo "*** Ignoring error. ***"
    else
      echo "Aborting..."
      rm -f $config_mak
      exit 1
    fi
}

echo_check_for () {

    echo "--------------- Checking for $@ ---------------" >> $config_log
    echo -n "Checking for $@... "
}


echo_check_if () {

    echo "--------------- Checking if $@ ----------------" >> $config_log
    echo -n "Checking if $@... "
}


echo_result () {

    echo "Result: $@" >> $config_log
    echo "-----------------------------------------------" >> $config_log
    echo >> $config_log
    echo "$@"
}

############################################################
# INIT
############################################################

rm -f $config_log config.cs2n.mak

############################################################
# TESTS
############################################################


# STEP 1
# Check for Java >= 1.4
echo_check_for "Java >= 1.4"
version='java -version'
if test "$?" = 0; then
    ver2=`$version 2>&1| head -1 | sed 's/.* \"\([0-9]*\.[0-9]\.[0-9]*\).*\"/\1/g'`
    case $ver2 in  
	1.4.*) 
	    javavm=1
	    echo_result "yes" 
	    ;; 	 
	1.5.*) 
	    javavm=1
	    echo_result "yes"
	    ;; 	 
	*) 
	    echo_result "no" 	    
	    ;; 
    esac 
fi 

# STEP 2
# Check for Antlr >= 2.7
if test "$javavm" = 1  ; then
    echo_check_for "antlr >= 2.7"
    classpath="$ANTLR_HOME/antlr.jar"
    ver=`antlr 2>&1| head -1 | sed 's/.* \([0-9]*\.[0-9]\.[0-9]*\).*/\1/g'`
    case $ver in  
	2.7*) 
	    antlr=1
	    echo_result "yes" 
	    ;; 	
	2.8*) 
	    antlr=1
	    echo_result "yes" 
		;; 
	*) 
	    echo_result "no" 
	    abort "antlr > = 2.7 not installed "	    
	    ;; 
    esac 
fi

echo 
echo "Type 'make' to compile cs2n"
# STEP 4
# Check if antlr.runtime.dll exists
# echo_check_if "antlr.runtime.dll exists in $ANTLR_HOME/lib/csharp/src"
# ex=`ls $ANTLR_HOME/lib/csharp/src/antlr.runtime.dll 2> /dev/null`
# if test "$?" = 0 ; then
#    echo_result "yes"
# else
#    echo "*** antlr.runtime.dll should be in $ANTLR_HOME/lib/csharp/src ***"
#    abort "antlr.runtime.dll missing"
#    echo_result "no"
# fi

# STEP 5
# Creating config.cs2n.mak

# echo "Creating config.cs2n.mak"

# cat > config.cs2n.mak <<EOF
# # Generated by configure

# antlr = $antlr

# EOF
