#! /usr/bin/env perl

use strict;
use warnings;
use File::Basename;
use File::Path;
use Cwd 'getcwd';

my $miriam_xml = $ARGV[0];
use Cwd 'abs_path';
$miriam_xml = abs_path($miriam_xml);

my $CWD=dirname(${0});
my $DB=basename(${0}, '.pl');
chdir $CWD;
$CWD=getcwd;
my $PWD=dirname($CWD);
my $ESROOT=$PWD;

open(FH, $miriam_xml);

printf "\@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
printf "\@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";

my $flag=0;
my $id = "";
my $name = "";
my $synonym = "";
my $identifiers = "";
my @n = ();
my @s = ();

while(my $line = <FH>){
   if($line =~ /datatype id/){
      ($id) = ( $line =~ /id="(\S+)"/ );
      $flag = 1;
      @n = ();
      @s = ();
      next;
   }
   if($line =~ /<\/datatype>/){
       $flag=0;
       next;
   }
   if($line =~ /<name>/ && $flag ){
      ($name) = ( $line =~ />(.*?)</ );
      push(@n, $name);
      next;
   }
   if($line =~ /<\/synonym>/){
      ($synonym) = ( $line =~ />(.*?)</ );
      push(@s, $synonym);
      next;
   }
   if($line =~ /<uri type="URL">/){
      ($identifiers) = ( $line =~ />(.*?)</ );
      $identifiers =~ s/\/$//;
      printf "<%s> rdfs:seeAlso <http://identifiers.org/miriam.collection/%s> .\n", $identifiers, $id;
      foreach $name (@n){
        printf "<%s> rdfs:label \"%s\" .\n", $identifiers, $name;
      }
      foreach $name (@s){
        printf "<%s> rdfs:comment \"%s\" .\n", $identifiers, $name;
      }
      next;
   }
}

close(FH);
