# Copyright 1999-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/elisp.eclass,v 1.5 2003/05/23 06:06:19 mkennedy Exp $
#
# Author Matthew Kennedy <mkennedy@gentoo.org>
#
# This eclass sets the site-lisp directory for emacs-related packages.

ECLASS=elisp
INHERITED="$INHERITED $ECLASS"

SITELISP=/usr/share/emacs/site-lisp

elisp-install() {
	local subdir=$1
	dodir ${SITELISP}/${subdir}
	insinto ${SITELISP}/${subdir}
	shift
	doins $@
}

elisp-site-file-install() {
	local sitefile=$1
	pushd ${S}
	cp ${sitefile} .
	D=${S}/ dosed "s:@SITELISP@:${SITELISP}/${PN}:g" $(basename ${sitefile})
	insinto ${SITELISP}
	doins ${S}/$(basename ${sitefile})
	popd
}

elisp-site-regen() {
	einfo "Regenerating ${SITELISP}/site-start.el..."
	einfo ""
	cat <<EOF >${SITELISP}/site-start.el
;;; DO NOT EDIT THIS FILE -- IT IS GENERATED AUTOMATICALLY BY PORTAGE
;;; -----------------------------------------------------------------

EOF
	ls ${SITELISP}/[0-9][0-9]* |sort -n |grep -vE '~$' | \
	while read sf 
	do
		einfo "  Adding $sf..."  
		# Great for debugging, too noisy and slow for users though
# 		echo "(message \"Loading $sf...\")" >>${SITELISP}/site-start.el
		cat $sf >>${SITELISP}/site-start.el
	done
	einfo ""
}

# DEFAULT OVERRIDES

# SRC_URI should be set to wherever the primary app-emacs/ maintainer
# keeps the local elisp mirror, since most app-emacs packages are
# upstream as a single .el file

SRC_URI="http://cvs.gentoo.org/~mkennedy/app-emacs/${P}.el.bz2"
S="${WORKDIR}/"
DEPEND="virtual/emacs"
IUSE=""

src_unpack() {
	unpack ${A}
	if [ "${SIMPLE_ELISP}" = 't' ]
	then
		cd ${S} && mv ${P}.el ${PN}.el
	fi 
}

src_compile() {
	emacs --batch -f batch-byte-compile --no-site-file --no-init-file *.el || die
}

src_install() {
	elisp-install ${PN} *.el *.elc
	elisp-site-file-install ${FILESDIR}/${SITEFILE}
}

pkg_postinst() {
	elisp-site-regen
}

pkg_postrm() {
	elisp-site-regen
}