#!/bin/sh
# Fix up the @since tags
# @author: Christopher Hylands
# @version: $Id: fixsince 69605 2014-07-30 15:04:12Z cxh $
#
# There are several cases
# 1. The file is new between releases and has no tag
# 2. The file is new between releases and has the wrong tag
# 3. An old file has the incorrect tag

# The first thing to do is to generate a list of new files
# by running diff between the old release and the new release

oldVersion=8.0.1
newVersion=10.0.devel
sinceVersion=10.0

#diffOutput=/tmp/fixsince.diffOutput.$$
diffOutput=/tmp/pt.diffs
if [ ! -f $diffOutput ]; then
    echo "Generating $diffOutput, this will take awhile"
    diff -r $PTII/adm/dists/ptII$oldVersion \
	$PTII/adm/dists/ptII$newVersion > $diffOutput
fi
ls -l $diffOutput

newJavaFiles=/tmp/fixsince.newJavaFiles
if [ ! -f $newJavaFiles ]; then
    echo "Creating $newJavaFiles for new Java files."
    egrep "^Only in ${PTII}/adm/dists/ptII${newVersion}/" $diffOutput |
        egrep '.java$' |
        sed -e 's/Only in //' -e 's@: @/@' |
	grep -v DEThreadActor.java | grep -v jimblackler | grep -v omc/corba | grep -v org/json | grep -v swig |
	sed -e "s@$PTII/adm/dists/ptII$newVersion/@@" -e 's@^\./@@'> $newJavaFiles 
    (cd $PTII/adm/dists/ptII$oldVersion; $PTII/adm/bin/ptIItxtfiles) | egrep '.java$' | sed -e "s@$PTII/adm/dists/ptII$newVersion/@@" -e 's@^\./@@' | sort > /tmp/fixsince.oldJavaFiles
    (cd $PTII/adm/dists/ptII$newVersion; $PTII/adm/bin/ptIItxtfiles) | egrep '.java$' | sed -e "s@$PTII/adm/dists/ptII$newVersion/@@" -e 's@^\./@@' | sort > /tmp/fixsince.newFiles
    comm -13 /tmp/fixsince.oldJavaFiles /tmp/fixsince.newFiles | 	grep -v DEThreadActor.java | grep -v jimblackler | grep -v omc/corba | grep -v org/json | grep -v swig  >> $newJavaFiles
    sort $newJavaFiles | uniq | egrep -v "^com" > /tmp/fixsince.newJavaFiles.s
    mv /tmp/fixsince.newJavaFiles.s $newJavaFiles
fi
ls -l $newJavaFiles

if [ ! -f $newJavaFiles ]; then
    echo "$0: Can't find $newJavaFiles to determine which files are new, exiting"
    exit 9 
fi

files=`cat $newJavaFiles`
for file in $files
do
  if [ ! -f $file ]; then
      echo "$file is not a file, skipping"
      continue
  fi
  grep @since $file >& /dev/null 
  retval=$?
  tmpfile=/tmp/fixsince.tmpfile
  tmpfile2=/tmp/fixsince.tmpfile2
  if [ $retval = 1 ]; then
    echo "$file does not have an @since tag"
    awk -v sinceVersion=$sinceVersion \
	'{  if ($0 ~ /@version/) {
	       print $0
	       print "@since Ptolemy II " sinceVersion
	    } else {
	       print $0
            }
         }' <$file >$tmpfile
    grep @since $file >& /dev/null 
    retval=$?
    if [ $retval = 1 ]; then
      echo "$file still does not have an @since tag, trying to add one after @version"
      awk -v sinceVersion=$sinceVersion \
          '{  if ($0 ~ /@author/) {
	       print $0
	       printf ("@version %cId%c\n", "$", "$")
	       print "@since Ptolemy II " sinceVersion
	    } else {
	       print $0
            }
         }' <$file >$tmpfile
   
       grep @since $tmpfile >& /dev/null 
       retval=$?
       if [ $retval = 1 ]; then
	   echo "$file still! does not have an @since tag.  Adding a class comment"
	   awk -v author=`$PTII/adm/bin/svnAuthor $file| awk '{print $1}'` -v class=`basename $file .java` -v sinceVersion=$sinceVersion \
          '{  if ($0 ~ /^public/ && sawJavaDocStart == 0 && createdClassComment == 0) {
               createdClassComment = 1
               print "/**"
               print " * " class " class."
               print " *"
               print " * @author " author
	       printf (" * @version %cId%c\n", "$", "$")
	       print " * @since Ptolemy II " sinceVersion
               print " * @Pt.ProposedRating Red (cxh)"
               print " * @Pt.AcceptedRating Red (cxh)"
               print " */"
	       print $0
	    } else {
               if ($0 ~ /^package/) {
                   sawPackage = 1
               } 
               if (sawPackage == 1 && $0 ~ /\/\*\*/) {
                   sawJavaDocStart = 1
               } 
	       print $0
            }
         }' <$file >$tmpfile

       fi
    fi
  else
    echo $file  
    sed -e "s/@since.*/@since Ptolemy II $sinceVersion/" \
	$file > $tmpfile

  fi
  # Don't perform copyright substitution on files that have been copied around. 
#   echo $file | egrep "domains/modal|moml/unit|vergil/modal" >& /dev/null
#   retval=$?
#   if [ $retval = 1 ]; then
#       echo "Updating copyright on $file"
#       sed -e 's/Copyright (c) 200[0-9]-200[0-9]/Copyright (c) 2009/' \
#           -e 's/Copyright (c) 199[0-9]-200[0-9]/Copyright (c) 2009/' \
#           -e 's/Copyright (c) 200[0-7]/Copyright (c) 2009/' \
# 	   < $tmpfile > $tmpfile2
#        diff $file $tmpfile2
#   else
#       echo "Not updating copyright on $file"
       diff $file $tmpfile

#  fi
  # If everything looks alright, then uncomment the cp command below and rerun
  #cp $tmpfile $file
done
