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

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

FLAVOR=$1
PACKAGE=proofgeneral

if [ ${FLAVOR} = emacs24 -o ${FLAVOR} = emacs25 ]; then 
    true
else
    exit 0; 
fi

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

PG_SUBDIRS="generic lib coq"

#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}
RELELDIR=../../../emacs/site-lisp/${PACKAGE}

install -m 755 -d ${ELCDIR}
ALLFILES=
for d in ${PG_SUBDIRS} ; do
    install -m 755 -d ${ELCDIR}/${d}
    cd ${ELDIR}
    FILES=`echo ${d}/*el`
    ALLFILES="${ALLFILES} ${FILES}"
    cd ${ELCDIR}/${d}
    ln -sf ../${RELELDIR}/${d}/*el .
done

cd ${ELCDIR}
cat << EOF > path.el
(require (quote bytecomp))
(mapc 
  (lambda (dir)
;; don't use debian-pkg-add-load-path-item until #676424 is fixed
;;     (if (fboundp 'debian-pkg-add-load-path-item)
;;         (debian-pkg-add-load-path-item dir)
       (add-to-list 'load-path dir))
  '(
EOF

for d in ${PG_SUBDIRS} ; do
    echo "\"${ELCDIR}/${d}\"" >> path.el
done

cat << EOF >> path.el
  ))
(setq byte-compile-warnings 
      (remove 
       (quote cl-functions)
       (remove (quote noruntime) byte-compile-warning-types)))
EOF
${FLAVOR} ${FLAGS} ${ALLFILES}

rm -f path.el


# The path for images, proof-images-directory, is determinded at the
# time when generic/proof-site.el is loaded, see there. The simplest
# solution to ensure that Proof General finds the images is to link
# them.
ln -sf ${RELELDIR}/images ${ELCDIR}

exit 0
