#!/usr/bin/env perl

use strict;
use CGI qw(standard);

my $q = new CGI;

print <<EOF;
Content-type: application/rdf+xml
Content-Disposition: attachment; filename=module.rdf

<?xml version="1.0"?>
<rdf:RDF xml:lang="en"
         xmlns="http://modules.httpd.apache.org/ns/doam#"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:foaf="http://xmlns.com/foaf/0.1/" >
<!--
  =======================================================================
   Generated by Module-A-Matic CGI
   Please modify to meet your exact requirements!
  =======================================================================
-->
  <Module>
EOF

my @params = (
    [ 'txtModuleName', '<name>%s</name>' ],
    [ 'txtModuleDescription', '<description>%s</description>' ],
    [ 'txtModuleWebsite', '<website rdf:resource="%s" />' ],
    [ 'txtModuleURL', '<download rdf:resource="%s" />'],
    [ 'lsLicence', '<licence rdf:resource="%s" />' ],
);
foreach my $p (@params) {
    my $val = $q->param($p->[0]);
    if ($val) {
        print "    ";
        printf($p->[1], $val);
        print "\n";
    }
}

if ($q->param('authname')) {
    print <<EOF;
    <Author>
      <foaf:Person>
EOF
    my @params = (
        [ 'txtModuleAuthor', '<foaf:name>%s</foaf:name>' ],
        [ 'txtModuleEmail', '<foaf:mbox>%s</foaf:mbox>' ],
    );
    foreach my $p (@params) {
        my $val = $q->param($p->[0]);
        if ($val) {
            print "        ";
            printf($p->[1], $val);
            print "\n";
        }
    }
    print <<EOF;
      </foaf:Person>
    </Author>
EOF
}
    print <<EOF;
    <Dependancies>
      <external>
EOF
my $nExt = $q->param('extDepCount');
for (my $i; $i <= $nExt; $i++) {
    my $dep = $q->param("txtExDep_".$i);
    my $ver = $q->param("txtExDepVer_".$i);
    next unless $dep;
    print <<EOF;
        <dependancy>
          <name>$dep</name>
          <version>$ver</version>
        </dependancy>
EOF
}
    print <<EOF;
      </external>
      <internal>
EOF

my $nInt = $q->param('intDepCount');
for (my $i; $i <= $nExt; $i++) {
    my $dep = $q->param("txtInDep_".$i);
    next unless $dep;
    print <<EOF;
        <dependancy>
          <name>$dep</name>
        </dependancy>
EOF
}
    print <<EOF;
      </internal>
    </Dependancies>
    <Versions>
EOF

if ($q->param('verText')) {
    my $ver = $q->param('verText');
    print "      <Version id=\"$ver\">\n";
    my $httpdVer = $q->param('version');
    print "        <httpdVersion>$httpdVer</httpdVersion>\n";
    my $min = $q->param('minVer');
    my $max = $q->param('maxVer');
    print "        <minVersion>$min</minVersion>\n" if $min;
    print "        <maxVersion>$max</maxVersion>\n" if $max;
    print "      </Version>\n";
}
print "    </Versions>\n";

if ($q->param('authname')) {
    print <<EOF;
    <Author>
      <foaf:Person>
EOF
    my @params = (
        [ 'authname', '<foaf:name>%s</foaf:name>' ],
        [ 'authemail', '<foaf:mbox>%s</foaf:mbox>' ],
    );
    foreach my $p (@params) {
        my $val = $q->param($p->[0]);
        if ($val) {
            print "        ";
            printf($p->[1], $val);
            print "\n";
        }
    }
    print <<EOF;
      </foaf:Person>
    </Author>
EOF
}

print <<EOF;
  </Module>
</rdf:RDF>
EOF

