#! /bin/bash -e
# /usr/lib/emacsen-common/packages/install/post-el

# 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>.
#
# Patched by Roland Mas <lolando@debian.org> to add support for lists
# of flavor-dependently included/excluded files

FLAVOR=$1
PACKAGE=post-el

# INCLUDED_emacs20=""
# INCLUDED_emacs21=""
# INCLUDED_xemacs21=""

# EXCLUDED_emacs20=""
# EXCLUDED_emacs21=""
# EXCLUDED_xemacs21=""

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

echo install/${PACKAGE}: Handling install for emacsen flavor ${FLAVOR}

#FLAVORTEST=`echo $FLAVOR | cut -c-6`
#if [ ${FLAVORTEST} = xemacs ] ; then
#    SITEFLAG="-no-site-file"
#else
#    SITEFLAG="--no-site-file"
#fi
FLAGS="${SITEFLAG} -q -batch -l path.el -f batch-byte-compile"

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

# 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}
cd ${ELDIR}

PATTERN=""
for i in ${!INCLUDED_*} ${!EXCLUDED_*} ; do
    [ ! -z "$PATTERN" ] && PATTERN="${PATTERN}\|"
    PATTERN="${PATTERN}^$(eval echo \$$i)\$"
done

if [ -z "$PATTERN" ] ; then
    FILES=$(ls -1 *.el)
else
    FILES=$(ls -1 *.el | grep -v $PATTERN)
    FILES="$FILES $(eval echo \$$(echo INCLUDED_$FLAVOR))"
fi

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

cat << EOF > path.el
(setq load-path (cons "." load-path) byte-compile-warnings nil)
EOF
${FLAVOR} ${FLAGS} ${FILES}
chmod 0644 ${FILES} # added for people with weird umasks
rm -f *.el path.el

exit 0
