#!/bin/sh
#/*************************************************************************/
#/* Program Database Toolkit (PDT)                                        */
#/* Copyright (C) 1998-2000                                               */
#/* University of Oregon, Department of Computer and Information Science  */
#/* Forschungszentrum Juelich, Zentralinstitut fuer Angewandte Mathematik */
#/* Los Alamos National Laboratory, Advanced Computing Laboratory         */
#/*************************************************************************/

echo "Program Database Toolkit (PDT) Configuration"
echo "--------------------------------------------"

#-- Set up defaults
#------------------
arch_given=no
gnu_given=no
enable_old_headers=no
PDTDIR=`pwd`
COMPDIR=

#-- Default compilers
#--------------------
cxx_compiler=default
useropt=default

#-- Set up portable echo command
#-------------------------------
case "`echo 'x\c'`" in
'x\c')  echo="echo -n"  nnl= ;;     #BSD
x)      echo="echo"     nnl="\c";;  #SysV
*)      echo "Cannot setup echo. What weird machine do you have?"
        exit 1;;
esac

#-- Determine system architecture
#--------------------------------
case `uname -s` in
SunOS)
  ARCH=SOL2
  PLATFORM=solaris2
  echo "Looks like a SUN Solaris machine ..."
  ;;
IRIX)
  ARCH=IRIXN32
  PLATFORM=sgin32
  echo "Looks like an SGI IRIXN32 machine ..."
  ;;
IRIX64)
  ARCH=IRIX64
  PLATFORM=sgi64
  echo "Looks like an SGI IRIX64 machine ..."
  ;;
Linux)
  ARCH=LINUX
  PLATFORM=linux
  echo "Looks like a Linux machine ..."
  ;;
sn6711)
  ARCH=CRAY
  PLATFORM=t3e
  echo "Looks like a Cray T3E machine ..."
  ;;
HP-UX)
  ARCH=HPUX
  PLATFORM=hp9000s700
  echo "Looks like an HP-UX machine ..."
  ;;
AIX)
  ARCH=AIX
  PLATFORM=rs6000
  echo "Looks like an IBM RS/6000 AIX machine ..."
  ;;
OSF1)
  ARCH=OSF1
  PLATFORM=alpha
  echo "Looks like a Compaq Alpha OSF1 machine ..."
  ;;
Darwin)
  ARCH=DARWIN
  PLATFORM=apple
  echo "Looks like an Apple Darwin machine ..."
  ;;
*)
  echo "`uname -s` is not yet supported"
  exit
  ;;
esac

#-- Examine system for C++ compilers
#-----------------------------------
$echo "Looking for C++ compilers ${nnl}"
plist=`echo $PATH | sed -e 's/:/ /g'`
for dir in $plist
do
  if [ -x $dir/CC -a -z "$path_CC" ]
  then
    $echo ".${nnl}"
    #echo "    Using $dir/CC"
    path_CC=$dir/CC
  fi
  if [ -x $dir/KCC -a -z "$path_KCC" ]
  then
    $echo ".${nnl}"
    #echo "    Using $dir/KCC"
    path_KCC=$dir/KCC
  fi
  if [ -x $dir/g++ -a -z "$path_gxx" ]
  then
    $echo ".${nnl}"
    #echo "    Using $dir/g++"
    path_gxx=$dir/g++
  fi
  if [ -x $dir/eg++ -a -z "$path_egxx" ]
    then
      $echo ".${nnl}"
      #echo "    Using $dir/eg++"
      path_egxx=$dir/eg++
    elif [ -x $dir/egcs -a -z "$path_egxx" ]
    then
      $echo ".${nnl}"
      #echo "    Using $dir/egcs"
      path_egxx=$dir/egcs
  fi
  if [ -x $dir/c++ -a -z "$path_cpp" ]
  then
    $echo ".${nnl}"
    #echo "    Using $dir/c++"
    path_cpp=$dir/c++
  fi
  if [ -x $dir/pgCC -a -z "$path_pgCC" ]
  then
    $echo ".${nnl}"
    #echo "    Using $dir/pgCC"
    path_pgCC=$dir/pgCC
  fi
  if [ -x $dir/xlC -a -z "$path_xlC" ]
  then
    $echo ".${nnl}"
    #echo "    Using $dir/xlC"
    path_xlC=$dir/xlC
  fi
  if [ -x $dir/cxx -a -z "$path_cxx" ]
  then
    $echo ".${nnl}"
    #echo "    Using $dir/cxx"
    path_cxx=$dir/cxx
  fi
    
done
echo " done"

#-- Process arguments
#--------------------
for arg in "$@";
do
  case $arg in

  -egcs|-EGCS|--egcs|--EGCS)
    gnu_given=yes
    cxx_compiler=EGXX
    if [ -z "$path_egxx" ]
    then
      echo "ERROR: eg++ not found"
      exit
    else
      echo "==> Using $path_egxx"
      path_cxx=$path_egxx
      #cxx_opt=gnu
    fi
    shift
    ;;

  -gnu|-GNU|--gnu|--GNU)
    gnu_given=yes
    cxx_compiler=GXX
    if [ -z "$path_gxx" ]
    then
      echo "ERROR: g++ not found"
      exit
    else
      echo "==> Using $path_gxx"
      path_cxx=$path_gxx
      #cxx_opt=gnu
    fi
    shift
    ;;

  -kai|-kcc|-KAI|-KCC|--kai|--kcc|--KAI|--KCC)
    cxx_compiler=KCC
    if [ -z "$path_KCC" ]
    then
      echo "ERROR: KCC not found"
      exit
    else
      echo "==> Using $path_KCC"
      path_cxx=$path_KCC
      #cxx_opt=kcc
    fi
    shift
    ;;

  -CC|--CC)
    cxx_compiler=CC
    if [ -z "$path_CC" ]
    then
      echo "ERROR: CC not found"
      exit
    else
      echo "==> Using $path_CC"
      path_cxx=$path_CC
      #cxx_opt=cc
    fi
    shift
    ;;

  -c++|--c++|-CPP|--CPP|-C++|--C++)
    cxx_compiler=CPP
    if [ -z "$path_cpp" ]
    then
      echo "ERROR: c++ not found"
      exit
    else
      echo "==> Using $path_cpp"
      path_cxx=$path_cpp
      #cxx_opt=cc
    fi
    shift
    ;;

  -cxx|--cxx|-CXX|--CXX)
    cxx_compiler=CXX
    if [ -z "$path_cxx" ]
    then
      echo "ERROR: cxx not found"
      exit
    else
      echo "==> Using $path_cxx"
      path_cxx=$path_cxx
      #cxx_opt=cc
    fi
    shift
    ;;

  -xlC|--xlC|-xlc|--xlc|-XLC|--XLC|-IBM|--IBM)
    cxx_compiler=XLC
    if [ -z "$path_xlC" ]
    then
      echo "ERROR: xlC not found"
      exit
    else
      echo "==> Using $path_xlC"
      path_cxx=$path_xlC
      #cxx_opt=cc
    fi
    shift
    ;;

  -pgCC|--pgCC|-pgcc|--pgcc|-PGI|--PGI)
    echo "Currently, we do not support the PGI C++ compiler"
    exit 1 	
    cxx_compiler=PGI
    if [ -z "$path_pgCC" ]
    then
      echo "ERROR: pgCC not found"
      exit
    else
      echo "==> Using $path_pgCC"
      path_cxx=$path_pgCC
      #cxx_opt=cc
    fi
    shift
    ;;

  -arch=*)
    arch_given=yes
    ARCH=`echo $arg | sed -e 's/-arch=//' -e 's/ /_/g'`
    case $ARCH in 
      IRIXO32)
	PLATFORM=sgi32
        ;;
      IRIXN32)
        PLATFORM=sgin32
        ;;
      IRIX64)
	PLATFORM=sgi64
	;;
      *)
        echo "ERROR: -arch=IRIXO32|IRIXN32|IRIX64"
	exit
	;;
    esac
    shift
    ;;

  -compdir=*)
    COMPDIR=`echo $arg | sed -e 's/-compdir=//'`
    shift
    ;;

  -enable-old-headers|--enable-old-headers)
    enable_old_headers=yes
    shift
    ;;

  -useropt=*)
    useropt=`echo $arg | sed -e 's/-useropt=//' -e 's/ /#/g'`
    shift
    ;;

  --useropt=*)
    useropt=`echo $arg | sed -e 's/--useropt=//' -e 's/ /#/g'`
    shift
    ;;

  -help|--help)
    if [ $arg != "-help" -a $arg != "--help" ]
    then
      echo "ERROR: unknown option $arg"
    fi
    echo "Usage: ./configure [-KAI|-KCC|-EGCS|-GNU|-CC|-c++|-cxx|-xlC]"
    echo "                   [-arch=IRIXO32|IRIXN32|IRIX64] [-help]"
    echo "                   [-compdir=<compdir>]"
    echo "                   [-enable-old-headers]"
    echo "                   [-useropt=<options>]"
    exit
    ;;
  esac
done

#-- For GNU compilers, if no architecture is given, use defaults
#---------------------------------------------------------------
case `uname -s` in
IRIX|IRIX64)
  if [ $gnu_given = yes -a $arch_given = yes -a $ARCH != IRIXN32 ]
  then
    echo "ERROR: For GNU compiler, -arch=IRIXN32"
    exit
  fi
  if [ $gnu_given = yes -a $arch_given = no ]
  then
    ARCH=IRIXN32
    PLATFORM=sgin32
    echo "==> Setting default GNU platform to $PLATFORM"
  fi
  ;;
esac

echo "==> ARCH is $ARCH"
echo "==> PLATFORM is $PLATFORM"

#-- Set up directory structure
#-----------------------------
if [ ! -d ductape/bin ]
then
  mkdir ductape/bin
fi
if [ ! -d $PLATFORM ]
then
  echo "ERROR: $PLATFORM directory missing"
  exit
fi
if [ ! -d $PLATFORM/bin ]
then
  echo "ERROR: $PLATFORM/bin directory missing"
  exit
fi
if [ ! -d $PLATFORM/$COMPDIR ]
then
  mkdir $PLATFORM/$COMPDIR
fi
cp $PDTDIR/ductape/inc/*.h $PDTDIR/ductape/inc/*.inl $PDTDIR/include
if [ ! -h $PLATFORM/$COMPDIR/include ]
then
  ln -s $PDTDIR/include $PLATFORM/$COMPDIR/include
fi
if [ ! -d $PLATFORM/$COMPDIR/bin ]
then
  mkdir $PLATFORM/$COMPDIR/bin
fi
if [ ! -d $PLATFORM/$COMPDIR/lib ]
then
  mkdir $PLATFORM/$COMPDIR/lib
fi
if [ ! -h i386_linux ]
then
  ln -s $PDTDIR/linux i386_linux
fi
if [ ! -h sgi8k ]
then
  ln -s $PDTDIR/sgin32 sgi8k
fi
if [ "x$COMPDIR" != "x" ]
then
  rm -f $PLATFORM/$COMPDIR/bin/edgcpfe
  rm -f $PLATFORM/$COMPDIR/bin/taucpdisp
  ln -s $PDTDIR/$PLATFORM/bin/edgcpfe   $PLATFORM/$COMPDIR/bin
  ln -s $PDTDIR/$PLATFORM/bin/taucpdisp $PLATFORM/$COMPDIR/bin
fi

#-- If no compiler specified; use "best" one available
#-----------------------------------------------------
if [ $cxx_compiler = default ]
then
  if [ -n "$path_KCC" ]
  then
    cxx_compiler=KCC
    echo "==> Using $path_KCC"
    path_cxx=$path_KCC
    #cxx_opt=kcc
  elif [ -n "$path_egxx" ]
  then
    cxx_compiler=EGXX
    echo "==> Using $path_egxx"
    path_cxx=$path_egxx
    #cxx_opt=gnu
  elif [ -n "$path_gxx" ]
  then
    cxx_compiler=GXX
    echo "==> Using $path_gxx"
    path_cxx=$path_gxx
    #cxx_opt=gnu
  elif [ -n "$path_cpp" ]
  then
    cxx_compiler=CPP
    echo "==> Using $path_cpp"
    path_cxx=$path_cpp
    #cxx_opt=cc
  elif [ -n "$path_CC" ]
  then
    cxx_compiler=CC
    echo "==> Using $path_CC"
    path_cxx=$path_egxx
    #cxx_opt=cc
  elif [ -n "$path_cxx" ]
  then
    cxx_compiler=CXX
    echo "==> Using $path_cxx"
    path_cxx=$path_cxx
    #cxx_opt=cc
  elif [ -n "$path_xlC" ]
  then
    cxx_compiler=XLC
    echo "==> Using $path_xlC"
    path_cxx=$path_xlC
    #cxx_opt=cc
  elif [ -n "$path_pgCC" ]
  then
    cxx_compiler=PGI
    echo "==> Using $path_pgCC"
    path_cxx=$path_pgCC
    #cxx_opt=cc
  else
    echo "ERROR: no C++ compiler found"
    exit
  fi
fi
if [ "$useropt" = default ]
then
  if [ $cxx_compiler = KCC ]
  then 
    useropt=+K2
  else
    useropt=-O2
  fi
fi
opt=`echo $useropt | sed -e 's/#/ /g' -e 's/^$/(none)/'`
echo "==> Default compiler options are $opt"
opt=`echo $useropt | sed -e 's/#/ /g'`

#-- if -arch is specified use it
#--------------------------------
if [ $ARCH = IRIXO32 ] 
then 
  path_cxx="$path_cxx -o32"
  opt="-o32 $opt"
fi

if [ $ARCH = IRIXN32 ] 
then
  path_cxx="$path_cxx -n32"
  opt="-n32 $opt"
fi

if [ $ARCH = IRIX64 ] 
then
  path_cxx="$path_cxx -64"
  opt="-64 $opt"
fi

if [ $enable_old_headers = yes ]
then
  opt="$opt -D_OLD_HEADER_"
fi

#-- Fix Makefiles
#----------------
if sed -e "s@^PLATFORM=.*\$@PLATFORM=$PLATFORM@" \
       -e "s@^COMPDIR=.*\$@COMPDIR=$COMPDIR@" \
       Makefile > /tmp/config.$$
then
  mv /tmp/config.$$ Makefile
else
  echo "ERROR: cannot configure Makefiles"
  exit
fi
if sed -e "s@\(.*\)#EOC#\(.*\)@\2\1#EOC#@" \
       -e "s@#$cxx_compiler#\(.*\)@\1#$cxx_compiler#@" \
       -e "s@^USEROPT=.*\$@USEROPT=$opt@" \
       -e "s@#$ARCH#\(.*\)@\1#$ARCH#@" \
       ductape/Makefile > /tmp/config.$$
then
  mv /tmp/config.$$ ductape/Makefile
  echo "==> Makefiles were configured"
else
  echo "ERROR: cannot configure Makefiles"
  exit
fi

# DON'T Discover path to the header files. Use include/kai dir
##-- Discover the paths for standard system include directories
##-------------------------------------------------------------
#$echo "Looking for C++ compilers standard include paths ... ${nnl}"
#cat << EOF > /tmp/get_stdinc.C
##include <ctype.h>
##include <iostream.h>
#EOF
#if [ $cxx_compiler = EGXX -o $cxx_compiler = GXX ]
#then 
#  echo "#include <_G_config.h>" >> /tmp/get_stdinc.C
#  echo "#include <iso646.h>" >> /tmp/get_stdinc.C
#fi
#
#cat << EOF > /tmp/find_stdinc.awk
#BEGIN {cinc=""; cppinc=""; g1inc=""; g2inc="";}
#/^#[0123456789]+ ".+\/ctype.h"/ {cinc = substr(\$2, 2, length(\$2)-10);}
#/^#line [0123456789]+ ".+\/ctype.h"/ {cinc = substr(\$3, 2, length(\$3)-10);}
#/^# [0123456789]+ ".+\/ctype.h"/ {cinc = substr(\$3, 2, length(\$3)-10);}
#/^#[0123456789]+ ".+\/iostream.h"/ {cppinc = substr(\$2, 2, length(\$2)-13);}
#/^#line [0123456789]+ ".+\/iostream.h"/ {cppinc = substr(\$3, 2, length(\$3)-13);}
#/^# [0123456789]+ ".+\/iostream.h"/ {cppinc = substr(\$3, 2, length(\$3)-13);}
#/^#[0123456789]+ ".+\/_G_config.h"/ {g1inc = substr(\$2, 2, length(\$2)-14);}
#/^#line [0123456789]+ ".+\/_G_config.h"/ {g1inc = substr(\$3, 2, length(\$3)-14);}
#/^# [0123456789]+ ".+\/_G_config.h"/ {g1inc = substr(\$3, 2, length(\$3)-14);}
#/^#[0123456789]+ ".+\/iso646.h"/ {g2inc = substr(\$2, 2, length(\$2)-11);}
#/^#line [0123456789]+ ".+\/iso646.h"/ {g2inc = substr(\$3, 2, length(\$3)-11);}
#/^# [0123456789]+ ".+\/iso646.h"/ {g2inc = substr(\$3, 2, length(\$3)-11);}
#END { if (cinc != "") printf("-I%s ", cinc); if (cppinc != "") printf("-I%s ", cppinc); if (g1inc != "") printf("-I%s ", g1inc); if (g2inc != "") printf("-I%s ", g2inc); printf("\n"); }
#EOF
#
#if ( $path_cxx -E /tmp/get_stdinc.C) 1>/dev/null 2>&1
#then
#  stdinc=`$path_cxx -E /tmp/get_stdinc.C | awk -f /tmp/find_stdinc.awk`
#else
#  echo "Error"
#  echo "Cannot auto-detect the path to the header files."
#  echo "Enter the directory containing system and C++ header files "
#  echo "(e.g., -I/usr/include -I/usr/local/packages/kcc/KCC_BASE/include):"
#  read stdinc
#fi


#xstdinc=`$path_cxx -E /tmp/get_stdinc.C | sed -n \
#  -e '/\.h"/s@^.*"\(.*\)/ctype.h".*$@-I\1@p' \
#  -e '/\.h"/s@^.*"\(.*\)/iostream.h".*$@-I\1@p' | sort -u | tr '\012' ' '`

#rm -f /tmp/get_stdinc.C /tmp/find_stdinc.awk

#echo "done"
#echo "==> Using $stdinc"
#######################################################################

# Find the compiler defines
cat << EOF  > getword.c
#include <stdio.h>

int main()
{
  int c;
  while ((c = fgetc(stdin)) != EOF) 
  { 
    putc(c,stdout);
    if (c == ' ')
      putc('\n',stdout);
  }
  return 0;

}
EOF

cat << EOF > getcompdefines
#!/bin/sh
\$* -v -c getword.c 1>pdtdata 2>&1
if [ ! -f getword ] 
then
  cc getword.c -o getword
fi 
./getword < pdtdata | grep '^-D' | grep -v ':'  >pdtdata2
echo "COMPILER_OPTS=\\""
cat pdtdata2
echo "\\""
rm -f pdtdata pdtdata2 
EOF

chmod +x getcompdefines
compdefines=`./getcompdefines $path_cxx`
rm -f getcompdefines getword.c getword.o getword

# Find the directory where edgcpfe and taucpdisp will reside 
bindir=`pwd | sed -e 's,^/tmp_mnt/nfs,,' -e 's,^/tmp_mnt,,'`
# The following is now handled by make and make install
#if [ ! -f $bindir/$PLATFORM/lib/libpdb-$cxx_opt.a ] 
#then
#  echo 
#  echo "ERROR: The desired library $PLATFORM/libpdb-$cxx_opt.a is not available."
#  echo "       Please choose another compiler or send mail to pdtoolkit@acl.lanl.gov."
#  echo 
#else 
#  echo "cd $bindir/$PLATFORM/lib; rm -f libpdb.a; ln -s libpdb-$cxx_opt.a libpdb.a"
#  `cd $bindir/$PLATFORM/lib; rm -f libpdb.a; ln -s libpdb-$cxx_opt.a libpdb.a`
#fi


#-- Fix cparse/cxxparse
#---------------
filestobefixed='cparse cxxparse f90parse'
for f in $filestobefixed 
do
  if sed -e "s@\(.*\)#EOC#\(.*\)@\2\1#EOC#@" \
         -e "s@#$cxx_compiler#\(.*\)@\1#$cxx_compiler#@" \
         -e "s@#$ARCH#\(.*\)@\1#$ARCH#@" \
         -e "s@^BINDIR=.*\$@BINDIR=$bindir/$PLATFORM/$COMPDIR/bin@" \
         -e "s@^PDTDIR=.*\$@PDTDIR=$bindir@" \
         $f.proto > /tmp/$f.$$
  then
    rm -f $f
    if [ $ARCH = "OSF1" -o $ARCH = "SOL2" ] 
    then
      echo "#!/bin/ksh " > $f 
    else
      echo "#!/bin/sh " > $f
    fi
    echo $compdefines >> $f
    echo "" >> $f
    cat /tmp/$f.$$ >> $f
    mv $f $PLATFORM/$COMPDIR/bin/$f
    chmod ugo+x $PLATFORM/$COMPDIR/bin/$f
    rm -f /tmp/$f.$$
    echo "==> $f was configured"
  else
    echo "ERROR: cannot configure $f"
    exit
  fi
done

#-- Complete configuration
#-------------------------
echo ""
echo "Configuration is complete!"
echo ""
echo "Run \"make\" and \"make install\""
echo "Add \"`pwd`/$PLATFORM/$COMPDIR/bin\" to your path"
exit 0

