#!/bin/sh
################################################################
# Copyright (C) 2001-2008, CompHEP COllaboration
#     on base of script makeCC by A.Pukhov       
# authors: A.Sherstnev and A.Kryukov
#-----------------------------------------------------
# $Id$
#
# $Log$
################################################################

CONFIG_ERROR=0
FFCOMP=g77
CC=gcc
STDCFLAGS=
LHAPDF=0
LIBXML=0

SHELP=
SGCC4=
SDEBUG=
SLHAPDF=
SLIBXML=
SOPTIMISE=
SARCH64=
SROOTHERE=
for i do
  if [ x$i = x--help ]; then
    SHELP=true 
  elif [ x$i = x--with-gcc4 ]; then
    SGCC4=true 
  elif [ x$i = x--debug ]; then
    SDEBUG=true 
  elif [ x$i = x--with-lhapdf ]; then
    SLHAPDF=true 
  elif [ x$i = x--with-libxml ]; then
    SLIBXML=true 
  elif [ x$i = x--optimise ]; then
    SOPTIMISE=true 
  elif [ x$i = x--with-m64 ]; then
    SARCH64=true 
  elif [ x$i = x--with-root ]; then
    SROOTHERE=true 
  fi
done

if (test -n "$SHELP" ) then
  echo './configure --debug | --with-gcc4 | --with-lhapdf | --with-libxml | --optimise | --with-root | --with-m64'
  echo '    --debug       - compilation with debugging symbols'
  echo '    --with-gcc4   - used if your gcc version is 4.0 or higher, FORTRAN compiler is gfortran, '
  echo '                    no linking against libg2c.a'
  echo '    --with-lhapdf - PDF from LHAPDF will be used. The variable LHAPDFPATH should point to '
  echo '                     a directory with lib/liblhapdf.a (set the full path!)'
  echo '    --with-libxml - mix program will use libxml2 in order to analyze and prepare HepML code'
  echo '    --optimise    - -O3 optimisation will be used'
  echo '    --with-m64    - -m64 option will be used, it is useful at 64-bit machines'
  echo '                    WARNING: THIS OPTION (-m64) DOES NOT WORK AT 32-BIT MACHINES!'
  echo '    --with-root   - a program for trasfering events to a rtuples is prepared (see file ROOTFLAGS)'
  echo ' '
  echo ' N.B. User always can change compiler/linker options editting CC, F77, CFLAGS, CLIBS, FORLIBS, RANLIB'
  exit;
fi

cat >test.c <<EOF
int main(void)
{char a; a=1; if (a>-a)return 0;else return 1;}
EOF

echo -n "checking for OS and C-compiler ...   "
OS=`uname`
RANLIB=ranlib
if (test $OS = 'Linux') then
  STDCFLAGS=" -Wall"
  if (test -n "$SGCC4") then
    FFCOMP="gfortran"
  fi
  if (test -n "$SDEBUG") then
    STDCFLAGS="-g ${STDCFLAGS}"
  fi
elif (test $OS = 'IRIX64') then
  STDCFLAGS='-signed -xansi -fullwarn'
  CC=cc
elif (test $OS = 'IRIX') then
  STDCFLAGS='-signed -xansi -fullwarn'
  CC=cc
elif (test $OS = 'HP-UX') then
  STDCFLAGS='-Ae -D_HPUX_SOURCE'
  CC=cc
elif (test $OS = 'SunOS') then
  STDCFLAGS=''
  CC=cc
elif (test $OS = 'Darwin') then
  STDCFLAGS='-fsigned-char'
  CC=gcc
  RANLIB='ranlib -c'
  if (test -n "$SDEBUG") then
    STDCFLAGS="-g ${STDCFLAGS}"
  fi
  if (test -n "$SGCC4") then
    FFCOMP="gfortran"
  fi
else
   CC="gcc";
   STDCFLAGS="-fsigned-char";
   $CC $STDCFLAGS test.c 1>/dev/null 2>/dev/null
   if (test $? -ne 0)then
     echo "Error: Unknown UNIX platform, gcc not found!"
     CC=cc
   fi
fi

echo "     OS=$OS, CC=$CC"

################################################################
#                   test for ANSI compiler existence           #
################################################################

$CC $STDCFLAGS test.c 1>/dev/null 2>/dev/null
if (test $? -ne 0)then
  exit 1
  CONFIG_ERROR=1
fi

################################################################
echo -n "checking char type...   "
./a.out
if (test $? -ne 0) then
  echo "unsigned."
  CONFIG_ERROR=1
  exit 1
else
  echo "signed."
fi

################################################################
# check of user defined interger type for symbolic calculation #
################################################################
num="OFF"
STDCFLAGS1=
for option in $STDCFLAGS
do
  if (test $option = "-DNUM_DOUBLE" -o $option = "-DNUM_LONG_LONG" \
      -o  $option = "-DNUM_LONG") then
     num=$option
  else
    STDCFLAGS1="$STDCFLAGS1 $option"
  fi
done

echo "void main(void){exit(sizeof(long long  ));}" >test.c
if (test $num = "-DNUM_LONG_LONG") then
  $CC $STDCFLAGS1 test.c 1>/dev/null 2>/dev/null
  if (test $? -ne 0) then
     num="OFF"
  fi
fi

if (test "$num" = "OFF") then

################################################################
#      selection of integer type for symbolic calculation      #
################################################################
  cat <<EOF >test.c
void main(void){exit(sizeof(long ));}
EOF

  echo -n "checking for long type...   "
  $CC $STDCFLAGS1 test.c 1>/dev/null 2>/dev/null
  ./a.out
  rc=$?
  echo "long is $rc bytes."
  if (test $rc -ne 8) then
     num="-DNUM_DOUBLE"
  else
     num="-DNUM_LONG"
  fi
fi

STDCFLAGS="$STDCFLAGS1 $num"

################################################################
#                       search for  X11                        #
################################################################

echo -n "checking for X11 libs ...   "
cat >test.c<<EOF
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
void main(void)
{  static Display *display;
  display = XOpenDisplay (NULL);
}
EOF

x_includes=NO
x_libraries=NO

################################################################
#                               test default                   #
################################################################
$CC $STDCFLAGS -c test.c 1>/dev/null 2>/dev/null
if (test $? -eq 0) then 
  x_includes=  
  $CC $STDCFLAGS test.o -lX11 1>/dev/null 2>/dev/null
  if (test $? -eq 0) then x_libraries=; fi
fi

if (test "$x_includes" = "NO" -o "$x_libraries" = "NO" ) then
################################################################
#                       search by xmkmf                        #
################################################################
mv Makefile Makefile.main

  cat > Imakefile <<'EOF'
findx:
	@echo 'im_incroot="${INCROOT}"; im_usrlibdir="${USRLIBDIR}"; im_libdir="${LIBDIR}"'
EOF

  if (xmkmf) >/dev/null 2>/dev/null && test -f Makefile; then 
    eval `make findx 2>/dev/null | grep -v make`
    if (test  "$x_includes" = "NO") then
      $CC $STDCFLAGS -c test.c -I$im_incroot 1>/dev/null 2>/dev/null 
      if (test $? -eq 0) then  
         x_includes=$im_incroot;
         $CC $STDCFLAGS test.o  -lX11 1>/dev/null 2>/dev/null
         if (test $? -eq 0) then 
           x_libraries= ; 
         else 
           $CC $STDCFLAGS test.o -L$im_usrlibdir -lX11 1>/dev/null 2>/dev/null
           if (test $? -eq 0) then x_libraries=$im_usrlibdir; fi
         fi
      fi
    else 
      $CC $STDCFLAGS test.o -L$im_usrlibdir -lX11 1>/dev/null 2>/dev/null
      if (test $? -eq 0) then  x_libraries=$im_usrlibdir; fi
    fi
  fi
     if (test -f Imakefile) then  rm Imakefile; fi
     if (test -f  Makefile) then  rm  Makefile; fi
mv Makefile.main Makefile
fi


XLIST="X11R6/Z X11R5/Z X11R4/Z Z/X11R6 Z/X11R5 Z/X11R4 local/X11R6/Z
local/X11R5/Z local/X11R4/Z local/Z/X11R6 local/Z/X11R5 local/Z/X11R4
X11/Z Z/X11 local/X11/Z local/Z/X11 X386/Z x386/ Z local/Z  openwin/Z 
openwin/share/Z"

################################################################
#                  search x-includes from XLIST                #
################################################################
if (test "$x_includes" = "NO") then
  for dir in $XLIST
  do
    test_dir=/usr/`echo $dir|sed s/Z/include/`
    if ( test -r "$test_dir/X11/Xlib.h") then
      $CC $STDCFLAGS -c test.c -I$test_dir 1>/dev/null 2>/dev/null
      if (test $? -eq 0) then
        x_includes=$test_dir
        $CC $STDCFLAGS test.o  -lX11 1>/dev/null 2>/dev/null
        if (test $? -eq 0) then
          x_libraries= ;
        else 
          test_dir=/usr/`echo $dir|sed s/Z/lib/`
          $CC $STDCFLAGS test.o -L$test_dir -lX11 1>/dev/null 2>/dev/null 
          if (test $? -eq 0) then 
            x_libraries=$test_dir; 
            break
          fi
        fi
      fi
    fi
  done
fi

################################################################
#                search x-includes from XLIST                  #
################################################################
if (test "$x_includes" != "NO"  -a "$x_libraries" = "NO" ) then
  for dir in $XLIST
  do
    test_dir=/usr/`echo $dir|sed s/Z/lib/`
    $CC $STDCFLAGS test.o -L$test_dir -lX11 1>/dev/null 2>/dev/null 
    if (test $? -eq 0) then 
      x_libraries=$test_dir; 
      break
    fi
  done
fi
rm a.out test.*

if (test "$x_includes" = "NO"  -o "$x_libraries" = "NO" ) then
  echo 'X11 is not detected '
  echo 'Maybe you need to install the xlibs-dev package'
  CONFIG_ERROR=1
  exit 1
fi


################################################################
# Set xinclude info
if ( test "$x_includes") then  
  STDCFLAGS="$STDCFLAGS -I$x_includes" 
#  echo X-includes=$x_includes >config.log
#else 
#  echo default  X-includes >config.log
fi

################################################################
# Set xlib info
if (test "$x_libraries") then
  CLIBS="-L$x_libraries"
#  echo X-libraries=$x_libraries >config.log
#else
#  echo default X-libraries  >config.log
fi

echo "OK"

################################################################
#          set proper standard C/FORTRAN libs                  #
################################################################

CLIBS="-Llib $CLIBS -lX11 -lm"
if (test -n "$SGCC4") then
  CLIBS="$CLIBS"
  FORLIB="-lgfortran"
else 
  CLIBS="$CLIBS -lg2c"
  FORLIB=""
fi

# MacOS correction for FORTRAN
if (test $OS = 'Darwin') then
  FORLIB="$FORLIB -lgcc"
fi

################################################################
#                      search for LHAPDF lib                   #
################################################################
if (test -n "$SLHAPDF") then
  LHAPDF=1
  echo -n "checking for LHAPDF...   "
  if [ x$LHAPDFPATH = x ]; then
    echo "Variable LHAPDFPATH is not defined! exit..."
    exit 1
  fi
  echo "OK"
fi

################################################################
#                      search for LIBXML2 lib                  #
################################################################
if (test -n "$SLIBXML") then
  LIBXML=1
  echo -n "checking for libxml2...   "
  if [ x$LIBXMLPATH = x ]; then
    echo "Variable LIBXMLPATH is not defined! So, the standard path (/usr/include) will be used... OK"
  else
  echo "OK"
  fi
fi

################################################################
#                      search for ROOT headers/libs            #
################################################################
if (test -n "$SROOTHERE") then
  LIBROOT=1
  echo -n "checking for root libraries...   "
  if [ x$ROOTSYS = x ]; then
    echo "Variable ROOTSYS is not defined! ROOT will not be used!"
    exit 1
  else
   echo "OK"
   ROOTINC=`$ROOTSYS/bin/root-config --cflags`
   ROOTLIB=`$ROOTSYS/bin/root-config --libs`
   STDCFLAGS="$STDCFLAGS -DROOTused"
  fi
fi

################################################################
#                      optimisation options                    #
################################################################
if (test -n "$SOPTIMISE") then
  STDCFLAGS="$STDCFLAGS -O3" 
fi

################################################################
#                      -m64 option                             #
################################################################
if (test -n "$SARCH64") then
  STDCFLAGS="$STDCFLAGS -m64" 
fi

################################################################
# Output info
STDCFLAGS="$STDCFLAGS -DCOMPHEP"

#echo $CC $STDCFLAGS > CC

#if (test "x$1" = "xcompilar") then
#   echo $CC > CC
#elif (test "x$1" = "xerror") then
#   echo $CONFIG_ERROR
#elif (test "x$1" = "xSTDCFLAGS") then
#   echo $STDCFLAGS > STDCFLAGS
#else
#  echo $CC $STDCFLAGS
#fi

CFLAGS=""
if (test $LHAPDF = 1) then
  CFLAGS="$CFLAGS -DLHAPDF"
  CLIBS="-L$LHAPDFPATH/lib -lLHAPDF $CLIBS"
fi

if (test $LIBXML = 1) then
  if [ x$LIBXMLPATH = x ]; then
    CFLAGS="$CFLAGS -DLIBXML -I/usr/include/libxml2"
    CLIBS=" -lxml2 $CLIBS"
  else
    CFLAGS="$CFLAGS -DLIBXML -I$LIBXMLPATH/include/libxml2"
    CLIBS=" -L$LIBXMLPATH/lib -lxml2 $CLIBS"
  fi
fi
CFLAGS="$STDCFLAGS $CFLAGS "

echo $CC     > CC
echo $FFCOMP > F77
echo $CFLAGS > CFLAGS
echo $CLIBS  > CLIBS
echo $FORLIB > FORLIBS
echo $RANLIB > RANLIB
echo $ROOTINC > ROOTFLAGS
echo $ROOTLIB > ROOTLIBS

echo "======================================="
echo "To compile CompHEP input make command"
echo "======================================="
