#!/bin/sh
#-----------------------------------------------------------------------------
# ac: Add entry to changelog
#-----------------------------------------------------------------------------
#
#   Copyright (C) 2000
#   Associated Universities, Inc. Washington DC, USA.
#
#   This program 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 of the License, or
#   (at your option) any later version.
#
#   This program 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 this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: casa-feedback@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#
#-----------------------------------------------------------------------------
# Usage: ac [-i] [-d cvsroot]
#-----------------------------------------------------------------------------
# ac adds an entry to the changelog of an application or library module.
#
# 'ac' first checks out the changelog and CHANGELOG.LAST* files.
# It starts a glish script to let the user fill in a change entry,
# which also increases the lastid in CHANGELOG.LASTID and adds the
# entry to the changelog file.
# Finally the files are checked into the system.
#
# Status returns:
#    0:  success
#    1:  initialization error
#    2:  checkout failure
#    3:  checkin failure
#    4:  no changes made
#
# Notes:
#    -i indicates that the files only need to be checked in.
#      This should hardly ever be needed. Only if the checkin part
#      cannot be done (e.g. because exhale started) it needs to be used
#      once checkins are possible again.
#    -d lets you give the cvs root in case $CVSROOT is not defined.
#
# Original: 2000/04/03 by Ger van Diepen, NFRA
# $Id$
#=============================================================================

# Determine the code directory (for CHANGELOG.LAST*).
a_code=`pwd | sed -e '{s#/code/.*#/code#;}'`
a_pwd=`pwd`

# Find switches

onlyci=0
while [ 1 = 1 ]
do
  # Special cvs command?
  if [ "$1" = "-d" ]; then
    shift
    CVSROOT="$1"
    shift
  elif [ "$1" = "-i" ]; then
    onlyci=1
    shift
  else
    break
  fi
done

# Ignore most if only a checkin needs to be done.
if [ "$onlyci" != "1" ]
then
    
    # A GUI is needed for the glish script.
    if [ "$DISPLAY" = "" ]
	then
	echo "ac: DISPLAY is not set, abort!" 1>&2
	exit 1
    fi

    # Check out the required files.
    # Exit if checking out fails.
    echo 'ac: Checking out changelog and CHANGELOG.LAST* ...'
    cd $a_code
    cvs update CHANGELOG.LASTID CHANGELOG.LAST
    if [ "$?" != 0 ]
    then
	exit 2;
    fi
    cd $a_pwd
    cvs update changelog
    if [ "$?" != 0 ]
    then
	cd $a_code
	\rm -f CHANGELOG.LAST*
	exit 2;
    fi

    # Let the user fill in a changelog form and append it to the changelog.
    # If no changes found, unlock the files.
    echo 'ac: Starting glish ...'
    glish addchange.g
    if [ ! -f changelog.entry-full ]
    then
	echo "ac: No entry added to changelog";
	\rm -f changelog changelog.*
	cd $a_code
	\rm -f CHANGELOG.LAST*
	exit 4;
    fi
fi

# Check out the last id again (it might be changed in the meantime).
# Increment the id. Make sure the new id is 4 characters.
echo 'ac: Updating and checking in changelog and CHANGELOG.LAST* ...'
cd $a_code
\rm -f CHANGELOG.LASTID CHANGELOG.LAST
cvs update CHANGELOG.LASTID CHANGELOG.LAST
a_idold=`cat CHANGELOG.LASTID`
a_id=`expr $a_idold + 1 | awk '{printf("%04d",$1)}'`
# Update the common files and commit them.
\rm -f CHANGELOG.LASTID
echo $a_id > CHANGELOG.LASTID
sed $a_pwd/changelog.entry-brief -e "s/-_-chid-_-/$a_id/" >> CHANGELOG.LAST
cvs commit -f -m "ac: set to change-id $a_id" CHANGELOG.LASTID CHANGELOG.LAST
a_sts=$?
\rm -f CHANGELOG.LASTID CHANGELOG.LAST
if [ $a_sts != 0 ]
then
  echo "Commit failed; try it later using:  ac -i"
  exit 1
fi
# Update and check in the changelog file.
# In one way or another the -f (force) switch has to be used with cvs commit,
# otherwise it thinks there are no changes.
cd $a_pwd
\rm -f changelog
cvs update changelog
sed $a_pwd/changelog.entry-full -e "s/-_-chid-_-/$a_id/" >> changelog
cvs commit -f -m "ac: added change-id $a_id" changelog
a_sts=$?
\rm -f changelog
if [ $a_sts != 0 ]
then
  echo "Commit failed; try it later using:  ac -i"
  exit 1
fi
# Commit was successful, so the entry files can be deleted.
\rm -f changelog.*


exit 0
