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

set -e

FLAVOR=$1
PACKAGE="navi2ch"
VERSION="2.0.0"

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

compile() {

    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-lispdir=${ELCDIR} > ${ELCDIR}/CompilationLog 2>&1
    ## Unrecognized options: --with-xemacs, --with-emacs in ./configure
    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.*
    rm ${ELCDIR}/*.el
    
    #if [ -e /usr/share/info/navi2ch.info.gz ]; then
    #   rm -f /usr/share/info/navi2ch.info*.gz
    #fi

    gzip -9 ${ELCDIR}/CompilationLog
    #gzip -9 /usr/share/info/navi2ch.info
    echo ${VERSION} > ${STAMP}
    
    install -p -m644 ${ELDIR}/vine-default-${PACKAGE}.el ${ELCDIR}
    ln -f -s ${ELDIR}/${STARTFILE} ${STARTDIR}/90${STARTFILE};
    echo " done."
}

case "${FLAVOR}" in

    emacs-20*)
	echo "Not Support"
	;;

    emacs-21*)
	compile
	;;

    emacs-22*)
	compile
	;;

    xemacs-*)
	compile
	;;

    emacs-23*)
	compile
	;;
    
    emacs-24*)
	compile
	;;
    
    *)
	;;
esac

exit 0;
