# 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"

# Undefine NDEBUG. Palp source has assert statements and preprocessing them
# away changes the syntactical meaning of the program.
CPPFLAGS=$(echo "${CPPFLAGS}" | sed "s/-DNDEBUG//g")
CFLAGS=$(echo "${CFLAGS}" | sed "s/-DNDEBUG//g")

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
