#!/usr/bin/env bash
pushd ~/git/NOFORK/emacs
./autogen.sh || exit 125
./configure || exit 125
make clean || exit 125
find -name '*.elc' -exec rm {} \; || exit 125
rm lisp/loaddefs.el || exit 125
make -j8 || exit 125
commit=$(git rev-parse HEAD)
popd
fn=emacs-racket-mode-$(date +%s)-${commit}.png
pushd ~/files/emacs-mprof
mprof run ~/git/NOFORK/emacs/src/emacs -Q -batch -l emacs-memory-leak-test.el 
mprof plot --backend TkAgg --title "emacs racket-mode ${commit}" --output ${fn}
unlink latest.png
ln -s ${fn} latest.png
feh ${fn}
popd
pushd ~/git/NOFORK/emacs
git stash
popd
echo y means leak; read -r -n 1 choice
if [[ "${choice}" == "y" ]]; then
exit 1; # yes leak means git bisect bad
else
exit 0;
fi
