##   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.

# Create new TLP entries
# - create RDF file
# - update pmc_list.xml to add references to the new RDF files
# - update xdocs/create.xml to add new TLP names

# Input file: newtlp.tmp
# Lines consist of: TLP Name[,tlpid]
# If not specified, the tlp id is derived by lower-casing the TLP Name.
# For example:
# Allura
# Open Climate Workbench,climate

use strict;

my $REloc1=qr(location>data_files/([^\.]+)\.rdf</location>);
my $REloc2=qr(location>http://svn\.apache\.org/repos/asf/([^/]+)/);
my $REloc3=qr(location>http://([^\.]+)\.apache\.org/);

sub seekpmc {
    local $_;
    my $seek=shift;
    our $pmcseen;
    if (defined $pmcseen && $pmcseen gt $seek) {
        return;
    }
    my $tell = tell(PMCIN);
    while(<PMCIN>) {
        if (m!</list>!) { # EOF
            seek(PMCIN, $tell, 0);
            $pmcseen='zzzzzzzz'; # flag EOF
            return;
        }
        if (/$REloc1/ or /$REloc2/ or /$REloc3/) {
            $pmcseen=$1;
            seek(PMCIN, $tell, 0),return if $pmcseen gt $seek;
        } elsif (/^\s*<!--/) {
        } else {
            warn "??: $_"; # Unexpected data line
        }
        print PMCOUT $_;
        $tell = tell(PMCIN); # Restart from here if required
    }
}

my $REform = qr(^\s+<option value="([^"]+)">);

sub seekform {
    local $_;
    my $seek=shift;
    our $formseen;
    if (defined $formseen && $formseen gt $seek) {
        return;
    }
    my $tell = tell(FORMIN);
    while(<FORMIN>) {
        if (m!</select>!) { # End of section
            seek(FORMIN, $tell, 0);
            $formseen='zzzzzzzz'; # flag EOS
            return;
        }
        if (/$REform/) {
            $formseen=$1;
            seek(FORMIN, $tell, 0),return if $formseen gt $seek;
        }
        print FORMOUT $_;
        $tell = tell(FORMIN); # Restart from here if required
    }
}

my %tlp=();
open IN,"<newtlp.tmp" or die;
while(<IN>){
    next if m!^#!; # skip comments
    chomp;
    my ($Tlp,$tlp)=split /,/;
    $tlp ||= lc $Tlp;
    die "$tlp RDF file already exists; check input file\n" if -r "data_files/${tlp}.rdf";
    $tlp{$tlp}=$Tlp;
}
close IN;
my $PMC = "pmc_list.xml";
my $FORM = "xdocs/create.xml";
open PMCIN, "<$PMC" or die;
open PMCOUT,">$PMC.tmp" or die;
open FORMIN, "<$FORM" or die;
open FORMOUT,">$FORM.tmp" or die;
while(<PMCIN>) { print PMCOUT; last if m/^<list>/; }
while(<FORMIN>) { print FORMOUT; last if m/^ +<select name="pmc">/; }
for my $key (sort keys %tlp) {
    seekpmc($key);
    seekform($key);
    print "$tlp{$key} => $key\n";
    create($tlp{$key}, $key);
}
while(<PMCIN>) { print PMCOUT; }
while(<FORMIN>) { print FORMOUT; }
close PMCIN;
close PMCOUT;
close FORMIN;
close FORMOUT;
rename("$PMC.tmp",$PMC);
rename("$FORM.tmp",$FORM);
sub create {
    my $Tlp = shift;
    my $tlp = shift;
    print PMCOUT  qq(  <location>data_files/${tlp}.rdf</location>\n);
    print FORMOUT qq(        <option value="${tlp}">${Tlp}</option>\n);
    open OUT,">data_files/${tlp}.rdf" or die;
    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="${tlp}">
    <asfext:name>Apache ${Tlp}</asfext:name>
    <foaf:homepage rdf:resource="http://${tlp}.apache.org/"/>
  </asfext:pmc>
</rdf:RDF>
EOD
    close OUT;
}
