﻿#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);


# Filter_3_05_add_basic_xml.pl
# 2010-04-12
# Wolfgang Schmidle

# what does it do?
# input
# output


my $identifier = "";
my $creator = "";
my $contributor = "";
my $title = "";
my $date = "";
my $language = "";
my $echolink = "";
my $echodir = "";

my $textLang = "xx";
my $textType = "free";


# text input

my @text;
while(<>) { push @text, $_; }


# read default parameters

my $filter = "mpdl-workflow-settings.pl";
my $result = `perl $filter`;

my $urlBase = ($result =~ m! urlBase=([^ ]+) ! ? $1 : "" );


# evaluate and remove the "metadata" block

my $inMetadata = 0;

foreach (@text) {
	last if m!<pb[ ^]!;	
	if (m!metadata:!) { 
		$inMetadata = 1;
		$_ = "";
		next;
	}
	if ($inMetadata) {
		if (m!^ *$!) {
			$_ = "";
			last;
		}

		chomp;
		if (m!dcterms:identifier +(.+)!) { 
			$identifier = "<dcterms:identifier>$1</dcterms:identifier>";
		}
		if (m!dcterms:(creator|contributor) +\((.+?)\) +(.+)!) { 
			$creator .= ($creator ? "\n    ": "");
			$creator .= "<dcterms:$1 identifier=\"$2\">$3</dcterms:$1>"; 
			$_ = ""; 
			next;
		}
		if (m!dcterms:(creator|contributor) +(.+)!) { 
			$creator .= ($creator ? "\n    ": "");
			$creator .= "<dcterms:$1>$2</dcterms:$1>"; 
		}
		if (m!dcterms:(title|alternative) +\((.+?)\) +(.+)!) { 
			$title .= ($title ? "\n    ": "");
			$title .= "<dcterms:$1 xml:lang=\"$2\">$3</dcterms:$1>";
		}
		if (m!dcterms:date +(.+)!) { 
			$date .= ($date ? "\n    ": "");
			$date .= "<dcterms:date xsi:type=\"dcterms:W3CDTF\">$1</dcterms:date>";
		}
		if (m!dcterms:language +(.+)!) { 
			$language .= ($language ? "\n    ": "");
			$language .= "<dcterms:language xsi:type=\"dcterms:ISO639-3\">$1</dcterms:language>";
		}
		if (m!echolink +(.+)!) { 
			$echodir = "\n    <echolink>$1</echolink>"; 
		}
		if (m!echodir +(.+)!) { 
			$echodir = "\n    <echodir>$1</echodir>"; 
		}
		if (m!text +\((.+?)\) +(.+)!) { 
			$textLang = $1; $textType = $2; 
		}
		if (m!URLbase +(.+)!) { 
			$urlBase = $1; 
		}

		$_ = "";
	}
}

unless ($identifier) { $identifier = "<dcterms:identifier>ECHO:xxxxxxxx.xml</dcterms:identifier>"; }
unless ($creator) { $creator = "<dcterms:creator>xxx, xxx</dcterms:creator>"; }
unless ($title) { $title = '<dcterms:title xml:lang="xx">xxx</dcterms:title>'; }
unless ($date) { $date = '<dcterms:date xsi:type="dcterms:W3CDTF">xxxx</dcterms:date>'; }
unless ($language) { $language = '<dcterms:language xsi:type="dcterms:ISO639-3">xxx</dcterms:language>'; }


# evaluate and remove the "parameters" block

my $parameters = "";
my $inParameters = 0;

foreach (@text) {
	last if m!<pb[ ^]!;	
	if (m!parameters:!) { 
		$inParameters  = 1; 
		$_ = "";
		next;
	}
	if ($inParameters) {
		if (m!^ *$!) {
			$_ = "";
			last;
		}
		$parameters .= $_;
		$_ = "";
	}
}

chomp $parameters;
$parameters =~ s!\n!\n      !g;

if ($parameters ne "") { $parameters = "\n    <parameters>$parameters</parameters>"; }


# evaluate and remove the "log" block

my $log = "";
my $inLog = 0;

foreach (@text) {
	last if m!<pb[ ^]!;	
	if (m!log:!) { 
		$inLog = 1; 
		$_ = "";
		next;
	}
	if ($inLog) {
		if (m!^ *$!) {
			$_ = "";
			last;
		}
		$log .= $_;
		$_ = "";
	}
}

chomp $log;
$log =~ s!\n!\n      !g;

$log =~ s!&!&amp;!g;
$log =~ s!<!&lt;!g;
$log =~ s!>!&gt;!g;

if ($log ne "") { $log = "\n    <log>$log</log>"; }


# text output

print <<"PREAMBLE";
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE echo [

<!ATTLIST head         xml:space  (preserve)  #FIXED "preserve">
<!ATTLIST s            xml:space  (preserve)  #FIXED "preserve">
<!ATTLIST caption      xml:space  (preserve)  #FIXED "preserve">
<!ATTLIST description  xml:space  (preserve)  #FIXED "preserve">
<!ATTLIST variables    xml:space  (preserve)  #FIXED "preserve">

<!ENTITY  URLbase "$urlBase">
]> 

<echo xmlns="http://www.mpiwg-berlin.mpg.de/ns/echo/1.0/"
  xmlns:de="http://www.mpiwg-berlin.mpg.de/ns/de/1.0/"
  xmlns:dcterms="http://purl.org/dc/terms"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:echo="http://www.mpiwg-berlin.mpg.de/ns/echo/1.0/"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:mml="http://www.w3.org/1998/Math/MathML"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  version="1.0RC">
  <metadata>
    $identifier
    $creator
    $title
    $date
    $language
    <dcterms:rights>open access</dcterms:rights>
    <dcterms:license>http://echo.mpiwg-berlin.mpg.de/policy/oa_basics/declaration</dcterms:license>
    <dcterms:accessRights>free</dcterms:accessRights>$echolink$echodir$parameters$log
  </metadata>
  <text xml:lang="$textLang" type="$textType">
PREAMBLE

print @text;

print "  </text>\n</echo>\n";


# TO DO:
# das spezifische Wissen über einzelne Metadaten-Einträge ist irgendwo im Skript versteckt. 
# --> Hilfsdatei verwenden? Oder einfach umstrukturieren?

# weitere links, etc.

