1#!/usr/bin/perl -i 2# 3# build/replace_license_blocks file ... 4# 5# Replace a <@LICENSE> ... </@LICENSE> delimited block of comment text inside 6# an arbitrary number of files with the contents of the file named 7# 'newlicense'. The comment character to use at start-of-line is read 8# from the file being worked on, e.g. 9# 10# * <@LICENSE> 11# 12# will result in all lines of the license text being prefixed with " * ". 13# 14# do something like the following: 15# - create a "newlicense" file with the license text as appropriate 16# - run something like: 17# grep -rl @LICENSE * | egrep -v 'replace_license_blocks|\.svn' | xargs build/replace_license_blocks 18# 19 20# read the new license text; die if not available 21open (IN, "<newlicense") or die "cannot read 'newlicense'"; 22my $license = join ('', <IN>); 23close IN; 24 25# remove final comment if present 26$license =~ s/\n \*\/$//gs; 27# remove C comment start-of-line markers 28$license =~ s/^(?:\/\* | \* | \*\/| \*)//gm; 29 30# and fixup in-place 31$in_license_block = 0; 32 33while (<>) { 34 if ($in_license_block) { 35 # we're waiting until the end-of-license "closing tag" 36 if (s/^.+<\/\@LICENSE>//) { 37 $in_license_block = 0; 38 } 39 } else { 40 if (s{^(.+)<\@LICENSE>\s*$}{ license_with_prefix($1); }eg) { 41 $in_license_block = 1; 42 } else { 43 # a non-block form -- will be replaced with a block 44 s{^(.+)\@LICENSE\s*$}{ license_with_prefix($1); }eg; 45 } 46 print; 47 } 48} 49 50sub license_with_prefix { 51 my $prefix = shift; 52 53 # if the prefix is "/*" replace with " *" 54 my $orig = $prefix; 55 $prefix =~ s@^(\s*)/\*(.*)$@$1 *$2@; 56 57 my $text = $license; $text =~ s/^/$prefix/gm; 58 59 return $orig."<\@LICENSE>\n". 60 $text. 61 $prefix."</\@LICENSE>\n"; 62} 63