# Move over the GNU makefile to the file 'Makefile'.
mv -f src/GNUmakefile src/Makefile

cd src
mv Global.h Global.h-template

CFLAGS="-O3 -W -Wall $CFLAGS -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"
# On Cygwin the stack size is ridiculously low by default, so that the nef
# executable which allocates everything on the stack for speed reason blows it
if [ "$UNAME" = "CYGWIN" ] ; then
  CFLAGS="$CFLAGS -Wl,--stack,8000000"
fi

BIN="$SAGE_LOCAL/bin"

for dim in 4 5 6 11; do
    echo Building PALP optimized for $dim dimensions

    sed "s/^#define[^a-zA-Z]*POLY_Dmax.*/#define POLY_Dmax $dim/" Global.h-template > Global.h

    sdh_make CC="$CC" CFLAGS="$CFLAGS"
    for file in poly class cws nef mori; do
        sdh_install -T "${file}.x" "${BIN}/${file}-${dim}d.x"
    done

    # the next step is important to avert races on older file systems
    # for example, ext3 has 1-second timestamp granularity!
    sdh_make cleanall
done

# symlinks for the default dimension
cd "${SAGE_DESTDIR}${BIN}"
for file in poly class cws nef mori; do
    ln -sf ${file}-6d.x ${file}.x
done
