#!/bin/sh

show_help()
{
    echo "Options:"
    echo "  --help            Show this message."
    echo "  --prefix=PREFIX   Use PREFIX as QMATH_HOME. (default $PREFIX)"
}

PREFIX=$PWD
CONTEXTS_PATH="contexts/"
if [ "$1" != "" ]; then
    case "$1" in
	--help)
	    show_help
	    exit 0
	    ;;
	--prefix*)
	    PREFIX=${1:9}
	    CONTEXTS_PATH="share/qmath/contexts/"
	    ;;
	*)
	    echo "Unknown option: $1"
	    show_help
	    exit 1
    esac
fi

DEFAULT_QMATH_HOME="$PWD"

if [ "$CXX" = "" ]; then
  export CXX=g++
fi

CONFIG_H=src/config.h

GCC_VERSION=`unset LANG LC_ALL LC_MESSAGES; $CXX -v 2>&1 | grep 'gcc version' | sed 's/.*gcc version \([0-9.]*\).*/\1/'`
GCC_VERSION_MAJOR=`echo $GCC_VERSION | sed 's/\([0-9]*\).*/\1/'`
GCC_VERSION_MINOR=`echo $GCC_VERSION | sed 's/[0-9]*\.\([0-9]*\).*/\1/'`

echo "Version of GCC found: $GCC_VERSION"

if [ "2.96" = "$GCC_VERSION_MAJOR.$GCC_VERSION_MINOR" ]; then
  echo "WARNING: GCC 2.96 is an unsupported version. It wasn't an official release."
fi

cat <<EOF >$CONFIG_H
///////////////////////////////////////////////////////////////////////////
//
//   Copyright  2003, 2005 Alberto Gonzlez Palomo
//   Author: Alberto Gonzlez Palomo
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
/////////////////////////////////////////////////////////////////////////////


#ifndef _H_GLOBAL_CONFIG_FILE
#define _H_GLOBAL_CONFIG_FILE

// Define USING_GCC_2_95 if you are using a version of GCC prior to 3.0,
// or some other compiler with the same issues detailed in "README.GCC_2.9x".
EOF

# GCC version:
if [ "$GCC_VERSION_MAJOR" -le 2 ]; then
    echo >>$CONFIG_H '#define USING_GCC_2_95'
    echo "Configured for GCC 2.95 or older."
else
    echo >>$CONFIG_H '//#define USING_GCC_2_95'
    echo "Configured for GCC 3.0 or newer."
fi


FLEX_VERSION=`unset LANG LC_ALL LC_MESSAGES; flex -V 2>&1 | grep 'flex \(version \)\?' | sed 's/.*flex \(version \)\?\([0-9.]*\).*/\2/'`
FLEX_VERSION_MAJOR=`echo $FLEX_VERSION | sed 's/\([0-9]*\).*/\1/'`
FLEX_VERSION_MINOR=`echo $FLEX_VERSION | sed 's/[0-9]*\.\([0-9]*\).*/\1/'`
if ! which flex >/dev/null; then
   echo "Error: GNU flex lexer generator not installed. The package name is usually 'flex'."
   exit 1
fi
if [ "$FLEX_VERSION" = "" ]; then
   echo "Error: unknown version of flex found: "`flex -V`
   #exit 1
fi
echo "Version of flex found: $FLEX_VERSION"

BISON_VERSION=`unset LANG LC_ALL LC_MESSAGES; bison -V 2>&1 | grep 'bison .GNU Bison. ' | sed 's/.*bison .GNU Bison. \([0-9.]*\).*/\1/'`
BISON_VERSION_MAJOR=`echo $BISON_VERSION | sed 's/\([0-9]*\).*/\1/'`
BISON_VERSION_MINOR=`echo $BISON_VERSION | sed 's/[0-9]*\.\([0-9]*\).*/\1/'`
if ! which bison >/dev/null; then
   echo "Error: GNU bison parser generator not installed. The package name is usually 'bison'."
   exit 1
fi
if [ "$BISON_VERSION" = "" ]; then
   if (echo `bison -V` | grep 'bison[+][+]'); then
      echo "Error: bison++ is installed, but QMath needs plain bison"
   else
      echo "Error: unknown version of bison found: "`bison -V`
   fi
   exit 1
fi
echo "Version of bison found: $BISON_VERSION"

# Installation path:
echo >>$CONFIG_H '#define DEFAULT_QMATH_HOME "'$PREFIX'"'
echo >>$CONFIG_H '#define CONTEXTS_PATH_FROM_QMATH_HOME "'$CONTEXTS_PATH'"'
echo "Configured to use the 'contexts' directory at:"
echo $PREFIX/$CONTEXTS_PATH
echo "You can change it by using the '--prefix' option for './configure', or by"
echo "setting the environment variable 'QMATH_HOME'."

cat <<EOF >>$CONFIG_H

#endif
EOF
