#!/bin/sh
#
# BFI configure-like script to bootstrap ATLAS's C-based config scripts
# dependencies: sed, pwd
# shell built-in deps: echo, test
#
cc=gcc
cflags="-g -w"
prefix=/usr/local/atlas
incinstdir=ATL_NoOverride
libinstdir=ATL_NoOverride
flapack=ATL_NoOverride
f77=1
#
# path is configure path without trailing configure :)
#
path=`echo $0 | sed -e "s/configure$//"`
#
# blddir is present directory
#
blddir=`pwd`
#
# If on cygwin, prefix blddir with /cygdrive/[c,d]/
#
if [ -d /cygdrive ]
then
   if [ -d /cygdrive/c/cygwin/$blddir ]
   then
        blddir=/cygdrive/c/cygwin/$blddir
   else
      if [ -d /cygdrive/d/cygwin/$blddir ]
      then
        blddir=/cygdrive/d/cygwin/$blddir
      fi
   fi
fi
#
# topdir is simply path if the line does not begin with "../" and is
#  blddir/path otherwise
chk=`echo "$path" | sed -e "s/^\.\.\///"`
if test "$chk" = "$path"
then
   topdir="$path"
else
   topdir="$blddir"/"$path"
fi
#
# Error out if topdir == blddir
#
touch $blddir/TstBld973.txt
if [ -f "$topdir/TstBld973.txt" ]
then
   echo "ATLAS can no longer be  configured in the exact source directory,"
   echo "create a subdir such as MyObj.  See ATLAS/INSTALL.txt for help."
   rm $blddir/TstBld973.txt
   exit 1
fi
rm $blddir/TstBld973.txt

#
# grab --cc=<c comp> and --cflags=<flags> to be used to build ATLAS's ANSI C
# config.c, and save the rest of the arguments to by passed to the 
# soon-to-be-built xconfig
#
#echo other args=$*
bitwidth=0
rdbitwidth=0
for arg in "$@"
do
   handled=0
   flag=`echo "$arg" | sed -e "s/--help//"`
   if test "$flag" != "$arg"
   then
      echo "ATLAS config includes this script, and probes written in C."
      echo "Therefore, configure flags are union of script and probe flags."
      echo "This configure script accepts the following flags:"
      echo "   --cc=<C compiler> : compiler to compile configure probes"
      echo "   --cflags='<flags>' : flags for above"
      echo "   --prefix=<dirname> : Toplevel installation directory."
      echo "                        Default: /usr/local/atlas"
      echo "   --incdir=<dirname> : Installation dir for include files"
      echo "                        Default: $prefix/include"
      echo "   --libdir=<dirname> : Installation dir for libraries"
      echo "                        Default: $prefix/lib"
      echo "   --nof77 : You have no Fortran compiler installed.  Note that"
      echo "             this will disallow building the F77 interface, and"
      echo "             some of the tests (eg, standard BLAS testers)"
      echo "  --with-netlib-lapack=<path to lapack.a>"
      echo "     Use this library to build a mixed netlib/ATLAS LAPACK."
      echo "     The two libs will be merged so they must be built wt compatible compilers."
      echo "Attempting to build xconfig to get probe flags:"
      rm -f Makefile > /dev/null 2>&1
      echo BLDdir="$blddir" > Makefile
      echo TOPdir="$topdir" >> Makefile
      echo CC="$cc" >> Makefile
      echo CFLAGS="$cflags" >> Makefile
      cat "$path"/CONFIG/src/Makefile >> Makefile
      make xconfig > /dev/null 2>&1
      ./xconfig --help
      rm -f Makefile xconfig atlconf_misc.o 2>&1
      exit 1
   fi
#
# Look for config flag -b <bidwidth>, if we find it set bitwidth to read next
# args val, but don't set handled, since flag must be passed to config as well
#
   if test $rdbitwidth -eq 1
   then
      rdbitwidth=0
      if test "$arg" -eq "64"
      then
         bitwidth=64
      fi
      if test "$arg" -eq "32"
      then
         bitwidth=32
      fi
   fi
   if test "$arg" = "-b"
   then
      rdbitwidth=1
   fi
#
   flag=`echo "$arg" | sed -e "s/--prefix=//"`
   if test "$flag" != "$arg"
   then
      prefix="$flag"
      handled=1
   fi
#
   flag=`echo "$arg" | sed -e "s/--libdir=//"`
   if test "$flag" != "$arg"
   then
      libinstdir="$flag"
      handled=1
   fi
#
   flag=`echo "$arg" | sed -e "s/--incdir=//"`
   if test "$flag" != "$arg"
   then
      incinstdir="$flag"
      handled=1
   fi
#
   flag=`echo "$arg" | sed -e "s/--with-netlib-lapack=//"`
   if test "$flag" != "$arg"
   then
      flapack="$flag"
      pass="$pass -Ss flapack $flapack"
      handled=1
   fi
#
   flag=`echo "$arg" | sed -e "s/--nof77//"`
   if test "$flag" != "$arg"
   then
      f77=0
      pass="$pass -Si nof77 1"
      handled=1
   fi
#
   flag=`echo "$arg" | sed -e "s/--cc=//"`
   if test "$flag" != "$arg"
   then
      cc="$flag"
       handled=1
   else
      flag=`echo "$arg" | sed -e "s/--cflags=//"`
      if test "$flag" != "$arg"
      then
         handled=1
         cflags="$flag"
      fi
   fi
   if test $handled -eq 0
   then
#
#     Get rid of leading and trailing spaces
#
      arg=`echo "$arg" | sed -e "s/^ +//"`
      arg=`echo "$arg" | sed -e "s/ +$//"`
# 
#     If flag have internal spaces, surround it with ''
#
      flag=`echo "$arg" | sed -e "s/ //"`
      if (test "$flag" != "$arg")
      then
         pass="$pass '`echo "$arg"`'"
      else
         pass="$pass $arg"
      fi
   fi
done
OSGUESS=`uname`
flag=`echo "$OSGUESS" | sed -e "s/AIX//"`
#
# Need to do some special crap for AIX installs
if test "$flag" != "$OSGUESS"
then
   if test $bitwidth -eq 64
   then
      export OBJECT_MODE=64
      if test $cc = "gcc"
      then
         flag=`echo "$cflags" | sed -e "s/-maix//"`
         if test "$flag" = "$cflags"
         then
             cflags="$cflags -maix64"
         fi
      fi
   else
      export OBJECT_MODE=32
   fi
fi
if test $incinstdir = "ATL_NoOverride"
then
   incinstdir='$(DESTDIR)/include'
fi
if test $libinstdir = "ATL_NoOverride"
then
   libinstdir='$(DESTDIR)/lib'
fi
#
# Create the Makefile and copy the compiler info file
#
echo BLDdir="$blddir" > Makefile
echo TOPdir="$topdir" >> Makefile
echo DESTDIR="$prefix" >> Makefile
echo INCINSTdir="$incinstdir" >> Makefile
echo LIBINSTdir="$libinstdir" >> Makefile
echo CC="$cc" >> Makefile
echo CFLAGS="$cflags" >> Makefile
if test $f77 -eq 0
then
   echo TESTS=C_test >> Makefile
   echo PTTESTS=C_pttest >> Makefile
else
   echo TESTS=test >> Makefile
   echo PTTESTS=pttest >> Makefile
fi
cat "$path"/CONFIG/src/Makefile >> Makefile
cp "$path"/CONFIG/src/atlcomp.txt .
#
# Build and run xconfig
#
make -f Makefile xconfig
echo ./xconfig -d s "$topdir" -d b "$blddir" "$pass"
#./xconfig -d s "$topdir" -d b "$blddir" `echo "$pass"`
echo ./xconfig -d s "$topdir" -d b "$blddir" "$pass" | /bin/sh
ierr=$?
if test $ierr -ne 0
then
   echo xconfig exited with $ierr
   exit $ierr
fi
#
# Build subdirectory structure and copy all makefiles
#
make -f Makefile make_subdirs
cp -f "$path"/Make.top .
cp -f "$path"/CONFIG/src/ATLrun.sh bin/.
make -f Makefile startup
if test $flapack != "ATL_NoOverride"
then
   cp -f $flapack lib/liblapack.a
   ar d lib/liblapack.a lsame.o xerbla.o csrot.o zdrot.o
fi
echo "DONE configure"
#
