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

FLAVOR=$1
PACKAGE=wl-beta
PKGSNAME=wl

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

case $FLAVOR in
    emacs19|mule2|xemacs20|xemacs19)
    exit 0
    ;;
    *xemacs*)
    mule=`/bin/ls -1 /usr/bin/$FLAVOR*-mule* 2>/dev/null | head -1`
    nomule=`/bin/ls -1 /usr/bin/$FLAVOR*-nomule* 2>/dev/null | head -1`
    if [ -n "$mule" ]; then
	if [ ! -f "/usr/share/$FLAVOR/site-lisp/apel/poe.elc" ]; then
	    inst=/usr/lib/emacsen-common/packages/install/apel
	    if [ ! -x "$inst" ]; then exit 0; fi
	    "$inst" "$FLAVOR"
	fi
	if [ ! -f "/usr/share/$FLAVOR/site-lisp/flim/mime.elc" ]; then
	    inst=/usr/lib/emacsen-common/packages/install/flim
	    if [ ! -x "$inst" ]; then exit 0; fi
	    "$inst" "$FLAVOR"
	fi
	if [ ! -f "/usr/share/$FLAVOR/site-lisp/semi/mime-view.elc" ]; then
	    inst=/usr/lib/emacsen-common/packages/install/semi
	    if [ ! -x "$inst" ]; then exit 0; fi
	    "$inst" "$FLAVOR"
	fi
	if [ ! -f "$ELCDIR/$PKGSNAME.elc" ]; then rm -f "$ELCSTAMP"; fi
    fi
    if [ -n "$nomule" ]; then
	if [ ! -f "/usr/share/$FLAVOR/site-lisp/apel/nomule/poe.elc" ]; then
	    inst=/usr/lib/emacsen-common/packages/install/apel
	    if [ ! -x "$inst" ]; then exit 0; fi
	    "$inst" "$FLAVOR"
	fi
	if [ ! -f "/usr/share/$FLAVOR/site-lisp/flim/nomule/mime.elc" ]; then
	    inst=/usr/lib/emacsen-common/packages/install/flim
	    if [ ! -x "$inst" ]; then exit 0; fi
	    "$inst" "$FLAVOR"
	fi
	if [ ! -f "/usr/share/$FLAVOR/site-lisp/semi/nomule/mime-view.elc" ]; then
	    inst=/usr/lib/emacsen-common/packages/install/semi
	    if [ ! -x "$inst" ]; then exit 0; fi
	    "$inst" "$FLAVOR"
	fi
	if [ ! -f "$ELCDIR/nomule/$PKGSNAME.elc" ]; then rm -f "$ELCSTAMP"; fi
	if [ -n "$mule" ]; then
	    EMACSEN="$mule $nomule"
	else
	    EMACSEN="$nomule"
	fi
    else
	EMACSEN=$FLAVOR
    fi
    ;;
    *)
    if [ ! -f "/usr/share/$FLAVOR/site-lisp/apel/poe.elc" ]; then exit 0; fi
    if [ ! -f "/usr/share/$FLAVOR/site-lisp/flim/mime.elc" ]; then exit 0; fi
    if [ ! -f "/usr/share/$FLAVOR/site-lisp/semi/mime-view.elc" ]; then exit 0; fi
    EMACSEN=$FLAVOR
    ;;
esac

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

LOG=`mktemp -t elc.XXXXXXXXXXXX`
chmod 644 "$LOG"
echo "install/$PACKAGE: byte-compiling for $FLAVOR, logged in $LOG"

cd "$ELDIR"
LINKS=`echo elmo/*.el utils/*.el wl/*.el wl/wl-news.el.in NEWS* WL-* etc`
if [ ! -d "$ELCDIR" ]; then
    mkdir "$ELCDIR"
    chmod 755 "$ELCDIR"
fi

for EMACS in $EMACSEN; do
    cd "$ELCDIR"
    TOELDIR=../../../$PKGSNAME
    case $EMACS in
	*-nomule*)
	if [ x"$EMACS" != x"$EMACSEN" ]; then
	    if [ ! -d nomule ]; then
		mkdir nomule
		chmod 755 nomule
	    fi
	    cd nomule
	    TOELDIR=../$TOELDIR
	fi
	;;
    esac
    rm -f *.elc __myinit.el
    for f in $LINKS; do
	ln -sf "$TOELDIR/$f" .
    done
    case $FLAVOR in
	*xemacs*)
	rm -f wl-e21.el wl-mule.el
	;;
	emacs22|emacs21|emacs20)
	break
	;;
	emacs|emacs2[3-9]|emacs-*|emacs*cvs*)
	rm -f wl-xmas.el wl-mule.el utf7.el
	;;
	*)
	rm -f wl-xmas.el wl-mule.el
	;;
    esac
    case $EMACS in
	*-nomule*)
	rm -f elmo-shimbun.el
	;;
    esac
    FILES=`/bin/ls -1 *.el | egrep -v '^(bbdb-wl.el|elsp-spamfilter.el|elmo-shimbun.el|wl-news.el)'`

    cat > __myinit.el << EOF
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/apel" load-path))
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/flim" load-path))
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/semi" load-path))
(setq load-path (cons "." load-path))
(setq byte-compile-warnings nil)
(setq WLDIR ".")
EOF
    if [ -f elmo-shimbun.el ] && [ -f "/usr/share/$FLAVOR/site-lisp/w3m/shimbun/shimbun.elc" ]; then
	FILES="$FILES elmo-shimbun.el"
	cat >> __myinit.el << EOF
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/w3m" load-path))
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/w3m/shimbun" load-path))
EOF
    fi
    case $EMACS in
	*-nomule*)
	cat >> __myinit.el << EOF
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/apel/nomule" load-path))
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/flim/nomule" load-path))
(setq load-path (cons "/usr/share/$FLAVOR/site-lisp/semi/nomule" load-path))
EOF
	;;
	*)
    esac

    FLAGS="-q -no-site-file -batch -l __myinit.el -l WL-MK -eval"
    ARG="(progn (config-wl-package-subr)(make-wl-news))"
    echo "$EMACS" $FLAGS "$ARG" >> "$LOG"
    "$EMACS" $FLAGS "$ARG" >> "$LOG" 2>&1

    FLAGS="-q -no-site-file -batch -l __myinit.el -f batch-byte-compile"
    echo "$EMACS" $FLAGS $FILES >> "$LOG"
    "$EMACS" $FLAGS $FILES >> "$LOG" 2>&1
    chmod 644 *.elc

    rm -f __myinit.el* wl-news.el.in NEWS* WL-*
done # EMACS in $EMACSEN

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

echo "install/$PACKAGE: deleting $LOG"
rm -f "$LOG"

echo "install/$PACKAGE: byte-compiling for reverse dependency"
pkg=mhc
dir=/usr/share/$FLAVOR/site-lisp/$pkg
inst=/usr/lib/emacsen-common/packages/install/$pkg
if [ -d "$dir" ] && [ -f "$inst" ]; then
    rm -f "$dir"/*.elc "$dir"/*-stamp
    "$inst" "$FLAVOR"
fi

touch "$ELCSTAMP"
exit 0
