if [ "$SAGE_LOCAL" = "" ]; then
   echo >&2 "SAGE_LOCAL undefined ... exiting";
   echo >&2 "Maybe run 'sage -sh'?"
   exit 1
fi


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://trac.sagemath.org/ticket/10224
# * Add rpath to compiler flags, see
#   https://trac.sagemath.org/ticket/22653
$MAKE -j1 opt SHELL=/bin/sh FLAGSo="-O3 -g -Wl,-rpath,$SAGE_LOCAL/lib"
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/"
cp bin/qepcad.help "$SAGE_LOCAL/bin/"
chmod 0644 "$SAGE_LOCAL/bin/qepcad.help"
cp ../sage-qepcad "$SAGE_LOCAL/bin/"
