Newer
Older
#!/usr/bin/perl
# update copyright messgage in source file, sandwiched by CVS operations
# *Please* do this with no files active from cvs and a clean build expected
# mh17 03 Mar 2010
# Copyright(c) 2010 Genome Research Limited
# test data in CopyRight.txt, copy to a header first the run over it (the other file)
# to use on ZMap try:
#
# find ~/zmap/ZMap/src -name "*.c" -exec ./CopyRight \{\} \;
# find ~/zmap/ZMap/src -name "*.h" -exec ./CopyRight \{\} \;
#
use warnings;
my ($file, $date_found, $date_changed,$file_date,$new_date);
my ($ofile);
my ($year_now);
my $CVS = 1; # use the CVS
my $many = 0; # change more than one: good for testing
sub change_date
{
for(@_) # process our one argument directly
{
# our copyright? let's enforce nice capitalised words
if(/Copyright/ && /Genome/)
{
if(m/(\d+)[- ]+(\d+)/)
{
$date_found = 1;
$file_date = $1. '-' .$2;
$new_date = "2006-$year_now";
s/(\d+)[- ]+(\d+)/$new_date/;
}
}
elsif(m/(\d+)/)
{
$date_found = 1;
$file_date = $1;
$new_date = "2006-$year_now";
s/(\d+)/$new_date/;
$date_changed = 1;
print "Date changed from $file_date to $new_date\n";
}
}
}
}
$file = shift @ARGV;
if($file eq "-many")
{
$many = 1;
}
elsif($file eq "-no-CVS")
{
$CVS = 0;
}
$file = shift @ARGV; # we take one file arg only
if(!$file) { die("No file!\n"); }
$new_date = $year_now = $time[5] + 1900;
$file_date = "??";
$date_found = 0;
$date_changed = 0;
$lineno = 0;
if($CVS)
{
@args = ("cvs", "edit", $file);
# test if writeable: not sure if CVS return values are good
system(@args);
if(!(-w $file))
{
die("Cannot checkout $file from CVS - not writeable\n");
}
}
open(IFILE,$file) or die("cannot open $file\n");
$ofile = $file . ".tmp"; # make tmp file in same directory
$year_now = 2010;
open(OFILE,"> ".$ofile) or die("cannot open $ofile\n");
while($line = <IFILE>)
{
$lineno++;
if($lineno < 50 && (!$date_found || $many))
{
change_date($line);
}
print OFILE $line;
}
close $ofile;
if(!$date_found)
{
print "No copyright date found\n";
}
if($date_changed)
{
unlink $file or die ("Can't remove old file\n");
rename($ofile,$file) or die("Can't update file\n");
if($CVS)
{
@args = ("cvs", "commit", "-m", "CopyRight date set", $file);
system(@args);
if(-w $file)
{
die("Copyright date set but cannot commit $file to CVS: still writeable\n");
}
if($date_found)
{
print "Copyright date was $file_date: not changed\n";
}
if($CVS)
{
@args = ("cvs", "unedit", $file);
die("Can't CVS unedit $file: still writeable\n");