#! /bin/sh
#
# NAME
#   release-utils - utilities useful when releasing MH-E
#
# SYNOPSIS
#   release-utils
#
# DESCRIPTION
#   This script contains a handful of utilities that are useful when
#   releasing MH-E
#
# OPTIONS
#   --debug
#     Turn on debugging messages.
#
#   --help
#     Display the usage of this command.
#
#   --import-emacs
#     Import Emacs changes. This should be invoked as "make import."
#
#   --variable-changes tag
#     Displays variables that have changed since the given tag in a form
#     suitable for the ChangeLog. 
#
#   --version
#     Display program version.
#
# RETURN VALUE
#
# EXAMPLES
#
# ENVIRONMENT
#   MHE_HOME
#     Location of MH-E development tree. Default is current directory.
#
#   EMACS_HOME
#     Location of Emacs development tree. Default is /usr/local/src/emacs.
#
# AUTHOR
#
#   Copyright (C) 2001, 2004 Bill Wohler
#
#   This file is part of MH-E.
#
#   MH-E is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   MH-E is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with MH-E; see the file COPYING.  If not, write to the
#   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.

# Initializations.

# Constants.
cmd=`basename $0`                       # name by which command called
MHE_HOME=${MHE_HOME:-`pwd`}
EMACS_HOME=${EMACS_HOME:-/usr/local/src/emacs}
export CVSROOT=`cat $MHE_HOME/CVS/Root`

# Program version.
ver=1.1

# Variables (may be overridden by arguments).
debug=0                                 # verbose mode
exitstatus=0                            # upon error, set to non-zero and exit
import_emacs=0				# import Emacs changes
variable_changes=0			# changed variables ChangeLog output

interrupt=0                             # upon interrupt, set to 1

# Functions.

# Display usage information and exit.
usage() {
    cat <<EOF
Usage: $cmd [options]
--debug                 print actions that program takes
--help                  display this message
--import_emacs		import Emacs changes
--version               display program version
EOF
    bye 1;
}

# Exit gracefully.
# Usage: bye [(status [message]]
bye() {
    if [ $# -gt 0 ]; then
        exitstatus=$1
        shift
    fi
    if [ $# -gt 0 ]; then
        echo "$*"
    elif [ $interrupt -eq 1 ]; then
        echo ""
    fi
    exit $exitstatus
}

# Handle an interrupt.
intr() {
    echo ""
    interrupt=1
    bye 1
}

# Display version information and exit.
show_version() {
    cat <<EOF
$cmd version $ver
Copyright (C) Bill Wohler <wohler@newt.com>

$cmd comes with ABSOLUTELY NO WARRANTY.

This is free software, and you are welcome
to redistribute it under certain conditions.

See http://www.gnu.org/copyleft/gpl.html for details.
EOF
    exit 0;
}

# Import Emacs changes.
import_emacs() {
    # Calculate release.
    cd $EMACS_HOME
    release=`cvs status lisp/mh-e/mh-e.el    | \
	     awk '/Sticky Tag/{print $3}'    | \
	     sed -e 's/EMACS_/emacs-/' -e 's/(none)/emacs-mainline/'`

    # Grab files.
    cd $MHE_HOME || return 1
    rm -rf t
    mkdir t || return 1
    cd t
    cp $EMACS_HOME/COPYING .
    cp $EMACS_HOME/lisp/mh-e/mh-*.el .
    cp $EMACS_HOME/lisp/mh-e/ChangeLog .
    cp $EMACS_HOME/etc/MH-E-NEWS .

    # Import and update.
    cvs -d $CVSROOT import -m "$release" src emacs $release
    cd ..
    rm -rf t
    cvs update -jemacs:yesterday -jemacs
}

# Display variable changes.
#
# Usage: variable_changes tag
#
variable_changes() {
    tag=$variable_changes

    # Check out old version.
    if [ ! -d $tag ]; then
	echo -n "Checking out version $tag..."
	cvs -Q -d `cat CVS/Root` co -r $tag -d $tag src || exit 1
	echo "done"
    fi
    if [ ! -d $tag ]; then
	echo "Directory $tag not found. Did you enter the tag name correctly?"
        exit 1
    fi
    
    grep '(defcustom' $tag/mh-customize.el \
	| awk '{print $2}' \
	| sort > /tmp/mh-var-$tag
	
    grep '(defcustom' mh-customize.el \
	| awk '{print $2}' \
	| sort > /tmp/mh-var-cur
	
    echo "** New Variables in MH-E XXX"
    for i in `diff -u /tmp/mh-var-$tag /tmp/mh-var-cur \
        | egrep -v '^(---|\+\+\+|@@)' \
	| grep ^\+ \
	| sed 's/^\+//'`; do
	echo ""
	echo "*** $i"
    done
    echo ""
    echo "** Variables Deleted in MH-E XXX"
    for i in `diff -u /tmp/mh-var-$tag /tmp/mh-var-cur \
        | egrep -v '^(---|\+\+\+|@@)' \
	| grep ^\- \
	| sed 's/^-//'`; do
	echo ""
	echo "*** $i"
    done

    rm -rf $tag /tmp/mh-var-$tag /tmp/mh-var-cur
}

# Parse command line.
while [ $# != 0 ]; do
    case "$1" in
    --d*)       debug=1;;
    --i*)	import_emacs=1;;
    --h*)       usage;;
    --va*)      shift; variable_changes="$1";;
    --ve*)      show_version;;
    -*)         usage;;
    *)          break;;
    esac
    shift
done

trap intr 2

[ $import_emacs = 1 ] && import_emacs
[ $variable_changes != 0 ] && variable_changes $variable_changes

bye
