#! /bin/sh
#
# This file is part of the PARI/GP package.
#  
# PARI/GP is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation. It is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY WHATSOEVER.
# 
# Check the License for details. You should have received a copy of it, along
# with the package; see the file 'COPYING'. If not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Configuration file for GP/PARI.  Run Configure -help for Usage.
#
# Perl's Configure and GNU autoconfig were of much help in writing these files.
# $Id: Configure,v 1.112 2003/02/10 01:08:51 karim Exp $

TOP=`pwd`
config_dir=config
data_dir=data
doc_dir=doc
emacs_dir=emacs
examples_dir=examples
misc_dir=misc
src_dir=src
extract_dir_list="$doc_dir $emacs_dir $misc_dir" # for extract_files

cd $config_dir
# Process options, initialize
. ./get_config_options
####################### CONFIGURE - SHELL ###################################
. ./get_nl # how to get echo without \n ? (for config questions)
. ./get_PATH
#  We might need the following :
echo Looking for some tools first ...
list='ld zcat gzip ranlib perl emacs'
pathspace=`echo $PATH | sed -e "s/$dir_sep/ /g" | sed -e 's,\\\\,/,g'`

for file in $list; do
  x=`./locate $file '' $pathspace`
  eval $file=$x
  case $x in
# support also DOS filesystems (hard drive prepended)
   ?:/*|/*) echo ..."$file is $x";;
      *) echo ..."I could not find $file." >&2;;
  esac
done
if test -z "$zcat" -a -n "$gzip"; then zcat="$gzip -dc"; fi

####################### CONFIGURE - ARCHITECTURE ############################
. ./get_archos # arch, osname, asmarch, pretty
####################### CONFIGURE - COMPILATION #############################
. ./get_cc   # $_cc_list (includes 'optimization'), extraflag
. ./get_as   # AS, ASFLAGS, KERNELCPPFLAGS, gnuas
. ./get_ld   # $_ld_list
. ./get_dlld # $_dlld_list, fix libpari_base
####################### CONFIGURE - LIBC ####################################
. ./get_double_format # doubleformat, sizeof_long, update pretty
. ./get_libc # $_has_list, update LIBS
####################### CONFIGURE - LIBRARIES ###############################
# Looking for libraries: gmp, X11, gnuplot, readline
echo Checking for optional libraries and headers...
. ./get_libpth # initialize
if test -n "$kernel"; then # kernel explicitly provided
  case "$kernlvl1" in
    none) with_gmp= ;;
    gmp) if test -z "$with_gmp"; then with_gmp=yes; fi ;;
  esac
fi
if test -n "$with_gmp"; then
  . ./get_gmp  # $_gmp_list
fi
if test "$optimization" != profiling; then
  . ./get_X11  # X11, Xincroot, extralib
  . ./get_gnuplot_libs # gnuplot, gnuplot_libs
  . ./get_readline # $_readline_list (includes 'readline')
  . ./get_graphic_lib # which_graphic_lib, graphic_lib_dll
  if test -z "$X11$gnuplot$readline" \
          -a "X$which_graphic_lib" != Xgnuplot-dynamic; then
    echo ...none
  fi
fi
####################### CONFIGURE - MAKE ####################################
. ./get_install # $_install_list
. ./get_objdir  # objdir, cdobjdir
. ./get_static  # static
echo "Hi-Res Graphics: $which_graphic_lib"
# For dynamic linking, before and after installing
runpath="$libdir" 
case "$osname" in
  *cygwin*) LDDYN="-L$libdir -lpari_dll"
	    DYNLIBS=-lpari_dll ; DYNFLAGS=-Dlibpari_USE_DLL ;;
  *)        LDDYN="-L$libdir -l$libpari_base" ;;
esac
# Which copy, SHELL ?
case "$osname" in
  os2) ln_s=cp;      make_sh=sh;;
  *)   ln_s="ln -s"; make_sh="/bin/sh";;
esac
####################### CONFIGURE - CLEANUP #################################
rm -f gmon.out # created by Configure -pg

####################### CONFIGURE - SPIT ####################################
# Now spit out the results
cat << EOT
==========================================================================
EOT
cd $TOP
if test ! -d $objdir; then mkdir -p $objdir; fi
dflt_conf_file=$objdir/$dflt_conf_file

cat > $dflt_conf_file << EOT
# Config file for Pari $release -- $pretty

EOT
case "$osname" in
  os2) shell_q='"'; echo "shell_q='\"'"  >> $dflt_conf_file;;
    *) shell_q="'"; echo "shell_q=\"'\"" >> $dflt_conf_file;;
esac

mydlldflags=`echo "$DLLDFLAGS" | \
   sed -e "s/,*-[^ \t-]*[ \t,=]*\\$(LIBPARI_SONAME)//" \
       -e "s/\\$(CFLAGS)/$CFLAGS/"`

__LIBS=$LIBS
if test "$static" = n; then LIBS="$LIBS $LDDYN"; fi
myextradlldflags=`eval echo $EXTRADLLDFLAGS`
LIBS=$__LIBS

myincludedir=`echo "$includedir" | sed -e 's/\/pari\/*$//'`
modules_build="$CC -c -o %s.o $CFLAGS -I$myincludedir %s.c && $DLLD -o %s.$DLSUFFIX $mydlldflags %s.o $myextradlldflags"

for variable in\
  pari_release pari_release_verbose version libpari_base static TOP objdir\
  arch asmarch osname pretty\
  kernlvl0 kernlvl1\
  gnuas AS ASFLAGS KERNELCPPFLAGS LIBS modules_build\
  dir_sep runpath runpathprefix LDDYN DYNLIBS DYNFLAGS DYNRELOC\
  ranlib gzip zcat emacs perl ln_s make_sh\
  sizeof_long doubleformat\
  X11 Xincroot extralib which_graphic_lib graphic_lib_dll gnuplot gnuplot_libs\
  $_install_list\
  $_cc_list\
  $_ld_list\
  $_dlld_list\
  $_readline_list\
  $_gmp_list\
  $_has_list; do
  eval "echo $variable=\'"'$'"$variable\'" \>\> $dflt_conf_file
done

. $config_dir/extract_files

#  Building...
cat << EOT
==========================================================================
EOT

echo $n "Shall we try to build pari $version.$patch ($status) now (y/n)? $c"
dflt=n; rep='y n'; . $config_dir/myread

case $ans in
y) if (cd $objdir; make gp); then
     echo $n "Shall we install the files where they belong (y/n)? $c"
     dflt=n; rep='y n'; . $config_dir/myread
     case $ans in
       y) make install;;
       n) echo "Ok. Type \"${cdobjdir}make install\" when you are ready";;
     esac
   fi;;
n) echo "Ok. Type \"${cdobjdir}make install\" when you are ready";;
esac
echo 'Bye !'
