#!/bin/sh 
#                               -*- Mode: Sh -*- 
# emacsen-common-install --- 
# Author           : Manoj Srivastava ( srivasta@tiamat.datasync.com ) 
# Created On       : Wed Oct 14 23:23:39 1998
# Created On Node  : tiamat.datasync.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Wed Sep 15 19:45:33 2004
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 30
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
# arch-tag: c66aafd1-413f-40ed-8bbd-d80716311b4a


set -e

FLAVOUR=$1
PACKAGE="calc"
VERSION="2.02f-23"

if [ "X$FLAVOUR" = "X" ]; then
    echo Need argument to determin FLAVOUR of emacs;
    exit 1
fi

if [ "X$PACKAGE" = "X" ]; then
    echo Internal error: need package name;
    exit 1;
fi

if [ "X$VERSION" = "X" ]; then
    echo Internal error: need package version;
    exit 1;
fi

ELDIR=/usr/share/emacs/site-lisp/$PACKAGE
ELCDIR=/usr/share/$FLAVOUR/site-lisp/$PACKAGE

INFO_FILES="/usr/info/${PACKAGE}.info.gz";
STARTDIR=/etc/$FLAVOUR/site-start.d
STARTFILE="$PACKAGE-init.el";

# Calc is compiled differently than other Emacs programs; it even
# provides its own compilation method.
FLAGS="${SITEFLAG} -q -batch -l path.el -l calc-maint.el -f calc-compile"

case "$FLAVOUR" in
    emacs | emacs19  | emacs20 | xemacs21)
	echo "install/$PACKAGE: Ignoring $FLAVOUR."
	;;
    emacs21 | emacs-snapshot)
	echo -n "install/$PACKAGE: Byte-compiling for $FLAVOUR..."
	
	if which $FLAVOUR 2>&1 >/dev/null; then
	    echo -n "found $FLAVOUR .."
	else
	    echo "Could not find $FLAVOUR. Exiting"
	    exit 0;
	fi

        if [ -d $ELCDIR ]; then
	    test -f $ELCDIR/install.log && rm -f $ELCDIR/install.log
	    rm -f $ELCDIR/*.elc || true;
             rm -f $ELCDIR/Makefile || true;
	    rm -f $ELCDIR/install.log.gz || true;
             rmdir $ELCDIR
        fi
        if [ ! -d $ELCDIR ]; then
            install -m 755 -d $ELCDIR
        fi

	cd $ELDIR
	FILES=`echo *.el`
	
	LOG=`tempfile`;
	trap "test -f $LOG && mv -f $LOG  $ELCDIR/install.log.gz > /dev/null 2>&1" exit
	
	cp Makefile ${FILES} ${ELCDIR}
	cd ${ELCDIR}
	rm $STARTFILE
	cat << EOF > path.el
(setq load-path (cons "." load-path))
EOF
	estatus=0
	if [ -x /usr/bin/make ]; then
	    make clean          2>&1     | gzip -9 2>/dev/null           > $LOG;
	    if (make EMACS=$FLAVOUR compile 2>&1) | gzip -9 2>/dev/null >> $LOG; then
		echo  -n " "
	    else
		estatus=1
		echo "Compilation failed, log saved as $ELCDIR/install.log.gz"
	    fi
	else
	    if (${FLAVOUR} ${FLAGS} 2>&1) | gzip -9 > $LOG  ; then
		echo  -n " "
	    else
		estatus=1
		echo "Compilation failed, log saved as $ELCDIR/install.log.gz"
	    fi
	fi
	mv -f $LOG                 $ELCDIR/install.log.gz;
	chmod 644                  $ELCDIR/install.log.gz;
	if [ "X$estatus" = "X1" ]; then
	    exit $estatus
	fi
	rm -f *.el  || true;
	
	ucf $ELDIR/$STARTFILE $STARTDIR/20$STARTFILE;
	
        # Install-info-altdir does not actually exist. 
        # Maybe somebody will write it.
        if test -x /usr/sbin/install-info-altdir; then
            for infofile in $INFO_FILES; do
                if [ -e $infofile ]; then
                    install-info-altdir --quiet --section "" "" \
                        --dirname=${FLAVOUR} $infofile
                fi
            done
        fi
        echo "done."
	;;
    *)
        echo "install/$PACKAGE: Ignoring emacsen flavor $FLAVOUR.";	
esac


exit $estatus
