changeCopyright.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/perl
  2. use Fcntl;
  3. if ($#ARGV != 0) {
  4. print "usage:\n $0 <source_file>\n";
  5. exit(0);
  6. }
  7. my $source_file = @ARGV[0];
  8. my $min_year = 2013;
  9. my $index = 0;
  10. my $copyright_notice_end_line = 0;
  11. open(SOURCE_FILE, "$source_file") || die "Cannot open file $source_file\n";
  12. while (<SOURCE_FILE>) {
  13. if ($index == 0) {
  14. /^\/\*/ || die "File $source_file doesn't contain copyright notice\n";
  15. } elsif (/\*\// && $copyright_notice_end_line == 0) {
  16. $min_year < 2013 || die "File $source_file doesn't contain copyright year information\n";
  17. $copyright_notice_end_line = $index + 1;
  18. }
  19. if (/Copyright \(C\)/) {
  20. my $year = $_;
  21. $year =~ s/.+Copyright \(C\) ([^ -]+).+\n/\1/;
  22. if ($year < $min_year) {
  23. $min_year = $year;
  24. }
  25. }
  26. ++$index;
  27. }
  28. $copyright_notice_end_line > 0 || die "File $source_file doesn't contain copyright notice\n";
  29. $years = ($min_year == 2010) ? 2010 : "$min_year-2010";
  30. open(TMP_FILE, ">TMP");
  31. open(COPYRIGHT_FILE, "./tools/copyright");
  32. while (<COPYRIGHT_FILE>) {
  33. s/YEARS/$years/;
  34. print TMP_FILE $_;
  35. }
  36. close(COPYRIGHT_FILE);
  37. $index = 0;
  38. seek(SOURCE_FILE, 0, SEEK_SET);
  39. while (<SOURCE_FILE>) {
  40. if (++$index > $copyright_notice_end_line) {
  41. print TMP_FILE $_;
  42. }
  43. }
  44. close(SOURCE_FILE);
  45. close(TMP_FILE);
  46. rename("TMP", "$source_file");