#! /bin/sh -e
#  /usr/lib/emacsen-common/packages/install/egg
# [ This particular script hasn't been tested, so be careful. ]

set -e

FLAVOR=$1
PACKAGE="egg"

if [ "X${FLAVOR}" = "X" ]; then
    echo Need argument to determin FLAVOR of emacs;
    exit 1
fi

if [ "X${PACKAGE}" = "X" ]; then
    echo Internal error: need package name;
    exit 1;
fi

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

STARTDIR=/etc/${FLAVOR}/site-start.d
STARTFILE="${PACKAGE}-init.el";

SITELISP=/usr/share/${FLAVOR}/site-lisp

EFLAGS="-batch -q --no-site-file --no-init-file -l ./docomp.el -l./jisx0213.el -f batch-byte-compile"
COMPILE="-batch -q -f batch-byte-compile"

case "${FLAVOR}" in
	emacs)
	;;
	emacs20)
	AYNU_BUILD=
	;;
	emacs21|emacs22|emacs23|emacs-snapshot)
	AYNU_BUILD=its/aynu.el
	;;
esac

case "${FLAVOR}" in
	emacs)
	;;
	emacs22|emacs23|emacs-snapshot)

	echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."

	rm -rf ${ELCDIR}
	install -m 755 -d ${ELCDIR}

    # Copy the temp .el files (and Makefile or so)
    # cp -par ${ELDIR}/* ${ELCDIR}/

    # Byte compile them
	(cd ${ELDIR}
	    make AYNU=${AYNU_BUILD} EMACS=${FLAVOR} lispdir=${SITELISP} install clean > CompilationLog 2>&1
	)

	mv ${ELDIR}/CompilationLog ${ELCDIR}/
	gzip -9 ${ELCDIR}/CompilationLog

	if [ ! -d ${STARTDIR} ]; then
	    install -m 755 -d ${STARTDIR}
	fi

	cp ${ELDIR}/${STARTFILE} ${STARTDIR}/50${STARTFILE};

	echo " done."
	;;
	*)
	;;
esac

exit 0;
