#! /bin/sh -
# ======================================================================
#  A configure script for Geant4Py
# ======================================================================
export LANG=C

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-g4install-dir=DIR  Geant4 installed dir

  --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-python3            Use Python3

  --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-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]
  raytracerx   RayTracerX support [disable]

Supported Architectures:
  linux           for Linux gcc (32bit)
  linux64         for Linux gcc (64bit)
  linuxx8664gcc   for Linux gcc (64bit)
  macosx          for Apple OS X with gcc

EOF
}

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

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

g4_incdir=/zzz
g4_libdir=/zzz

python_incdir=/zzz
python_exe=python
with_python3=0
boost_incdir=/usr/include
boost_python_lib=boost_python

enable_openglx=1
enable_openglxm=0
enable_raytracerx=0
enable_gl2ps=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-g4install-dir=*)    g4_install=$optarg                ;;
    --with-extra-dir=*)        extra_dir=$optarg                 ;;
    --with-python-incdir=*)    python_incdir=$optarg             ;;
    --with-python-libdir=*)    python_libdir=$optarg             ;;
    --with-python3 )           python_exe=python3
                               with_python3=1
                               boost_python_lib=boost_python3    ;;
    --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_incdir=$g4_install/include/Geant4

if [ ! -d "$g4_incdir" ]; then
  echo "no"
  echo $ac_n "### Geant4 include directory does not exist. $ac_c"
  exit -1
fi
echo "$g4_incdir"

# ---
echo $ac_n "Checking for G4 lib dir ... $ac_c"
if [ $arch = 64 ]; then
  g4_libdir=$g4_install/lib64
else
  g4_libdir=$g4_install/lib
fi

if [ ! -d "$g4_libdir" ]; then
  echo "no"
  echo $ac_n "### Geant4 lib directory does not exist. $ac_c"
  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"

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

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 GL2PS support ...$ac_c"
if [ -f "$g4_libdir"/libG4gl2ps.${shlib} ]; then
  echo "yes"
  enable_gl2ps=1
else
  echo "no"
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_PYTHON_INCDIR       := $python_incdir
Q_PYTHON_LIBDIR       := $python_libdir
Q_PYTHON              := $python_exe
Q_WITH_PYTHON3        := $with_python3
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_GL2PS        := $enable_gl2ps

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_gl2ps = 1 ]; then
  echo $ac_n " gl2ps$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

