#!/bin/sh
set -u
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

#This script takes two paramaters:
# 1)  The initial version to use for the reporting of changes.
# 2)  The later version to stop the reporting of changes.
#
# All changes that occured between the two specified versions in CVS are
# then printed out to standard out.  It is formated as a basic html page.
# After this script is run, the user should then edit the output file and
# add in general user summaries of the cvs logs.


#Show how to use the script.
print_help()
{
  echo "USAGE:"
  echo "`basename $0` <first version> <second version>"
  echo
  echo "Example:"
  echo "`basename $0` v2_18_encp v2_19_encp"
}

#This function gets the changes for a single file (in $1), between the versions
# specified in $2 and $3.
get_file_changes()
{
  file=$1
  version1=$2   # something like 'v2_19_encp'
  version2=$3

  #Get the older cvs revison number.
  rev1=`cvs status -v $file 2>/dev/null | grep $version1 | head -n 1| sed -e "s/.*revision://" -e 's/)//'`

  #If the 'cvs status' failed; exit.
  if [ -z $rev1 ]; then return 1;fi

  rev1=`echo $rev1`
  major1=`echo $rev1 |cut -f1 -d.`
  minor1=`echo $rev1 |cut -f2 -d.`
  next1=`expr $minor1 + 1`

  #Get the newer cvs revison number.
  rev2=`cvs status -v $file 2>/dev/null | grep $version2 | tail -n 1| sed -e "s/.*revision://" -e 's/)//'`

  #If the 'cvs status' failed; exit.
  if [ -z $rev2 ]; then return 1;fi

  rev2=`echo $rev2`
  major2=`echo $rev2 |cut -f1 -d.`
  minor2=`echo $rev2 |cut -f2 -d.`
  prev2=`expr $minor2 - 1`
    
  #If the file didn't change between cuts don't report it.
  if [ "$rev1" = "$rev2" ];then return;fi

  #Write out the output.
  /bin/echo -n "========== $file ===================================================================================="
  changes=`cvs log -N -r$major1.$next1:$major2.$minor2 $file|egrep -v '^revision |^date:|^RCS file:|^Working file:|^head:|^branch:|^locks:|^access list:|^keyword substitution:|^total revisions:|^selected revisions:|^description:|*** empty log message ***'`
  echo $changes|sed -e 's/---------------------------- /\
\
/g'| sed -e 's/=============================================================================//'
  echo
}

#Take an enstore directory as input and report on all files in that directory.
report_dir()
{
  directory=$1
  version1=$2
  version2=$3

  cd $directory
  files=`ls -1`

  for f in $files
  do
    # check all text files only
    file $f | grep text > /dev/null
    if [ $? -eq 0 ] # text file
    then
	get_file_changes `basename $f` $version1 $version2 2> /dev/null
    fi
  done
}

#Takes two paramaters, the first is the older version the second is the
# new version.
print_header()
{
  echo "<html><body><pre>"
  echo "                      Enstore release notes, from $1 to $2"


  echo "                      Detailed cvs commit logs"

  echo
  echo
}

print_trailer()
{
  echo "</pre></body></html>"
}



#This is where execution begins.

#If two version were not specified on the command line; print help and exit.
if [ $# -lt 2 ] ; then print_help; exit 1; fi

#Print out the report.

print_header $1 $2

cd $ENSTORE_DIR
# go through all directories recursively
directories=`find . -type d -exec ls -d {} \; | grep -v CVS`
for d in $directories 
do
    echo "${d}"; report_dir ${ENSTORE_DIR}/${d} $1 $2
done

print_trailer
