g.pl: make your scripts GPL compatible
Here is some more code that you might find useful.
I don’t know about you, but when I write a script to do something, I normally just put in the shebang line and get coding, but if I then decide to release it (under the GPL), I have to manually add the “This script has no warranty blah blah blah” heading. This is a pain.
So, in order to stop having to do this, and also to practise a bit of Perl, which I have recently discovered and surprised myself at liking, I wrote a little script to do it for me. Currently it only supports shell-style comments, but as all the scripts I write are in Perl/Python/bash anyway, it’s not really a problem. There is a separate “comment” subroutine, so adding C-style comments (I no longer care about C++) shouldn’t be all that hard.
#!/usr/bin/perl
# g.pl
#
# Add a GPL compliant copyright notice to script files
# Currently only supports shell-style comments (# to EOL)
#
# The notice is added at the end of any initial comments to preserve
# shebang lines and program descriptions (such as this)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
my $notice = <<EOF;
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
EOF
use Getopt::Long;
my $outfile;
my $comment = "#";
GetOptions ("output=s" => \$outfile,
"comment=s" => \$comment);
die "Comment symbol must be one character ($comment given)\n"
if length $comment != 1;
my $out;
if ($outfile) {
open $out, "> $outfile" or die "Can't open $outfile: $!";
else {
open $out, ">-";
}
for (<>) {
print $out quote($notice) if ?^[^#]?;
print $out $_;
}
sub quote {
my $quoted = shift;
$quoted =~ s/^(.*)/$comment $1/mg;
return $quoted;
}
That header was generated by running it on itself ;)
