#! /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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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$

TOP=`pwd`
config_dir=config
data_dir=data
doc_dir=doc
examples_dir=examples
misc_dir=misc
src_dir=src
desc_dir=src/desc

case "x$RUNTEST" in x);; x/*);; *) RUNTEST=$TOP/$RUNTEST;; esac

cd $config_dir
# Process options, initialize
. ./get_head # do we need head -n # or head -# ?
. ./version
echo "Configuring pari-$pari_release_verbose $patchlevel_verbose"
. ./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'
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
####################### CONFIGURE - COMPILATION #############################
# $_cc_list (includes 'optimization'), extraflag
. ./get_cc
# doubleformat, sizeof_long
. ./get_double_format
# asmarch, pretty
. ./get_kernel
# _dl_list, DLCFLAGS, update CFLAGS
. ./get_dlcflags
# $_ld_list
. ./get_ld
# $_dlld_list
. ./get_dlld
# $_perl_list
. ./get_perl
####################### CONFIGURE - LIBC ####################################
. ./get_libc # $_has_list, DL_LIBS
####################### CONFIGURE - LIBRARIES ###############################
# Looking for libraries: gmp, X11, fltk, Qt, readline
echo Checking for optional libraries and headers...
. ./get_include_path
. ./get_libpth
# $_gmp_list
. ./get_gmp
# $_graphic_list
. ./get_graphic_lib
# $_readline_list (includes 'readline')
. ./get_readline
#############################################################################
case $kernlvl1 in
gmp) libpari_base=pari-gmp;;
none) libpari_base=pari;;
esac
case $enable_tls in
yes) libpari_base="${libpari_base}-tls"
esac

if test `expr $VersionMinor % 2` = 0; then
  libpari_base=$libpari_base-$version
fi

####################### CONFIGURE - MAKE ####################################
. ./get_install # $_install_list
. ./get_objdir  # objdir, cdobjdir
. ./get_static  # static
# For dynamic linking, before and after installing
runpath=\"$libdir\"
LDDYN="-lpari"
# get_modld needs $includedir from get_install, static, and LDDYN
. ./get_modld   # $_modld_list

# 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 ####################################
. ./get_tests #_test_list
# 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|mingw) shell_q='"'; echo "shell_q='\"'"  >> $dflt_conf_file;;
    *) shell_q="'"; echo "shell_q=\"'\"" >> $dflt_conf_file;;
esac

for variable in\
  pari_release pari_release_verbose version libpari_base static TOP objdir\
  arch asmarch osname pretty\
  kernlvl0 kernlvl1 DL_LIBS LIBS\
  dir_sep runpath runpathprefix LDDYN RUNTEST\
  ranlib gzip zcat perl ln_s make_sh\
  sizeof_long doubleformat\
  enable_tls\
  $_test_list\
  $_install_list\
  $_perl_list\
  $_cc_list\
  $_ld_list\
  $_dl_list\
  $_dlld_list\
  $_graphic_list\
  $_modld_list\
  $_readline_list\
  $_gmp_list\
  $_has_list; do
  eval "echo $variable=\'"'$'"$variable\'" \>\> $dflt_conf_file
done

. $config_dir/extract_files

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

if test -n "$tune"; then
  echo "Building and tuning PARI (this may take a while)"
  echo
  (cd $objdir; rm -f parilvl1.h pariinl.h;\
  make tune; tune -t > tune.h.new && mv tune.h.new tune.h;\
  rm -f parilvl1.h pariinl.h; make gp)
else
  echo $n "Shall we try to build pari $version.$patch ($status) now (y/n)? $c"
  dflt=n; rep='y n'; . $config_dir/myread
fi

mkobjdir=`$config_dir/objdir`
cdobjdir=
if test "$objdir" != "$mkobjdir"; then
  cdobjdir="cd $objdir; "
fi

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 !'
