cd src

# QEPCAD needs these environment variables to be set
export saclib="$SAGE_LOCAL/lib/saclib"
export qe=$(pwd -P)

# * Override SHELL: use /bin/sh instead of /bin/csh, see
#   https://github.com/sagemath/sage/issues/10224
# * Add rpath to compiler flags, see
#   https://github.com/sagemath/sage/issues/22653
# * Use ARFLAGS that also work on macOS, avoiding the U option, see
#   https://github.com/sagemath/sage/issues/28388
LIBS=-lreadline
if [ "$UNAME" = "Linux" ]; then
   LIBS="$LIBS -lrt"
fi
$MAKE -j1 opt SHELL=/bin/sh FLAGSo="${CXXFLAGS} -Wl,-rpath,$SAGE_LOCAL/lib" ARFLAGS="crv" LIBS="$LIBS"
if [ $? -ne 0 ]; then
   echo >&2 "Error building qepcad."
   exit 1
fi


# install binaries to the Sage tree
find . -name *.a -exec cp {} "$SAGE_LOCAL/lib/" \;
cp source/qepcad "$SAGE_LOCAL/bin/"
mkdir -p "${SAGE_LOCAL}/share/qepcad"
cp bin/qepcad.help "$SAGE_LOCAL/share/qepcad"
chmod 0644 "$SAGE_LOCAL/share/qepcad/qepcad.help"
cp ../sage-qepcad "$SAGE_LOCAL/bin/"

cat > "$SAGE_LOCAL/etc/default.qepcadrc" <<EOF
# THIS FILE IS AUTOMATICALLY GENERATED by build/pkgs/qepcad/spkg-install.in -- DO NOT EDIT

#####################################################
# QEPCAD rc file.
# This file allows for some customization of QEPCAD.
# Right now, the ability to give a path to Singular,
# so that it gets used for some computer algebra
# computations is the only feature.
#####################################################
SINGULAR yes
EOF
