#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use open qw/:std :utf8/;

use Encode;
use Getopt::Long;

my ($programme) = $0 =~ m|^(?:.*/)?(.+)|;
$programme = decode_utf8($programme);

my $metadata = ""; 
my $sortie = ""; 

eval	{
	$SIG{__WARN__} = sub {usage(1);};
		GetOptions(
		"metadata=s"      => \$metadata,
		"sortie=s"         => \$sortie,
		);
	};
$SIG{__WARN__} = sub {warn $_[0];};

usage(2) if not $metadata or not $sortie;

open(META, "<:utf8", $metadata) or die "Couldn't open file \"$metadata\", $!";
open(SORTIE, "+<:utf8", $sortie) or die "Couldn't open file \"$sortie\", $!";

foreach my $ligne (<META>)
{
	chomp($ligne);
	if   ($ligne =~ /^DT : (.*)/)
		{
			print SORTIE "**** *DT_";
			my $dt = $1;
			$dt =~ s/ ; /_/g;
			print SORTIE $dt;
			print SORTIE " *SO_"
			#my $dt = $1;
			#print SORTIE "**** *DT_"
			#print SORTIE $dt;
		}
	elsif ($ligne =~ /^SO : (.*?) ;/)
		{
			#$valeurs[1] = "$1";
			#print SORTIE " *SO_"
			#print SORTIE $valeurs[1];
			my $so = $1;
			$so =~ s/ /_/g;
			print SORTIE $so;
			print SORTIE " *LA_"
		}
	elsif ($ligne =~ /^LA : (.*)/)
		{
			#$valeurs[2] = "$1";
			#print SORTIE " *LA_"
			#print SORTIE $valeurs[2];
			my $la = $1;
			$la =~ s/ /_/g;
			print SORTIE $la;
			print SORTIE "\n";
		}
}

close META;
close SORTIE;

exit 0;

sub usage
{
my $code = shift;

print STDERR "Usage : $programme -m metadata -s sortie \n";

exit $code;
}
