#!/bin/sh
##################################################
# Copyright (C) 2001-2003, CompHEP COllaboration
#     on base of script makeCC by A.Pukhov       
# configure script, v1.0 10/03/2002 Sherstnev    
# configure script, v1.5 08/01/2003 Sherstnev    
# Modified by A.Kryukov(kryukov@theory.sinp.msu.ru)
#-----------------------------------------------------
# $Id: configure,v 1.12 2003/07/20 14:32:43 kryukov Exp $
#
# $Log: configure,v $
# Revision 1.12  2003/07/20 14:32:43  kryukov
# include lib dir into binary distributive
#
# Revision 1.11  2003/06/25 18:46:41  kryukov
# *** empty log message ***
#
# Revision 1.10  2003/06/24 12:05:08  kryukov
# Warning message if user try to use MSSN and NO_CERN_LIB flag define
#
# Revision 1.9  2003/06/15 16:38:54  kryukov
# Just misprint
#
# Revision 1.8  2003/04/04 18:43:43  kryukov
# Fix minor bug in debug mode
#
# Revision 1.7  2003/02/23 17:26:57  kryukov
# Add flag '-g' if necessary for Linux
#
# Revision 1.6  2003/01/25 19:09:35  kryukov
# no message
#
##################################################

CONFIG_ERROR=0
CC=gcc
CFLAGS=
CERNLIB=/cern/pro/lib

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

echo -n "Checking OS and C-compiler ..."
OS=`uname`
if (test $OS = 'Linux') then
  CFLAGS=" -Wall"
  if [ x$1 = xDEBUG ]; then
  	CFLAGS="-g ${CFLAGS}"
  fi
  CC=gcc
elif (test $OS = 'IRIX64') then
  CFLAGS='-signed -xansi -fullwarn'
  CC=cc
elif (test $OS = 'IRIX') then
  CFLAGS='-signed -xansi -fullwarn'
  CC=cc
elif (test $OS = 'HP-UX') then
  CFLAGS='-Ae -D_HPUX_SOURCE'
  CC=cc
elif (test $OS = 'SunOS') then
  CFLAGS=''
  CC=cc
else
   CC="gcc -fsigned-char";
   $CC  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 $CFLAGS 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"
CFLAGS1=
for option in $CFLAGS
do
  if(test $option = "-DNUM_DOUBLE" -o $option = "-DNUM_LONG_LONG" \
      -o  $option = "-DNUM_LONG") then
     num=$option
  else
    CFLAGS1="$CFLAGS1 $option"
  fi
done

echo "void main(void){exit(sizeof(long long  ));}" >test.c
if(test $num = "-DNUM_LONG_LONG") then
  $CC $CFLAGS1 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 long type..."
  $CC $CFLAGS1 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

CFLAGS="$CFLAGS1 $num"

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

echo -n "Cheking 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

##################################################
# I) test default

$CC $CFLAGS -c test.c 1>/dev/null 2>/dev/null
if(test $? -eq 0) then 
  x_includes=  
  $CC $CFLAGS 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
##################################################
# II) 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 $CFLAGS -c test.c -I$im_incroot 1>/dev/null 2>/dev/null 
      if(test $? -eq 0) then  
         x_includes=$im_incroot;
         $CC $CFLAGS test.o  -lX11 1>/dev/null 2>/dev/null
         if(test $? -eq 0) then 
           x_libraries= ; 
         else 
           $CC $CFLAGS 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 $CFLAGS 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 $CFLAGS -c test.c -I$test_dir 1>/dev/null 2>/dev/null
      if(test $? -eq 0) then
        x_includes=$test_dir
        $CC $CFLAGS 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 $CFLAGS 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 $CFLAGS 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 >config.log
  CONFIG_ERROR=1
  exit 1
fi


################################################################
# Set xinclude info
if( test "$x_includes") then  
  CFLAGS="$CFLAGS -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"

##################################################
#               search for CERN lib              #
##################################################

echo -n "Checking CERNlibs..."

if [ -e $CERNLIB/libmathlib.a ]; then
    CLIBS="$CLIBS -L$CERNLIB"
    xx=`ls $CERNLIB/libisajet* 2>/dev/null`
    if [ $? == 0 ]; then
        for yy in $xx; do
	      yyy=`basename $xx | sed s/[a-z,A-Z,\.]//g`
          break
        done
        CLIBS="$CLIBS -lisajet$yyy -lmathlib"
    else
      echo "Warning: the configure script does not find isajet library."
      echo "         You can not use MSSM models.           "
    fi
else
  CFLAGS="$CFLAGS -DNO_CERN_LIB"
  echo "          "
  echo "Warning: the configure script does not find CERN libraries."
  echo "         You can not use MSSM models. "
  echo "         If you sure that your system contains the cernlib package,"
  echo "         change path name to cernlib (a variable CERNLIB at the "
  echo "         beginning of the configure script). "
fi
CLIBS="-L`pwd`/lib $CLIBS -lX11 -lm"

echo "OK"

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

#echo $CC $CFLAGS > CC

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

echo $CC     > CC
echo $CFLAGS > CFLAGS
echo $CLIBS  > CLIBS

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