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

set -e

FLAVOR=$1
EMACS=${FLAVOR}
PACKAGE="t-gnus"

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

if [ ${FLAVOR} = xemacs21 ];then
       XEMACS=`ls /usr/bin/xemacs*21*-mule* 2> /dev/null | head -1`
       if [ -z $XEMACS ];then exit 0; fi
       EMACS=$XEMACS
fi

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

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

EFLAGS="-batch -q -l ./dgnushack.el -f dgnushack-compile"
COMPILE="-batch -q -f batch-byte-compile"

case "${FLAVOR}" in
	emacs2* | xemacs21 | mule2)

	echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."
	if [ "${FLAVOR}" = "mule2" ]; then
		if [ ! -d /usr/share/emacs/site-lisp/clime ]; then
		echo " exited."
		echo "W: Please install \`clime' package for mule2." ;
		exit 0;
		fi
	else
		if [ ! -d /usr/share/emacs/site-lisp/flim ]; then
		echo " exited."
		echo "W: Please install \`flim' package for ${FLAVOR}." ;
		exit 0;
		fi
	fi

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

    # Copy the temp .el files
    cp ${ELDIR}/* ${ELCDIR}/

    # Byte compile them
    (cd ${ELCDIR}
        ${EMACS} ${EFLAGS} > CompilationLog 2>&1
	rm -f *.el Makefile
    )

	gzip -9 ${ELCDIR}/CompilationLog

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

exit 0;
