#!/bin/sh
# Fix the copyright date
#  $Id: fixcopyright 47882 2008-01-24 01:04:23Z cxh $
#
# The way to run this is to use $PTII/adm/bin/ptIItxtfiles to generate
# a list of files and then run this script on that over and over
# again until all the bogus copyrights have been found

# 1. Check out a fresh tree in a separate directory 
#    PtII:  cd ~/tmp; cvs -d :ext:gigasource:/home/cvs co ptII
#    Metro: cd ~/tmp; cvs -d  :ext:ic:/projects/hwsw/hwsw/common/src co metro)
#
# 2. Remove ptII/vendors
#    PtII: cd $PTII; make cleanDerivedJavaFiles clean_shipping
#
# 3. Generate a list of text files
#    cd ptII; $PTII/adm/bin/ptIItxtfiles > /tmp/f
#    Metro: cd metro; $METRO/adm/bin/metrotxtfiles > /tmp/f)
#
# 4. Find files modified since the start of the year
#    find . -type f -a -mtime -`date +%j` | grep -v CVS | sort > /tmp/new
#    sort /tmp/f > /tmp/f.sort
#    comm -12 /tmp/new /tmp/f.sort > /tmp/f.new
#
# 5. Update the script
#    - Update the year regex in the lines marked 'EVERY_YEAR' below
#
# 6. Run the script in testing mode:
#    PtII:  cat /tmp/f.new | xargs $PTII/adm/copyright/fixcopyright -n >& /tmp/fc.txt
#    Metro: cat /tmp/f.new | xargs $METRO/adm/bin/fixcopyright -n >& /tmp/fc.txt
#
# 6. Check the output in /tmp/fc.txt
#
# 7. Run it for real:
#    PtII:  cat /tmp/f.new | xargs $PTII/adm/copyright/fixcopyright >& /tmp/fc2.txt
#    Metro: cat /tmp/f.new | xargs $METRO/adm/bin/fixcopyright >& /tmp/fc2.txt
#
# 8. Do a clean checkout somewhere and rebuild and test
#    cd ~/tmp_2; cvs -d :ext:gigasource:/home/cvs co ptII
#    make fast

printonly=no

while getopts nd-- opt
do
    case $opt in
	d) 	set -x;;
	n) 	printonly=yes;;
	\?)	echo "$0: Usage: $0 [-p] [-x] [filenames . . .]"
		echo " -n  Print only, do not modify any files "
		echo " -d  debug"
		exit 3;;
    esac
done
shift `expr $OPTIND - 1`


search() {
topdir=`pwd`
echo "************ $EGREPMATCH: $topdir"

for fullfile in $@
do
    cd $topdir
    echo "Now processing: $fullfile"
    egrep "$EGREPMATCH" $fullfile
    retval=$?
    if [ $retval = 0 ]; then
	# There was a difference, so we might want to check this sucker out
	file=`basename $fullfile`
	dirname=`dirname $fullfile`
	cd $dirname
	if [ -d SCCS -o -d RCS ]; then
	    echo "Error: SCCS or RCS directory"
	    exit 2
	fi
	doit
	if [ "$printonly" = "no" ]; then
	    cp /tmp/fixcopyright.tmp $file
	    if [ -d CVS ]; then
		cvs commit -m "$LOGMESSAGE" $file 
	    fi	
	else
	    echo "Would update and commit"
	fi	
    fi
done
rm -f /tmp/fixcopyright.tmp
cd $topdir
}

doit() {
    sed 's/Copyright \(\&[^ ]*\) 1998\([ ,]\)/Copyright \1 1998-2007\2/' \
	    $file > /tmp/fixcopyright.tmp	
    diff $file /tmp/fixcopyright.tmp	
}
EGREPMATCH="Copyright &[^ ]* 1998[ ,]"
LOGMESSAGE="Changed copyright to 2007"

search $@

doit() {
    sed 's/\(199[0-8]\)-199[0-9]/\1-2007/' \
	    $file > /tmp/fixcopyright.tmp	
    diff $file /tmp/fixcopyright.tmp	
}
EGREPMATCH="199[0-8]-199[0-9]"
LOGMESSAGE="Changed copyright to 2007"

search $@

# EVERY_YEAR: update the regex to get last-year's changes

doit() {
    sed 's/Copyright (c) \(199[0-9]\)-200[01234567]/Copyright (c) \1-2008/' \
	    $file > /tmp/fixcopyright.tmp	
    diff $file /tmp/fixcopyright.tmp	
}

# EVERY_YEAR: update the regex to get last-year's changes

EGREPMATCH='Copyright \(c\) 199[0-9]-200[01234567]'
LOGMESSAGE="Changed copyright to 2008"

search $@

# EVERY_YEAR: update the regex to get last-year's changes

doit() {
    sed 's/Copyright (c) \(200[01234]\)-200[01234567] /Copyright (c) \1-2008 /' \
	    $file > /tmp/fixcopyright.tmp	
    diff $file /tmp/fixcopyright.tmp	
}

# EVERY_YEAR: update the regex to get last-year's changes

EGREPMATCH='Copyright \(c\) 200[01234]-200[01234567]'
LOGMESSAGE="Changed copyright to 2008"

search $@

doit() {
    sed 's/Copyright (c) \(199[0-9]\) /Copyright (c) \1-2008 /' \
	    $file > /tmp/fixcopyright.tmp	
    diff $file /tmp/fixcopyright.tmp	
}
EGREPMATCH="Copyright \(c\) 199[0-9] "
LOGMESSAGE="Changed copyright to 2008"

search $@

# EVERY_YEAR: update the regex to get last-year's changes

doit() {
    sed 's/Copyright (c) \(200[01234567]\) /Copyright (c) \1-2008 /' \
	    $file > /tmp/fixcopyright.tmp	
    diff $file /tmp/fixcopyright.tmp	
}

# EVERY_YEAR: update the regex to get last-year's changes

EGREPMATCH="Copyright \(c\) 200[01234567] "
LOGMESSAGE="Changed copyright to 2008"

search $@


