#! /bin/sh -e

FLAVOR=$1
PACKAGE=edb

if [ ${FLAVOR} = emacs ]; then exit 0; fi

echo install/${PACKAGE}: Handling install of emacsen flavor ${FLAVOR}

ELDIR=/usr/share/emacs/site-lisp/${PACKAGE}
ELCDIR=/usr/share/${FLAVOR}/site-lisp/${PACKAGE}

case ${FLAVOR} in
    xemacs*)
        echo install/${PACKAGE}: Ignoring emacsen flavor ${FLAVOR};;
    *)
        echo -n install/${PACKAGE}: Byte-compiling for ${FLAVOR} ...
        install -m 755 -d ${ELCDIR}
	rm -f ${ELDIR}/BYTE-COMPILE.el ${ELCDIR}/*.el ${ELCDIR}/*.elc
        (
            cd ${ELDIR}
            echo '(setq load-path (cons "." load-path))' > BYTE-COMPILE.el 
            echo '(setq edb-directory ".")' >> BYTE-COMPILE.el 
            ${FLAVOR} -batch -q -no-site-file \
                -l ./BYTE-COMPILE.el -l database.el \
                -f byte-compile-database-all \
                -f batch-byte-compile database.el
            ${FLAVOR} -batch -q -no-site-file -f batch-byte-compile edb-fixes-1.21.el

            rm -f BYTE-COMPILE.el
            mv -f *.elc ${ELCDIR}
	    for f in *.el; do
		ln -sf ${ELDIR}/$f ${ELCDIR}
	    done
        )
        echo " done.";;
esac

exit 0
