#! /bin/bash

if [ "$1" != "" ];
then
echo "**ERROR**: Configure does not take any options for SYMPOW"; exit;
fi

ADDBINPATH=${ADDBINPATH:-"yes"}

PREFIX=${PREFIX:-"/usr/local"}
VARPREFIX=${VARPREFIX:-"/var"}

FILE="Makefile.new"
CONFIG="config.h"
RM=`which \rm`
if [ -z "$RM" ];
then
echo "**ERROR**: Could not find rm"; exit;
fi
$RM -f $FILE
$RM -f $CONFIG
VERSION=2.023.5
echo "#define PREFIX \"$PREFIX\"" >> $CONFIG
echo "#define VARPREFIX \"$VARPREFIX\"" >> $CONFIG
echo "#define VERSION \"$VERSION\"" >> $CONFIG
echo "VERSION = $VERSION" >> $FILE

GREP=`which \grep`
if [ -z "$GREP" ];
then
echo "*WARNING*: Could not find grep --- will not be able to build new_data"
fi

GP=`which \gp`
if [ -z "$GP" ];
then
echo "*WARNING*: Could not find gp --- will not be able to build new_data"
fi

SED=`which \sed` && echo "SED = $SED" >> $FILE
if [ -z "$SED" ];
then
echo "*WARNING*: Could not find sed --- will not be able to build new_data"
fi

if [ "x$ADDBINPATH" = "xyes" ]; then
echo "#define RM \"$RM\"" >> $CONFIG
[ -n "$GREP" ] && echo "#define GREP \"$GREP\"" >> $CONFIG
[ -n "$GP" ] && echo "#define GP \"$GP\"" >> $CONFIG
[ -n "$SED" ] && echo "#define SED \"$SED\"" >> $CONFIG
else
echo "#define RM \"rm\"" >> $CONFIG
[ -n "$GREP" ] && echo "#define GREP \"grep\"" >> $CONFIG
[ -n "$GP" ] && echo "#define GP \"gp\"" >> $CONFIG
[ -n "$SED" ] && echo "#define SED \"sed\"" >> $CONFIG
fi

[ -n "$GREP" ] && [ -n "$GP" ] && [ -n "$SED" ]\
 && echo "#define NEW_DATA" >> $CONFIG

SH=/bin/sh
echo "#define SH \"$SH\"" >> $CONFIG
echo "SH = $SH" >> $FILE

AUTOHEADER=`which \autoheader`
if [ -z "$AUTOHEADER" ];
then
echo "**ERROR**: Could not find autoheader"; exit;
fi

AUTOCONF=`which \autoconf`
if [ -z "$AUTOCONF" ];
then
echo "**ERROR**: Could not find autoconf"; exit;
fi

CC=${CC:-gcc} && echo "CC = $CC" >> $FILE
##if [ -z "$CC" ];
##then
##echo "**ERROR**: Could not find cc"; exit;
##fi
export CC

##UNAME=`which \uname`
##if [ -z "$UNAME" ];
##then
##echo "**ERROR**: Could not find uname"; exit;
##fi

HELP2MAN=`which \help2man` && echo "HELP2MAN = $HELP2MAN" >> $FILE
if [ -z "$HELP2MAN" ];
then
echo "**ERROR**: Could not find help2man"; exit;
fi

## Sage material (spkg-install)
#######################################################################
# Fix FPU precision
#######################################################################
# SYMPOW really needs doubles to have *exactly* 53 bits precision,
# they need to be true IEEE-754 double precision numbers.
# In particular, we need to avoid 80 bits extended precision on i386
# and we need to avoid fused multiply-add instructions (e.g. on ia64).
# We might need to add various flags to CFLAGS to ensure this.
# See Trac tickets #9703, #9734, #9166, #11226, #11920.

ORIGINALCFLAGS=${CFLAGS}
CFLAGS=''

# Usage: try_add_CFLAG $FLAG
# Try adding $FLAG to $CFLAGS, compile and run the fpubits program.
# If it compiles and running it doesn't crash it (with an Illegal
# Instruction for example), then we add $FLAG to $CFLAGS.
# Return 0 if we added $FLAG and the FPU correctly used 53 bits,
# return 1 if we added $FLAG but the FPU doesn't use 53 bits,
# return 2 if we did not add $FLAG.
try_add_CFLAG()
{
    # We use -O3 here to really force generation of fused
    # multiply-add instructions and to keep floats as much as
    # possible in registers.
    # We compile in the fpu.c which only does
    # something if the macro x86 is defined.
    local flag=$1                      # first argument:  C FLAG to test
    local bypassfputest=${2:-nobypass} # second argument: whether or not bypass the numerical test (default: not)
    local status=0
    if $CC $ORIGINALCFLAGS -Werror $CFLAGS -O3 $flag config/fpubits1.c config/fpubits2.c fpu.c -o config/fpubits 2>/dev/null; then
        # Compiled successfully, now run it
        config/fpubits >/dev/null 2>/dev/null
        status=$?
        if [ $status -le 1 ]; then
            # The program ran successfully.  For now, we don't need
            # the exit status to be zero (indicating exactly 53 bits),
            # we simply need the program not to crash (which would
            # give an exit status > 128).
            CFLAGS="$CFLAGS $flag"
            if [ "x$bypassfputest" = "xnobypass" ]; then
                return $status
            else
                return 0
            fi
        fi
    fi
    return 2
}

# Let move to 21st century and add GNU features (asprintf)
try_add_CFLAG '-std=gnu17' bypass
if [ $? -ne 0 ]; then
    try_add_CFLAG '-std=gnu11' bypass
    if [ $? -ne 0 ]; then
      echo >&2 "Error: moving to 21st century and adding GNU feature are denied"
      exit 1
    fi
fi

# These flags never hurt, so add them if possible
for FLAG in '-fno-fast-math' '-mfpmath=sse'; do
    try_add_CFLAG $FLAG bypass
done

# Add at most one flag of the following to avoid gcc warnings:
# gcc versions which support -ffp-contract deprecate -mno-fused-madd
for FLAG in '-ffp-contract=on' '-mno-fused-madd'; do
    try_add_CFLAG $FLAG bypass && break
done

# Select the most appropriate FPU control scheme
for FLAG in '-DISOC99_FENV' '-DFPUCONTROLH' '-Dx86'; do
    try_add_CFLAG $FLAG && break
done

# Some flags to try as last resort.  These hurt performance, so only add
# them if needed.
for FLAG in '' '-ffloat-store' '-O0'; do
    # Stop the loop if the FPU precision already is 53 bits
    try_add_CFLAG $FLAG && break
done

# Check the actual FPU precision with our new flags.
CC_ARGS="$ORIGINALCFLAGS -O3 $CFLAGS config/fpubits1.c config/fpubits2.c fpu.c -o config/fpubits"
$CC $CC_ARGS
if [ $? -ne 0 ]; then
    echo >&2 "Error: the command below failed:"
    echo >&2 "$CC $CC_ARGS"
    exit 1
fi
export CFLAGS

echo "CFLAGS for SYMPOW: $CFLAGS"

config/fpubits
status=$?
if [ $status -eq 1 ]; then
cat >&2 <<EOF
Error: the Quad Double library used by SYMPOW assumes IEEE-754 double
precision numbers with exactly 53 bits in the mantissa (64 bits in
total).  Unfortunately, this is not the case on your system and we
currently have no workaround for your system.  Running SYMPOW will
almost certainly fail on some inputs.

Please report this problem to sage-devel
(http://groups.google.com/group/sage-devel), mentioning in particular
your operating system, processor type and compiler version
(run $CC --version).
EOF
elif [ $status -ne 0 ]; then
cat >&2 <<EOF
Error: something very bad happened while checking the precision of your
FPU.  Please report this problem (mentioning any error messages above)
to sage-devel (http://groups.google.com/group/sage-devel).
Mention in particular your operating system and compiler version
(run $CC --version).
EOF
exit 1
fi

##

## inspired from fontconfig material (configure.ac and fcarch.h)
#######################################################################
# Determine architecture endianess-tuple
#######################################################################
( \
cd config && \
$AUTOHEADER endiantuple.ac && $AUTOCONF -o endiantuple_ac.sh endiantuple.ac && \
$SH endiantuple_ac.sh > endiantuple_ac.log )
if [ $? -ne 0 -o ! -f config/endiantuple.c ]; then
    echo >&2 "Error: failed to generate config/endiantuple.c"
    exit 1
fi

$CC -o config/endiantuple config/endiantuple.c
if [ $? -ne 0 ]; then
    echo >&2 "Error: the command below failed:"
    echo >&2 "$CC -o config/endiantuple config/endiantuple.c"
    exit 1
fi
ENDIANTUPLE=$(config/endiantuple) && echo "#define ENDIANTUPLE \"$ENDIANTUPLE\"" >> $CONFIG
if [ -z "$ENDIANTUPLE" ]; then
    echo >&2 "Error: could not determine the endian tuple"
		exit 1
fi

echo "ENDIANTUPLE for SYMPOW: $ENDIANTUPLE"

echo "CONFETA = config/endiantuple_ac.sh config/endiantuple.c.in" >> $FILE
echo "CONFETC = config/endiantuple.c" >> $FILE
echo "CONFEXE = config/endiantuple config/fpubits" >> $FILE
echo "CONFO = \$(CONFETA) \$(CONFETC) \$(CONFEXE)" >> $FILE

##

##MACH=`"$UNAME" -m`
##for x in ix86 i386 i486 i586 i686 i386pc x86_64 ia64
##do
##if [ "$MACH" = "$x" ];
##then
##if [ "$x" = "x86_64" ] || [ "$x" = "ia64" ]
##then
##echo "You appear to have a $x based system --- linking fpu.s to fpu64.s"
##$RM -f fpu.s
##LN=ln
##$LN -s fpu64.s fpu.s
##DEFS="-Dx86 -m64"
##else
##echo "You appear to have a $x based system --- linking fpu.s to fpu32.s"
##$RM -f fpu.s
##LN=ln
##$LN -s fpu32.s fpu.s
##DEFS="-Dx86 -m32"
##fi
##fi
##done
##if [ -z "$DEFS" ];
##then
##echo "You do not appear to have an x86 based system --- not using fpu.s"
##$RM -f fpu.s
##$TOUCH fpu.s
##fi
##
##echo "DEFS = $DEFS" >> $FILE

OPT="-O3 ${CFLAGS}" && echo "OPT = $OPT" >> $FILE

echo "H2MFLAGS = \\" >> $FILE
echo "	--manual=\"SYMPOW package\" \\" >> $FILE
echo "	--source=\"SYMPOW (\$(VERSION))\" \\" >> $FILE
echo "	--help-option=-help \\" >> $FILE
echo "	--version-option=-dump-versiontuple \\" >> $FILE
echo "	--no-info" >> $FILE

echo "SRCS0 = fpu.c" >> $FILE
echo "SRCS1 = analrank.c analytic.c compute.c compute2.c help.c" >> $FILE
echo "SRCS2 = conductors.c disk.c ec_ap.c ec_ap_bsgs.c ec_ap_large.c" >> $FILE
echo "SRCS3 = eulerfactors.c factor.c generate.c init_curve.c main.c" >> $FILE
echo "SRCS4 = moddeg.c periods.c prepare.c QD.c rootno.c util.c" >> $FILE
echo "SRCS = \$(SRCS0) \$(SRCS1) \$(SRCS2) \$(SRCS3) \$(SRCS4)" >> $FILE
echo "OBJS = \$(SRCS:.c=.o)" >> $FILE
##echo "OBJSf = \$(OBJS) fpu.o" >> $FILE
echo "OTHER = new_data COPYING README Configure fpu32.s fpu64.s"  >> $FILE
echo "OTHERgp = standard1.gp standard2.gp standard3.gp" >> $FILE
echo "OTHERb = armd.gp ianalrank.gp ihmd.gp imd.gp armd.sh" >> $FILE
echo "OTHERS = \$(OTHER) \$(OTHERgp) \$(OTHERb)" >> $FILE
echo "HEADERS = sympow.h" >> $FILE
echo "SRC = \$(SRCS) \$(HEADERS) \$(OTHERS)" >> $FILE
echo "TILDES = *~ datafiles/*~" >> $FILE
df="datafiles"
echo "DATAFILES = $df/*M.txt $df/*S.txt $df/param_data" >> $FILE

echo "RM = $RM" >> $FILE
CP=`which \cp` && echo "CP = $CP" >> $FILE
if [ -z "$CP" ];
then
echo "**ERROR**: Could not find cp"; exit;
fi
MKDIR=`which \mkdir` && echo "MKDIR = $MKDIR" >> $FILE
if [ -z "$MKDIR" ];
then
echo "**ERROR**: Could not find mkdir"; exit;
fi
TOUCH=`which \touch` && echo "TOUCH = $TOUCH" >> $FILE
if [ -z "$TOUCH" ];
then
echo "**ERROR**: Could not find touch"; exit;
fi
####TAR=`which \tar` && echo "TAR = $TAR" >> $FILE
####if [ -z "$TAR" ];
####then
####echo "*WARNING*: Could not find tar --- source/archive omitted from Makefile";
####else
####echo "TARS = sympow.tar sympow.src.tar" >> $FILE
####echo "WDIR = SYMPOW-\$(VERSION)" >> $FILE && echo "" >> $FILE
####fi

echo "default: build-arch" >> $FILE
echo "all: build-arch build-indep" >> $FILE
echo "build-arch: sympow sympow.1" >> $FILE
echo "build-indep: datafiles/param_data" >> $FILE
echo "sympow: \$(OBJS)" >> $FILE
echo "	\$(CC) \$(CPPFLAGS) \$(CFLAGS) \$(OPT) -o \$@ \$(DEFS) \$(OBJS) \$(LDFLAGS) \$(LIBS)" >> $FILE
##echo "fpu.o : fpu.s Makefile" >> $FILE
##echo "	\$(CC) \$(OPT) -c fpu.s" >> $FILE
echo "%.o : %.c \$(HEADERS) Makefile" >> $FILE
echo "	\$(CC) \$(CPPFLAGS) \$(CFLAGS) \$(OPT) \$(DEFS) -c -o \$@ \$<" >> $FILE
echo "datafiles/param_data: \$(OTHERb)" >> $FILE
echo "	\$(MKDIR) -p datafiles" >> $FILE
echo "	\$(TOUCH) datafiles/param_data" >> $FILE
echo "	\$(SH) armd.sh" >> $FILE
echo "	\$(SED) -i -e '/logfile =/d' datafiles/*.txt" >> $FILE
echo "sympow.1: sympow" >> $FILE
echo "	\$(HELP2MAN) \$(H2MFLAGS) -s 1 -n \"SYMPOW program\" -I sympow.h2m -o \$@ ./\$<" >> $FILE
echo "clean:" >> $FILE
##echo "	\$(RM) -f \$(OBJSf) sympow \$(TILDES) \$(TARS)" >> $FILE
echo "	\$(RM) -f \$(OBJS) sympow sympow.1 \$(TILDES)" >> $FILE
echo "	\$(RM) -rf datafiles" >> $FILE
echo "distclean: clean" >> $FILE
echo "	\$(RM) -rf config/autom4te.cache" >> $FILE
echo "	\$(RM) -f config/config.status config/*.log" >> $FILE
echo "	\$(RM) -f \$(CONFO) config/*.log" >> $FILE
echo "	\$(RM) -f config.h Makefile" >> $FILE
echo "install-arch: build-arch" >> $FILE
echo "	install -d \$(DESTDIR)$PREFIX/bin" >> $FILE
echo "	install -d \$(DESTDIR)$PREFIX/share/man/man1" >> $FILE
echo "	install -m 0755 sympow \$(DESTDIR)$PREFIX/bin" >> $FILE
echo "	install -m 0644 sympow.1 \$(DESTDIR)$PREFIX/share/man/man1" >> $FILE
echo "install-indep: build-indep" >> $FILE
echo "	install -d \$(DESTDIR)$PREFIX/lib/sympow" >> $FILE
echo "	install -d \$(DESTDIR)$PREFIX/share/sympow" >> $FILE
echo "	install -d \$(DESTDIR)$PREFIX/share/sympow/datafiles" >> $FILE
echo "	install -m 0755 new_data \$(DESTDIR)$PREFIX/lib/sympow" >> $FILE
echo "	install -m 0644 standard1.gp standard2.gp standard3.gp \$(DESTDIR)$PREFIX/share/sympow" >> $FILE
echo "	install -m 0644 datafiles/*.txt datafiles/param_data \$(DESTDIR)$PREFIX/share/sympow/datafiles" >> $FILE
echo "install: install-arch install-indep" >> $FILE
####if [ -n "$TAR" ];
####then
####echo "source:" >> $FILE
####echo "	\$(MKDIR) \$(WDIR)" >> $FILE
####echo "	\$(CP) -f \$(SRC) \$(WDIR)" >> $FILE
####echo "	\$(TAR) cf sympow.src.tar \$(WDIR)/*" >> $FILE
####echo "	\$(RM) -rf \$(WDIR)" >> $FILE
####echo "archive:" >> $FILE
####echo "	\$(MKDIR) \$(WDIR)" >> $FILE
####echo "	\$(CP) -f \$(SRC) \$(WDIR)" >> $FILE
####echo "	\$(MKDIR) \$(WDIR)/datafiles" >> $FILE
####echo "	\$(CP) -f \$(DATAFILES) \$(WDIR)/datafiles" >> $FILE
####echo "	\$(TAR) cf sympow.tar \$(WDIR)/*" >> $FILE
####echo "	\$(RM) -rf \$(WDIR)" >> $FILE
####fi

$RM -f Makefile
$CP -f Makefile.new Makefile
$RM -f Makefile.new

echo "Makefile has been re-made. Use make if you wish to build SYMPOW"
echo ""
echo "**ATTENTION** If you wish build SYMPOW, please ensure beforehand"
echo "that the various licenses of your C compiler, linker, assembler, etc."
echo "allow you to create a derived work based on SYMPOW and your C libraries"
