#! /bin/sh -
#$Id: configure,v 1.15.2.1 2009/08/11 15:48:46 gcosmo Exp $
# ======================================================================
#  A configure script for Geant4Py
# ======================================================================
export LANG=C

IFS='
 	'
PATH=/bin:/usr/bin
export PATH

# ======================================================================
# testing the echo features
# ======================================================================
if `(echo "testing\c"; echo 1,2,3) | grep c > /dev/null` ; then
  if `(echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn > /dev/null`; then
    ac_n= ac_c='
' ac_t='        '
  else
    ac_n=-n ac_c= ac_t=
  fi
else
  ac_n= ac_c='\c' ac_t=
fi

# ======================================================================
# help message
# ======================================================================
show_help() {
cat <<EOF
\`configure' configures Geant4Py to adapt to many kinds of systems.

Usage:  ./configure SYSTEM [OPTION]... [VAR=VALUE]...

SYSTEM: System type (see Supported Arhitectures)

Options:
  -h, --help                Display this help and exit

Installation directories:
  --prefix=PREFIX           Installation prefix  [./]
  --libdir=DIR              Python modules dir [PREFIX/lib]

Fine tuning of the library path:
  --with-g4-incdir=DIR      Geant4 header dir [\$G4INSTALL/include]
  --with-g4-libdir=DIR      Geant4 library dir [\$G4INSTALL/lib/\$G4SYSTEM]
  --with-clhep-incdir=DIR   CLHEP header dir [\$CLHEP_BASE_DIR/include]
  --with-clhep-libdir=DIR   CLHEP library dir [\$CLHEP_BASE_DIR/lib]
  --with-clhep-lib=LIB      library name of libCLHEP.so [CLHEP|\$CLHEP_LIB]

  --with-python-incdir=DIR  Python header dir [/usr/include/python(2.#)],
                            (location of pyconfig.h)
  --with-python-libdir=DIR  Python library dir [/usr/lib(64)]

  --with-boost-incdir=DIR   BOOST-C++ header dir [/usr/include],
                            (location of boost/)
  --with-boost-libdir=DIR   BOOST-C++ library dir [/usr/lib]
  --with-boost-python-lib=LIB library name of libboost_python.so [boost_python]

  --with-package-dir=DIR    Geant4 Package dir

  --with-extra-dir=DIR      Install path for extra packages [/usr/local]

  --with-xercesc-incdir=DIR Xerces-C header dir [/usr/include]
  --with-xercesc-libdir=DIR Xerces-C library dir [/usr/lib(64)]

Enable/disable options: prefix with either --enable- or --disable-
  openglx      OpenGLX support    [auto]
  openglxm     OpenGLXm support   [disable, \$G4VIS_USE_OPENGLXM]
  raytracerx   RayTracerX support [disable, \$G4VIS_USE_RAYTRACERX]

Supported Architectures:
  linux           for Linux gcc 3.x and 4.x (32bit)
  linux64         for Linux gcc 3.x and 4.x (64bit, alias to linuxx8664gcc)
  linuxx8664gcc   for AMD Opteron and Intel EM64T Linux gcc 3.x and 4.x
  macosx          for MacOSX with gcc (Tiger/Leopard and Xcode)

EOF
}

# ======================================================================
# main
# ======================================================================
# input
arguments="$@"

# default values
prefix=`pwd`
current_dir=`pwd`

g4_incdir=/zzz
g4_libdir=/zzz

clhep_incdir=/zzz
clhep_libdir=/zzz
clhep_lib=${CLHEP_LIB:-CLHEP}

python_incdir=/zzz
boost_incdir=/usr/include
boost_python_lib=boost_python

enable_openglx=1
enable_openglxm=${G4VIS_USE_OPENGLXM:+1}
enable_openglxm=${enable_openglxm:-0}
enable_raytracerx=${G4VIS_USE_RAYTRACERX:+1}
enable_raytracerx=${enable_raytracerx:-0}

enable_physicslist=0

enable_gdml=0

xercesc_incdir=/usr/include

extra_dir=/usr/local

# checking system
case $1 in
  linux|linux64|linuxx8664gcc|macosx)
    system=$1
    if [ $system = linux64 ]; then
      system=linuxx8664gcc
    fi
    echo $ac_n "Checking for system type ... $ac_c"
    if [ ! -e "config/sys/$system.gmk" ]; then
      echo "no"
      echo $ac_n "### Invalid system type. $ac_c"
      echo "Please taka a look in config/sys/."
      exit -1
    fi
    echo "$system"
    ;;
  --help|-h)
    show_help
    exit 0
    ;;
  *)
    echo "Error: System type is not specified or more than one system type. "
    echo ""
    echo "Usage:  ./configure SYSTEM [OPTION]... [VAR=VALUE]..."
    echo ""
    exit -1
    ;;
esac
shift

# parsing options
while test $# -gt 0
do
  case $1 in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) optarg= ;;
  esac

  case $1 in
    --help|-h) show_help;  exit 0 ;;
    # ---------------------------------------------------------------
    --prefix=*)                prefix=$optarg                    ;;
    --libdir=*)                libdir=$optarg                    ;;
    --with-package-dir=*)      g4_incdir=$optarg/include/Geant4
                               g4_libdir=$optarg/lib
                               clhep_incdir=$optarg/include
                               clhep_libdir=$optarg/lib          ;;
    --with-g4-incdir=*)        g4_incdir=$optarg                 ;;
    --with-g4-libdir=*)        g4_libdir=$optarg                 ;;
    --with-clhep-incdir=*)     clhep_incdir=$optarg              ;;
    --with-clhep-libdir=*)     clhep_libdir=$optarg              ;;
    --with-clhep-lib=*)        clhep_lib=$optarg                 ;;
    --with-extra-dir=*)        extra_dir=$optarg                 ;;
    --with-python-incdir=*)    python_incdir=$optarg             ;;
    --with-python-libdir=*)    python_libdir=$optarg             ;;
    --with-boost-incdir=*)     boost_incdir=$optarg              ;;
    --with-boost-libdir=*)     boost_libdir=$optarg              ;;
    --with-boost-python-lib=*) boost_python_lib=$optarg          ;;
    --with-xercesc-incdir=*)   xercesc_incdir=$optarg            ;;
    --with-xercesc-libdir=*)   xercesc_libdir=$optarg            ;;
    # ---------------------------------------------------------------
    --enable-openglx )         enable_openglx=1                  ;;
    --disable-openglx )        enable_openglx=0                  ;;
    --enable-openglxm )        enable_openglxm=1                 ;;
    --disable-openglxm )       enable_openglxm=0                 ;;
    --enable-raytracerx )      enable_raytracerx=1               ;;
    --disable-raytracerx )     enable_raytracerx=0               ;;
    # ---------------------------------------------------------------
    -*)
      echo "Unrecognized option: $1"
      exit -1
      ;;    
    *)
      echo "Invalid argument: $1"
      exit -1
      ;;    
  esac
  shift
done

# check variables
arch=32
if [ $system = linuxx8664gcc ]; then
  arch=64
fi

shlib=so
if [ $system = macosx ]; then
shlib=dylib
fi

extra_incdir=$extra_dir/include
if [ $arch = 64 ];then
  extra_libdir=$extra_dir/lib64
else
  extra_libdir=$extra_dir/lib
fi

echo $ac_n "Checking for prefix ... $ac_c"
echo $prefix

echo $ac_n "Checking for lib dir ... $ac_c"
libdir=${libdir:-$prefix/lib}
echo $libdir

echo $ac_n "Checking for G4 include dir ... $ac_c"
g4_incdir1=${G4INSTALL:+$G4INSTALL/include}
g4_incdir2=${G4INCLUDE:+$G4INCLUDE}
set $g4_incdir $g4_incdir1 $g4_incdir2
for adir in $*
do
  if [ -d "$adir" ]; then
    g4_incdir=$adir
    break
  fi
done

if [ ! -d "$g4_incdir" ]; then
  echo "no"
  echo $ac_n "### $g4_incdir (g4-incdir) does not exist. $ac_c"
  echo "Try --with-g4-incdir option."
  exit -1
fi
echo "$g4_incdir"

# ---
echo $ac_n "Checking for G4 lib dir ... $ac_c"
g4_libdir1=${G4INSTALL:+$G4INSTALL/lib/$G4SYSTEM}
g4_libdir2=${G4LIB:+$G4LIB/$G4SYSTEM}
set $g4_libdir/$G4SYSTEM $g4_libdir $g4_libdir1 $g4_libdir2
for adir in $*
do
  if [ -d "$adir" ]; then
    g4_libdir=$adir
    break
  fi
done

if [ ! -d "$g4_libdir" ]; then
  echo "no"
  echo $ac_n "### $g4_libdir (g4-libdir) does not exist. $ac_c"
  echo "Try --with-g4-libdir option."
  exit -1
fi
echo "$g4_libdir"

echo $ac_n "Checking for G4 libs are shared libraray ... $ac_c"
if [ ! -f ${g4_libdir}/libG4run.${shlib} ]; then
  echo "no"
  echo $ac_n "### G4 libray must be global and shared. $ac_c"
  echo "Recompile with global and shared mode."
  exit -1
fi
echo "ok"

# ---
clhep_incdir1=${CLHEP_INCLUDE_DIR:-/zzz}
clhep_incdir2=${CLHEP_BASE_DIR:+$CLHEP_BASE_DIR/include}
set $clhep_incdir $clhep_incdir1 $clhep_incdir2
for adir in $*
do
  if [ -d "$adir" ]; then
    clhep_incdir=$adir
    break
  fi
done

echo $ac_n "Checking for CLHEP include dir ... $ac_c"
if [ ! -d "$clhep_incdir" ]; then
  echo "no"
  echo $ac_n "### $clhep_incdir (clhep-incdir) does not exist. $ac_c"
  echo "Try --with-clhep-incdir option."
  exit -1
fi
echo "$clhep_incdir"

# ---
clhep_libdir1=${CLHEP_LIB_DIR:-/zzz}
clhep_libdir2=${CLHEP_BASE_DIR:+$CLHEP_BASE_DIR/lib}
set $clhep_libdir $clhep_libdir1 $clhep_libdir2
for adir in $*
do
  if [ -d "$adir" ]; then
    clhep_libdir=$adir
    break
  fi
done

echo $ac_n "Checking for CLHEP lib dir ... $ac_c"
if [ ! -d "$clhep_libdir" ]; then
  echo "no"
  echo $ac_n "### $clhep_libdir (clhep-libdir) does not exist. $ac_c"
  echo "Try --with-clhep-libdir option."
  exit -1
fi
echo "$clhep_libdir"

echo $ac_n "Checking for CLHEP lib name ... $ac_c"
if [ ! -f "$clhep_libdir"/lib${clhep_lib}.${shlib} ]; then
  echo "no"
  echo $ac_n "### lib${clhep_lib}.${shlib} (clhep-lib) does not exist. $ac_c"
  echo "Try --with-clhep-lib option."
  exit -1
fi
echo "lib${clhep_lib}.${shlib}"

# ---
echo $ac_n "Checking for Python include dir (pyconfig.h) ... $ac_c"
# check version
set python python2.6 python2.5 python2.4 python2.3 python2.2
for aincdir in $*
do
  if [ -d /usr/include/"$aincdir" ]; then
    python_incdir=/usr/include/$aincdir
    break
  fi
done

if [ ! -f "$python_incdir"/pyconfig.h ]; then
  echo "no"
  echo $ac_n "### $python_incdir (python-incdir) is invalid. $ac_c"
  echo "Try --with-python-incdir option."
  exit -1
fi
echo "$python_incdir"

echo $ac_n "Checking for Python lib dir ... $ac_c"
if [ ! $python_libdir ]; then
  if [ $arch = 64 ]; then
    python_libdir=/usr/lib64
  else
    python_libdir=/usr/lib
  fi
fi
if [ ! -d "$python_libdir" ]; then
  echo "no"
  echo $ac_n "### $python_libdir (python-libdir) does not exist. $ac_c"
  echo "Try --with-python-libdir option."
  exit -1
fi
echo "$python_libdir"

# ---
echo $ac_n "Checking for Boost include dir (boost/python.hpp) ... $ac_c"
set $boost_incdir $extra_incdir
for adir in $*
do
  if [ -f "$adir"/boost/python.hpp ]; then
    boost_incdir=$adir
    break
  fi
done
if [ ! -f "$boost_incdir"/boost/python.hpp ]; then
  echo "no"
  echo $ac_n "### $boost_incdir (boost-incdir) is invalid. $ac_c"
  echo "Try --with-boost-incdir option."
  exit -1
fi
echo "$boost_incdir"

echo $ac_n "Checking for Boost version ... $ac_c"
cat > conftest.cc <<EOF
#include <boost/version.hpp>

int main() {
  if (BOOST_VERSION >= 103200)
    return 0;
  else
    return 1;
}
EOF
g++ -I$boost_incdir -o conftest conftest.cc > /dev/null 2>&1
./conftest
q_boost_version=$?
rm conftest.cc conftest
if [ $q_boost_version = 0 ]; then
  echo "ok"
else
  echo "boost version >= 1.32.00 is required."
  exit -1
fi

echo $ac_n "Checking for Boost lib dir ... $ac_c"
if [ ! $boost_libdir ]; then
  if [ $arch = 64 ]; then
    boost_libdir=/usr/lib64
  else
    boost_libdir=/usr/lib
  fi
fi
set $boost_libdir $extra_libdir
for adir in $*
do
  if [ -f "$adir"/lib${boost_python_lib}.${shlib} ]; then
    boost_libdir=$adir
    break
  fi
done

if [ ! -d "$boost_libdir" ]; then
  echo "no"
  echo $ac_n "### $boost_libdir (boost-libdir) does not exist. $ac_c"
  echo "Try --with-boost-libdir option."
  exit -1
fi
echo "$boost_libdir"

echo $ac_n "Checking for Boost Python lib name ... $ac_c"
if [ ! -f "$boost_libdir"/lib${boost_python_lib}.${shlib} ]; then
  echo "no"
  echo $ac_n "### lib${boost_python_lib}.${shlib} (boost-python-lib) does not exist. $ac_c"
  echo "Try --with-boost-python-lib option."
  exit -1
fi
echo "lib${boost_python_lib}.${shlib}"

# ---
echo $ac_n "Checking for OpenGL support ...$ac_c"
if [ -f "$g4_libdir"/libG4OpenGL.${shlib} ]; then
  if [ $enable_openglx = 0 ]; then
    echo "disabled"
  else
    echo "yes"
  fi
else
  echo "no"
  enable_openglx=0
fi

# ---
echo $ac_n "Checking for physics list support ...$ac_c"
if [ -f "$g4_incdir"/QGSP.hh ]; then
  enable_physicslist=1
fi
if [ $enable_physicslist = 0 ]; then
  echo "no"
else
  echo "yes"
fi

# ---
echo $ac_n "Checking for GDML support ...$ac_c"
if [ -f "$g4_incdir"/G4GDMLParser.hh ]; then
  echo "yes"
  enable_gdml=1
else
  echo "no"
  enable_gdml=0
fi

# ---
if [ $enable_gdml = 1 ]; then
  echo $ac_n "Checking for Xerces-C include dir ...$ac_c"
  set $xercesc_incdir $extra_incdir
  for adir in $*
  do
    if [ -d "$adir/xercesc" ]; then
      xercesc_incdir=$adir
      break
    fi
  done
  if [ -d "$xercesc_incdir/xercesc" ]; then
    echo "yes"
  else
    echo "no"
    echo "### $xercesc_incdir/xercesc does not exist. "
    echo "Try --with-xercesc-incdir option."
    exit -1
  fi

  echo $ac_n "Checking for Xerces-C lib dir ...$ac_c"
  if [ ! $xercesc_libdir ]; then
    if [ $arch = 64 ]; then
      xercesc_libdir=/usr/lib64
    else
      xercesc_libdir=/usr/lib
    fi
  fi
  set $xerces_libdir $extra_libdir
  for adir in $*
  do
    if [ -f "$adir"/libxerces-c.${shlib} ]; then
      xercesc_libdir=$adir
      break
    fi
  done

  if [ -f "$xercesc_libdir"/libxerces-c.${shlib} ]; then
    echo "yes"
  else
    echo "no"
    echo "### $xercesc_libdir/libxerces-c.${shlib} does not exist. "
    echo "Try --with-xercesc-libdir option."
    exit -1
  fi
fi

# ---
echo $ac_n "Writing config.gmk ... $ac_c"

make_config=config/config.gmk

cat > $make_config <<EOF
# config.gmk
#
# Automatically generated by configure script
#
# Makefile definitions included by the top Makefile

G4PY_INSTALL          := $current_dir

Q_SYSTEM              := $system

Q_PREFIX              := $prefix
Q_LIBDIR              := $libdir

Q_G4_INCDIR           := $g4_incdir
Q_G4_LIBDIR           := $g4_libdir
EOF


cat >> $make_config <<EOF

Q_CLHEP_INCDIR        := $clhep_incdir
Q_CLHEP_LIBDIR        := $clhep_libdir
Q_CLHEP_LIB           := $clhep_lib

Q_PYTHON_INCDIR       := $python_incdir
Q_PYTHON_LIBDIR       := $python_libdir
Q_BOOST_INCDIR        := $boost_incdir
Q_BOOST_LIBDIR        := $boost_libdir
Q_BOOST_PYTHON_LIB    := $boost_python_lib

Q_ENABLE_OPENGLX      := $enable_openglx
Q_ENABLE_OPENGLXM     := $enable_openglxm
Q_ENABLE_RAYTRACERX   := $enable_raytracerx

Q_ENABLE_PHYSICSLIST  := $enable_physicslist

Q_ENABLE_GDML         := $enable_gdml
EOF

if [ $enable_gdml = 1 ]; then
cat >> $make_config <<EOF
Q_XERCESC_INCDIR      := $xercesc_incdir
Q_XERCESC_LIBDIR      := $xercesc_libdir
EOF
fi

echo "done"
echo $ac_n "Writing config.status ... $ac_c"
echo $arguments > config.status
echo "done"

# ---
echo ""
echo $ac_n "Enabled support for$ac_c"
if [ $enable_openglx = 1 ]; then
  echo $ac_n " openglx$ac_c"
fi

if [ $enable_openglxm = 1 ]; then
  echo $ac_n " openglxm$ac_c"
fi

if [ $enable_raytracerx = 1 ]; then
  echo $ac_n " raytracerx$ac_c"
fi

if [ $enable_physicslist = 1 ]; then
  echo $ac_n " physicslist$ac_c"
fi

if [ $enable_gdml = 1 ]; then
  echo $ac_n " gdml$ac_c"
fi

echo "."

# ---
echo ""
echo "To build Geant4Py type:"
echo ""
echo "  make"
echo "  make install"
echo ""

exit 0

