#!/usr/bin/perl

use Modern::Perl '2015';

my $tosfile = '/opt/repository/sources/dspace/dspace-xmlui/src/main/webapp/themes/UFAL/lib/html/terms-of-service.html';
my $outfile = '/opt/repository/sources/dspace/dspace-xmlui/src/main/webapp/themes/UFAL/lib/html/terms-of-service.html.new';
my $locpropfile = '/opt/repository/sources/dspace/local.properties';

my ($repo_name,$centre_legal_entity,$full_address,$country,$city);

open (my $tos, '<', $tosfile);
open (my $out, '>', $outfile);
open (my $lp, '<', $locpropfile);

while (my $in = <$lp>)
{
	if ($in =~ /^dspace\.name/)
	{
		(undef,$repo_name) = split /=/, $in;
		chomp $repo_name;
		$repo_name =~ s/^\s//;
	}
	elsif ($in =~ /^lr.description.institution /)
	{
		(undef,$centre_legal_entity) = split /=/, $in;
		chomp $centre_legal_entity;
		$centre_legal_entity =~ s/^\s//;
	}
	elsif ($in =~ /^lr.description.location/)
	{
		(undef,$full_address) = split /=/, $in;
		chomp $full_address;
		$full_address =~ s/^\s//;
	}
	elsif ($in =~ /^lr.description.shortLocation/)
	{
		(undef,my $location) = split /=/, $in;
		chomp $location;
		($city,$country) = split /,/,  $location;
		$city =~ s/^\s//;
		$country =~ s/^\s//;
	}
}

while (my $line = <$tos>)
{
	if ($line =~ /REPOSITORY_NAME/)
	{
		$line =~ s/\$REPOSITORY_NAME/$repo_name/;
	}
	if ($line =~ /A_CENTRE_OR_UNIT_WITH_LEGAL_PERSONALITY/)
	{
		$line =~ s/\$A_CENTRE_OR_UNIT_WITH_LEGAL_PERSONALITY/$centre_legal_entity/;
	}
	if ($line =~ /FULL_ADDRESS/)
	{
		$line =~ s/\$FULL_ADDRESS/$full_address/;
	}
	if ($line =~ /COUNTRY/)
	{
		$line =~ s/\$COUNTRY/$country/;
	}
	if ($line =~ /CITY/)
	{
		$line =~ s/\$CITY/$city/;
	}
	print $out $line;
}

	#print "-$repo_name--$centre_legal_entity--$full_address--$city--$country-\n";


__DATA__
$REPOSITORY_NAME							dspace.name
$A_CENTRE_OR_UNIT_WITH_LEGAL_PERSONALITY	lr.description.institution
$FULL_ADDRESS								lr.description.location
$COUNTRY									lr.description.shortLocation[1]
$CITY										lr.description.shortLocation[0]