##   Licensed to the Apache Software Foundation (ASF) under one or more
##   contributor license agreements.  See the NOTICE file distributed with
##   this work for additional information regarding copyright ownership.
##   The ASF licenses this file to You under the Apache License, Version 2.0
##   (the "License"); you may not use this file except in compliance with
##   the License.  You may obtain a copy of the License at
## 
##       http://www.apache.org/licenses/LICENSE-2.0
## 
##   Unless required by applicable law or agreed to in writing, software
##   distributed under the License is distributed on an "AS IS" BASIS,
##   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##   See the License for the specific language governing permissions and
##   limitations under the License.

# Check that the local entries exist

use strict;
# TODO create TLP listing automatically,
# e.g. from LDAP ou=pmc,ou=committees,ou=groups,dc=apache,dc=org
open PMC,"<pmc_list.txt" or die "Cannot open pmc_list.txt $!";
# This list is intended to be read-only once created, hence the uppercase name
my %TLPS;
while(<PMC>){
	next if /^#/;
	chomp $_;
	$TLPS{$_} = 1;
}
close PMC;
delete $TLPS{ws} and $TLPS{webservices}=1; # ws => webservices if it existed
delete $TLPS{$_} for qw(security tac); # drop non-TLP

# projects that may still have DOAPS (but not PMCs)
my %ATTIC=map { $_ => 1 } qw(
beehive
excalibur
esme
harmony
xml
jakarta
hivemind
shale
stccxx
);

my %create=%TLPS;
my $pmc;
open IN,"<xdocs/create.xml" or die;
while(<IN>){
    $pmc=1,next if m!<select name="pmc">!;
    $pmc=0,next if m!</select>!;
    next unless $pmc;
    if (m!<option value="([-\w]+)">[^<]+</option>!) {
        delete $create{$1};
    } else {
        print "Could not match create.xml entry: $_" unless m(<!--);
    }
}
close IN;
for(sort keys %create) {
    print "$_ not referenced in xdocs/create.xml\n";
}

my %pmclist=%TLPS;
my %files = map { $_ => 1 } glob("data_files/*.rdf");
open IN,"<pmc_list.xml" or die;
my $last='';
while(<IN>){
      #<location>data_files/couchdb.rdf</location>
      #<location>http://svn.apache.org/repos/asf/cordova/site/infra/doap_Cordova_PMC.rdf</location>
    
    if (m!^\s*<location>([^<]+)</location>!) {
        my $url=$1;
        if ($url =~ m!data_files/!) {
            print "Cannot read $url\n" unless -r $url;
            delete $files{$url}; # we have seen it
            $url =~ s!data_files/!!;
        }
        $url =~ s!https?://svn.apache.org/repos/asf/!!;
        $url =~ s!https?://!!;
        $url =~ s!\.apache\.org!!;
        $url =~ s!git-wip-us/repos/asf\?p=!!;
        $url =~ s!/.*!!;
        $url =~ s!\.rdf$!!;
        print "$url should come before $last\n" if $url le $last;
        delete $pmclist{$url};
        print "pmc_list.xml: did not expect $url\n" unless defined $TLPS{$url};
        $last=$url;
    }
}
close IN;
for(sort keys %files) {
    print "$_ not referenced in pmc_list.xml\n";
}
for(sort keys %pmclist) {
    print "$_ not defined in pmc_list.xml\n";
    next if defined $files{"data_files/$_.rdf"};
    print "Creating data_files/$_.rdf\n";
    open OUT,">data_files/$_.rdf" or die;
    my $Name = ucfirst $_;
    print OUT <<EOD;
<?xml version="1.0"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<rdf:RDF xml:lang="en"
         xmlns="http://usefulinc.com/ns/doap#" 
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
         xmlns:asfext="http://projects.apache.org/ns/asfext#"
         xmlns:foaf="http://xmlns.com/foaf/0.1/">
  <asfext:pmc rdf:about="$_">
    <asfext:name>Apache $Name</asfext:name>
    <foaf:homepage rdf:resource="http://$_.apache.org/"/>
  </asfext:pmc>
</rdf:RDF>
EOD
    close OUT;
}
for(sort keys %pmclist) {
    print "<location>data_files/$_.rdf</location>\n";
}

my %doap=%TLPS;
open DOAP,"<files.xml" or die;
while(<DOAP>){
    #<location>http://svn.apache.org/repos/asf/incubator/droids/doap.rdf</location>
    if (m!^\s*<location>https?://svn\.apache\.org/repos/asf/incubator/([^/]+)/\S+</location>!) {
        print "Warning: TLP entry found under Incubator: $1\n" if exists $TLPS{$1};
        next;
    }
    #<location>http://taverna.incubator.apache.org/doap.rdf</location>
    if (m!^\s*<location>https?://([^.]+)\.incubator\.apache\.org\/\S*</location>!) {
        print "Warning: TLP entry found under Incubator: $1\n" if exists $TLPS{$1};
        next;
    }
    #<location>https://git-wip-us.apache.org/repos/asf?p=incubator-datafu.git;a=blob_plain;f=doap_DataFu.rdf;hb=HEAD</location>
    if (m!^\s*<location>https?://git-wip-us\.apache\.org/repos/asf\?p=incubator-(.+?)\.git;!) {
        print "Warning: TLP entry found under Incubator: $1\n" if exists $TLPS{$1};
        next;
    }    
    #<location>http://svn.apache.org/repos/asf/abdera/java/trunk/doap_Abdera.rdf</location>
    if (m!^\s*<location>https?://svn\.apache\.org/repos/asf/([^/]+)/\S+</location>!) {
        delete $doap{$1};
        print "files.xml:(1) did not expect $1\n" unless defined $TLPS{$1} || defined $ATTIC{$1};
        next;
    }
    #<location>https://git-wip-us.apache.org/repos/asf?p=commmons-xxxx.git;f=doap.rdf;a=blob_plain;hb=HEAD</location>
    if (m!\s*<location>https?://git-wip-us\.apache\.org/repos/asf\?p=(commons)-\w+\.git;!) {
        delete $doap{$1};
        print "files.xml:(2) did not expect $1\n" unless defined $TLPS{$1} || defined $ATTIC{$1};
        next;
    }
    #<location>https://git-wip-us.apache.org/repos/asf?p=curator.git;f=doap.rdf;a=blob_plain;hb=HEAD</location>
    if (m!\s*<location>https?://git-wip-us\.apache\.org/repos/asf\?p=(.+?)(-ivyde|-site|-committers)?\.git;!) {
        delete $doap{$1};
        print "files.xml:(3) did not expect $1\n" unless defined $TLPS{$1} || defined $ATTIC{$1};
        next;
    }
    #<location>http://accumulo.apache.org/doap_Accumulo.rdf</location>
    if (m!^\s*<location>https?://([^.]+)\.apache\.org/\S+</location>!) {
        #print "$1\n";
        delete $doap{$1};
        print "files.xml:(4) did not expect $1\n" unless defined $TLPS{$1} || defined $ATTIC{$1};
        next;
    }
    print "Unexpected files.xml entry: $_" if m!^\s*<location>!;
}
close DOAP;

delete $doap{incubator}; # DOAP does not make sense for Incubator
delete $doap{attic}; # or Attic
 
print "The following TLPs don't seem to have registered their DOAPS:\n";
print join "\n", sort keys %doap;
print "\n";
