use strict;
use warnings;
my $cd2cd = "0.1"; my $dir;
my $extension;
my $inplace = 0;
while(1) {
if(@ARGV && $ARGV[0] eq "--in-place") {
shift @ARGV;
$inplace = 1;
}
else {
last;
}
}
use POSIX qw(strftime);
my @ts;
if(defined($ENV{SOURCE_DATE_EPOCH})) {
@ts = localtime($ENV{SOURCE_DATE_EPOCH});
} else {
@ts = localtime;
}
my $date = strftime "%B %d %Y", @ts;
sub outseealso {
my (@sa) = @_;
my $comma = 0;
my @o;
push @o, ".SH SEE ALSO\n";
for my $s (sort @sa) {
push @o, sprintf "%s.BR $s", $comma ? ",\n": "";
$comma = 1;
}
push @o, "\n";
return @o;
}
sub single {
my @head;
my @seealso;
my ($f)=@_;
my $title;
my $section;
my $source;
my $start = 0;
my $d;
my $line = 0;
my $salist = 0;
my $copyright;
my $spdx;
open(F, "<:crlf", "$f") ||
return 1;
while(<F>) {
$line++;
$d = $_;
if(!$start) {
if(/^---/) {
$start = 1;
push @head, $d;
}
next;
}
if(/^Title: *(.*)/i) {
$title=$1;
}
elsif(/^Section: *(.*)/i) {
$section=$1;
}
elsif(/^Source: *(.*)/i) {
$source=$1;
}
elsif(/^See-also: +(.*)/i) {
$salist = 0;
push @seealso, $1;
}
elsif(/^See-also: */i) {
if($seealso[0]) {
print STDERR "$f:$line:1:ERROR: bad See-Also, needs list\n";
return 2;
}
$salist = 1;
}
elsif(/^ +- (.*)/i) {
if($salist) {
push @seealso, $1;
}
}
elsif(/^C: (.*)/i) {
$copyright=$1;
}
elsif(/^SPDX-License-Identifier: (.*)/i) {
$spdx=$1;
}
elsif(/^---/) {
if(!$title) {
print STDERR "ERROR: no 'Title:' in $f\n";
return 1;
}
if(!$section) {
print STDERR "ERROR: no 'Section:' in $f\n";
return 2;
}
if(!$seealso[0]) {
print STDERR "$f:$line:1:ERROR: no 'See-also:' present\n";
return 2;
}
if(!$copyright) {
print STDERR "$f:$line:1:ERROR: no 'C:' field present\n";
return 2;
}
if(!$spdx) {
print STDERR "$f:$line:1:ERROR: no 'SPDX-License-Identifier:' field present\n";
return 2;
}
last;
}
else {
chomp;
print STDERR "WARN: unrecognized line in $f, ignoring:\n:'$_';"
}
}
if(!$start) {
print STDERR "$f:$line:1:ERROR: no header present\n";
return 2;
}
my @desc;
push @desc, sprintf <<HEAD
---
c: Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
SPDX-License-Identifier: curl
Title: $title
Section: $section
Source: $source
HEAD
;
push @desc, "See-also:\n";
for my $s (sort @seealso) {
push @desc, " - $s\n" if($s);
}
push @desc, "---\n";
my $blankline = 0;
while(<F>) {
$d = $_;
$line++;
if($d =~ /^[ \t]*\n/) {
$blankline++;
}
else {
$blankline = 0;
}
$d =~ s/\*((lib|)curl[^ ]*\(3\))\*/$1/gi;
if(length($d) > 90) {
print STDERR "$f:$line:1:WARN: excessive line length\n";
}
push @desc, $d if($blankline < 2);
}
close(F);
if($inplace) {
open(O, ">$f") || return 1;
print O @desc;
close(O);
}
else {
print @desc;
}
return 0;
}
if($inplace) {
for my $a (@ARGV) {
single($a);
}
}
elsif(@ARGV) {
exit single($ARGV[0]);
}