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

FLAVOR=$1
PACKAGE=ddskk

if [ -z "${FLAVOR}" ]; then exit 1; fi

case $FLAVOR in
    emacs23|emacs22|emacs21|emacs20|emacs19|mule2|*xemacs*)
    exit 0
    ;;
esac

ELCDIR=/usr/share/$FLAVOR/site-lisp/$PACKAGE
ELDIR=/usr/share/$PACKAGE
ELCSTAMP=$ELCDIR/compile-stamp

if [ -f "$ELCSTAMP" ]; then
    if [ -z "`find "$ELDIR/skk.el" -newer "$ELCSTAMP"`" ]; then
	echo "install/$PACKAGE: already byte-compiled for $FLAVOR, skipped"
	exit 0
    fi
    rm -f "$ELCSTAMP"
fi

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

FLAGS="-no-site-file -q -batch -l __path.el"

install -m 755 -d ${ELCDIR}
cd ${ELDIR}
LINKS=`echo SKK-* *.el*`
if [ -d nicola ]; then
    LINKS="$LINKS "`echo nicola/NICOLA-* nicola/*.el*`
fi
cd ${ELCDIR}
for f in $LINKS; do
    ln -sf "../../../$PACKAGE/$f" .
done
cp -R "$ELDIR/maint" .

cat << EOF > __path.el
(setq inhibit-automatic-native-compilation t)
(setq native-comp-deferred-compilation nil)
(setq comp-enable-subr-trampolines nil)
(set-language-environment "Japanese")
(prefer-coding-system 'euc-jp)
(prefer-coding-system 'iso-2022-jp)
(setq SKK_SET_JISYO nil)
(setq load-path (cons "/usr/share/${FLAVOR}/site-lisp/apel" load-path))
(setq load-path (cons "${ELCDIR}" load-path))
(setq byte-compile-warnings t)
EOF
mv -f skk-setup.el.in skk-setup.el
mv -f leim-list.el.in leim-list.el
rm -f *.elc
set +e
${FLAVOR} ${FLAGS} -f batch-byte-compile skk-setup.el >CompilationLog 2>&1
if [ $? -ne 0 ]; then cat CompilationLog; exit 1; fi
${FLAVOR} ${FLAGS} -l SKK-MK -f SKK-MK-compile >>CompilationLog 2>&1
if [ $? -ne 0 ]; then cat CompilationLog; exit 1; fi

${FLAVOR} ${FLAGS} -l NICOLA-DDSKK-ELS -l NICOLA-DDSKK-MK -f nicola-ddskk-generate-autoloads >>CompilationLog 2>&1
if [ $? -ne 0 ]; then cat CompilationLog; exit 1; fi
${FLAVOR} ${FLAGS} -f batch-byte-compile nicola-ddskk-autoloads.el >>CompilationLog 2>&1
if [ $? -ne 0 ]; then cat CompilationLog; exit 1; fi
${FLAVOR} ${FLAGS} -l maint/install.el -l NICOLA-DDSKK-MK -f compile-nicola-ddskk NONE NONE NONE >>CompilationLog 2>&1
if [ $? -ne 0 ]; then cat CompilationLog; exit 1; fi
set -e

rm -f SKK-* leim-list.el* *.in NICOLA-* __path.el*
rm -f ${ELCDIR}/skk-lookup.elc ${ELCDIR}/skk-jisx0213.elc
rm -f ${ELCDIR}/ptexinfmt.el* ${ELCDIR}/install.el*
rm -rf "$ELCDIR/maint"
gzip -9qf CompilationLog

if [ -f /usr/share/emacsen-common/debian-startup.el ] && \
   [ ! -f "/etc/$FLAVOR/site-start.d/50$PACKAGE.el" ] && \
   [ -f "/etc/emacs/site-start.d/50$PACKAGE.el" ] && \
   [ -d "/etc/$FLAVOR/site-start.d" ]; then
    ln -sf "../../emacs/site-start.d/50$PACKAGE.el" "/etc/$FLAVOR/site-start.d/50$PACKAGE.el"
fi

touch "$ELCSTAMP"
exit 0
