#! /bin/sh -e
# /usr/lib/emacsen-common/packages/install/preview-latex

# Written by Jim Van Zandt <jrv@vanzandt.mv.com>, borrowing heavily
# from the install scripts for gettext by Santiago Vila
# <sanvila@ctv.es> and octave by Dirk Eddelbuettel <edd@debian.org>.

FLAVOR=$1
PACKAGE=preview-latex
VERSION=0.9.1-1

FLAGS="-q -batch -l path.el -f batch-byte-compile"

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

STAMP=${ELCDIR}/compile-stamp

if [ ${FLAVOR} = emacs ]; then exit 0; fi

if [ ${FLAVOR} != emacs21 -a ${FLAVOR} != xemacs21 ]
then
echo "Ignoring emacsen flavor ${FLAVOR}"
else
echo install/${PACKAGE}: Handling install for emacsen flavor ${FLAVOR}

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

# Install-info-altdir does not actually exist. 
# Maybe somebody will write it.
if test -x /usr/sbin/install-info-altdir; then
    echo install/${PACKAGE}: install Info links for ${FLAVOR}
    install-info-altdir --quiet --section "" "" --dirname=${FLAVOR} /usr/info/${PACKAGE}.info.gz
fi

install -m 755 -d ${ELCDIR}

case "${FLAVOR}"
in
'emacs21')
	FILES="preview.el prv-emacs.el preview-latex.el"
	SITEFLAG="--no-site-file"
	cat << EOF > ${ELCDIR}/path.el
(setq load-path (cons "." load-path) byte-compile-warnings nil)
EOF
	;;
'xemacs21')
	FILES="preview.el prv-xemacs.el preview-latex.el"
	SITEFLAG="-no-site-file"
	cat << EOF > ${ELCDIR}/path.el
(setq data-directory-list (cons "." data-directory-list))
(setq load-path (cons "." load-path) byte-compile-warnings nil)
EOF
	;;
*)
	;;
esac

cd ${ELDIR}
cp ${FILES} ${ELCDIR}
cd ${ELCDIR}

${FLAVOR} ${SITEFLAG} ${FLAGS} ${FILES} > ${ELCDIR}/CompilationLog 2>&1
gzip -9 --force ${ELCDIR}/CompilationLog
echo ${VERSION} > ${STAMP}
rm -f *.el path.el
fi

exit 0
