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

set -e

FLAVOR=$1
PACKAGE="wl"

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

PIXMAPDIR=/usr/share/${FLAVOR}/etc/wl

EFLAGS="-batch -q"
COMPILE="-batch -q -f batch-byte-compile"

case "${FLAVOR}" in

    emacs|emacs19)

	;;

    *)

	if [ -e /usr/lib/emacsen-common/packages/install/wemi ]; then
	    if [ ! -d ${SITELISP}/semi ] ; then
		/usr/lib/emacsen-common/packages/install/wemi ${FLAVOR}
	    fi
	fi

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

	if [ ! -d /usr/share/emacs/site-lisp/semi ]; then
	    echo " exited."
	    echo "W: Please install \`semi' package for ${FLAVOR}." ;
	    exit 0;
	fi

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

	# Byte compile
	(
	    cd ${ELDIR}
	    make EMACS=${FLAVOR} FLAGS="${EFLAGS}" LISPDIR=${SITELISP} \
		> ${ELCDIR}/CompilationLog 2>&1

	    case "${FLAVOR}" in
		xemacs-*|emacs-21*)
		    make EMACS=${FLAVOR} FLAGS="${EFLAGS}" LISPDIR=${SITELISP} \
			PIXMAPDIR=${PIXMAPDIR} install \
			>> ${ELCDIR}/CompilationLog 2>&1
		;;
		*)
		    make EMACS=${FLAVOR} FLAGS="${EFLAGS}" LISPDIR=${SITELISP} \
			PIXMAPDIR=${PIXMAPDIR} \
			install  >> ${ELCDIR}/CompilationLog 2>&1
		;;
	    esac

	    make EMACS=${FLAVOR} FLAGS="${EFLAGS}" LISPDIR=${SITELISP} clean \
		>> ${ELCDIR}/CompilationLog 2>&1

	    rm -f ${ELCDIR}/*.el
	    #cp ${STARTFILE} ${ELCDIR}
	    rm -f *.elc elmo/*.elc

	    rm -f vine-default-${PACKAGE}.elc
	    install -p -m644 ${ELDIR}/vine-default-${PACKAGE}.el ${ELCDIR}
	)

	gzip -9 ${ELCDIR}/CompilationLog

	ln -sf ${ELDIR}/${STARTFILE} ${STARTDIR}/55${STARTFILE};
	echo " done."

	;;

esac

exit 0;
