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

set -e

FLAVOR=$1
PACKAGE="color-mate"
VERSION=10.1.1

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

STAMP=${ELCDIR}/compile-stamp

case "${FLAVOR}" in

    emacs-21*|emacs-22*)

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

	if [ -e ${STAMP} ]; then
	    if [ "${VERSION}" = "`cat ${STAMP}`" ]; then
		echo " exited. (already compiled)" 
		exit
	    fi
	fi

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

	cd ${ELDIR}
	rm -f config.*
	./configure --with-skk-cursor2 --with-leim-cursor \
	    --with-emacs=${FLAVOR} --with-install-path=${ELCDIR} \
	    --without-canna-cursor > ${ELCDIR}/CompilationLog 2>&1
	#make clean >> ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${FLAVOR} >> ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${FLAVOR} -C contrib >> ${ELCDIR}/CompilationLog 2>&1
	make INSTALLPATH=${ELCDIR} install >> ${ELCDIR}/CompilationLog 2>&1
	make INSTALLPATH=${ELCDIR} install -C contrib \
	    >> ${ELCDIR}/CompilationLog 2>&1
	cp -av kanakan-cursor/egg-cursor.el ${ELCDIR} \
	    >> ${ELCDIR}/CompilationLog 2>&1
	#make clean >> ${ELCDIR}/CompilationLog 2>&1
	rm -f config.* *.elc {theme,contrib,kanakan-cursor}/*.elc
	rm -f ${ELCDIR}/*.X

	gzip -9 ${ELCDIR}/CompilationLog
	echo ${VERSION} > ${STAMP}

	#cp -a ${ELDIR}/${STARTFILE} ${STARTDIR}/60${STARTFILE};
	echo " done."

	;;

    emacs-20*)

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

	if [ -e ${STAMP} ]; then
	    if [ "${VERSION}" = "`cat ${STAMP}`" ]; then
		echo " exited. (already compiled)" 
		exit
	    fi
	fi

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

	cd ${ELDIR}
	rm -f config.*
	./configure --with-skk-cursor2 --with-leim-cursor \
	    --with-emacs=${FLAVOR} --with-install-path=${ELCDIR} \
	    --with-canna-cursor > ${ELCDIR}/CompilationLog 2>&1
	#make clean >> ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${FLAVOR} >> ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${FLAVOR} -C contrib >> ${ELCDIR}/CompilationLog 2>&1
	make INSTALLPATH=${ELCDIR} install >> ${ELCDIR}/CompilationLog 2>&1
	make INSTALLPATH=${ELCDIR} install -C contrib \
	    >> ${ELCDIR}/CompilationLog 2>&1
	cp -av kanakan-cursor/egg-cursor.el ${ELCDIR} \
	    >> ${ELCDIR}/CompilationLog 2>&1
	#make clean >> ${ELCDIR}/CompilationLog 2>&1
	rm -f config.* *.elc {theme,contrib,kanakan-cursor}/*.elc
	rm -f ${ELCDIR}/*.X

	gzip -9 ${ELCDIR}/CompilationLog
	echo ${VERSION} > ${STAMP}

	#cp -a ${ELDIR}/${STARTFILE} ${STARTDIR}/60${STARTFILE};
	echo " done."

	;;

    xemacs-*)

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

	if [ -e ${STAMP} ]; then
	    if [ "${VERSION}" = "`cat ${STAMP}`" ]; then
		echo " exited. (already compiled)" 
		exit
	    fi
	fi

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

	cd ${ELDIR}
	rm -f config.*
	./configure --with-skk-cursor2 --with-leim-cursor \
	    --with-emacs=${FLAVOR} --with-install-path=${ELCDIR} \
	    --with-canna-cursor --with-egg-cursor \
	    > ${ELCDIR}/CompilationLog 2>&1
	#make clean >> ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${FLAVOR} >> ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${FLAVOR} -C contrib >> ${ELCDIR}/CompilationLog 2>&1
	make INSTALLPATH=${ELCDIR} install >> ${ELCDIR}/CompilationLog 2>&1
	make INSTALLPATH=${ELCDIR} install -C contrib \
	    >> ${ELCDIR}/CompilationLog 2>&1
	#make clean >> ${ELCDIR}/CompilationLog 2>&1
	rm -f config.* *.elc {theme,contrib,kanakan-cursor}/*.elc
	rm -f ${ELCDIR}/*.X

	gzip -9 ${ELCDIR}/CompilationLog
	echo ${VERSION} > ${STAMP}

	#cp -a ${ELDIR}/${STARTFILE} ${STARTDIR}/60${STARTFILE};
	echo " done."
	;;
	
	*)
	;;
esac

exit 0;
