#!/usr/bin/perl 

use strict;
use warnings;

use Data::Dumper;

my $data = {};

{
        my $skip = <>;
        while( my $line = <> )
        {
                $line =~ s/\t\t//g; # remove end bits and odd nesting
                $line =~ s/[\n\r]//g;
                my @parts = split( /\t/, $line );
                if( scalar @parts % 2 != 0 ) { die "odd number of values on line"; }
        
                my @row = ();
                my $name;
                my $id;
                my $prev = "UOS";
                while( scalar @parts )
                {
                        ($name,$id,@parts) = @parts;
                        $data->{$id}->{name} = $name;
                        $data->{$prev}->{child}->{$id} = 1;
                        $prev = $id;
                }
                
        }
}


print <<END;
<?xml version="1.0" encoding='utf-8'?>
<rdf:RDF

  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"

  xmlns:foaf='http://xmlns.com/foaf/0.1/'
  xmlns:org="http://www.w3.org/ns/org#"
  xmlns:rooms="http://vocab.deri.ie/rooms#"
  xmlns:skos="http://www.w3.org/2004/02/skos/core#"
  xmlns:soton="http://id.southampton.ac.uk/ns/"
  xmlns:owl="http://www.w3.org/2002/07/owl#"
>
END
foreach my $org_id ( keys %$data )
{
	my $org = $data->{$org_id};
	my $uri = uri( $org_id );
	my $hr_uri = "http://id.southampton.ac.uk/org/$org_id";
	print "<org:Organization rdf:about='$uri'>\n";
	if( defined $org->{name} ) 
	{
		print "  <rdfs:label><![CDATA[".$org->{name}."]]></rdfs:label>\n";
	}
        print "  <skos:notation rdf:datatype='http://id.southampton.ac.uk/ns/10CharHRCode'>$org_id</skos:notation>\n";
	if( $uri =~ m/([A-Z][A-Z0-9])$/ )
	{
        	print "  <skos:notation rdf:datatype='http://id.southampton.ac.uk/ns/alphaCode'>$1</skos:notation>\n";
	}
	if( $uri =~ m/([A-Z][A-Z])$/ )
	{
		print "  <owl:sameAs rdf:resource='$hr_uri' />\n";
	}
	if( $org->{child} )
	{
		foreach my $kid_id ( keys %{$org->{child}} )
		{
			print "  <org:hasSubOrganization rdf:resource='".uri($kid_id)."' />\n";
		}
	}
	print "</org:Organization>\n";
}
print "
    </rdf:RDF>
";
exit;

sub uri
{
	my( $hr ) = @_;

	if( $hr eq "UOS" ) { return "http://id.southampton.ac.uk/"; }
	if( $hr =~ /^(..)00000000/ ) { return "http://id.southampton.ac.uk/org/$1"; }
	if( $hr =~ /^..([A-Z][A-Z])000000/ ) { return "http://id.southampton.ac.uk/org/$1"; }
	return "http://id.southampton.ac.uk/org/$hr";
}

__DATA__
        <org:Organization rdf:about="http://id.southampton.ac.uk/">

          <xsl:for-each select="hr:row">
            <org:hasSubOrganization>
              <org:Organization 
                  rdf:about="http://id.southampton.ac.uk/org/{substring(hr:pers-str-level-2-id,1,2)}">
                <xsl:if test="substring(hr:pers-str-level-2-id,1,1) = 'F'">
                  <rdfs:label>Faculty of <xsl:value-of select="hr:pers-str-level-2-long-desc" /></rdfs:label>
                  <rdf:type rdf:resource="http://purl.org/vocab/aiiso/schema#Faculty" />
                </xsl:if>
                <xsl:if test="substring(hr:pers-str-level-2-id,1,1) != 'F'">
                  <rdfs:label><xsl:value-of select="hr:pers-str-level-2-long-desc" /></rdfs:label>
                </xsl:if>
                <xsl:if test="hr:pers-str-level-3-long-desc/text()">
                  <org:hasSubOrganization>
                  <org:Organization 
                      rdf:about="http://id.southampton.ac.uk/org/{substring(hr:pers-str-level-3-id, string-length(substring-before(hr:pers-str-level-3-id,'0'))-1 ,2)}"
                      rdfs:label="{hr:pers-str-level-3-long-desc}">
                    <skos:notation rdf:datatype="http://id.southampton.ac.uk/ns/alphaCode"><xsl:value-of select="substring(hr:pers-str-level-3-id, string-length(substring-before(hr:pers-str-level-3-id,'0'))-1 ,2)" /></skos:notation>
                    <skos:notation rdf:datatype="http://id.southampton.ac.uk/ns/10CharHRCode"><xsl:value-of select="hr:pers-str-level-3-id" /></skos:notation>
                    <xsl:if test="hr:pers-str-level-4-long-desc/text()">
                      <org:hasSubOrganization>
                      <org:Organization 
                          rdf:about="http://id.southampton.ac.uk/org/{hr:pers-str-level-4-id}"
                          rdfs:label="{hr:pers-str-level-4-long-desc}">
                        <skos:notation rdf:datatype="http://id.southampton.ac.uk/ns/10CharHRCode"><xsl:value-of select="hr:pers-str-level-4-id" /></skos:notation>
                        <xsl:if test="hr:pers-str-level-5-long-desc/text()">
                          <org:hasSubOrganization>
                          <org:Organization 
                              rdf:about="http://id.southampton.ac.uk/org/{hr:pers-str-level-5-id}"
                              rdfs:label="{hr:pers-str-level-5-long-desc}">
                            <skos:notation rdf:datatype="http://id.southampton.ac.uk/ns/10CharHRCode"><xsl:value-of select="hr:pers-str-level-5-id" /></skos:notation>
                            <xsl:if test="hr:pers-str-level-6-long-desc/text()">
                              <org:hasSubOrganization>
                              <org:Organization 
                                  rdf:about="http://id.southampton.ac.uk/org/{hr:pers-str-level-6-id}"
                                  rdfs:label="{hr:pers-str-level-6-long-desc}">
                                <skos:notation rdf:datatype="http://id.southampton.ac.uk/ns/10CharHRCode"><xsl:value-of select="hr:pers-str-level-6-id" /></skos:notation>
                              </org:Organization>
                              </org:hasSubOrganization>
                            </xsl:if>
                          </org:Organization>
                          </org:hasSubOrganization>
                        </xsl:if>
                      </org:Organization>
                      </org:hasSubOrganization>
                    </xsl:if>
                  </org:Organization>
                  </org:hasSubOrganization>
                </xsl:if>
              </org:Organization>
            </org:hasSubOrganization>
          </xsl:for-each>
        </org:Organization>

    </rdf:RDF>
  </xsl:template>

</xsl:stylesheet>
