#!/bin/sh -e

prepare_environ()
{
tar xf download
mv maxima-5.45.1/* ./
rm download
rm -r maxima-5.45.1
}

make_universe()
{
ORIG_DIR=$(pwd)

sbcl --disable-debugger << '(EOF)'
(load "configure.lisp")
(configure :interactive nil)
(quit)
(EOF)

cd src/

sbcl --disable-debugger << '(EOF)'
(load "maxima-build.lisp")
(maxima-compile)
(quit)
(EOF)

sbcl --disable-debugger << '(EOF)'
(load "maxima-build.lisp")
(maxima-load)
(maxima-dump)
(EOF)

cd $ORIG_DIR
}

emacs_check()
{
if [ -x /bin/emacs ]
then
   printf 'Emacs discovered, installing Maxima interface to site-lisp.\n'
   for i in $(find . -regex '.*\.el$')
   do
      install -Dm644 "${i}" "$1/usr/share/emacs/site-lisp/"
   done
fi
}
emacs_check "${@}"

cd 5.45.1
make_universe
cd ..
mkdir -p "$1/usr/lib/maxima/" "$1/usr/bin/"
mv 5.45.1 "$1/usr/lib/maxima/"
install -Dm755 maxima-mid "$1/usr/bin/maxima"

chmod 0755 "$1/usr/lib/maxima/5.45.1/src/maxima"
