####################################################################################################
#                                                                                                  #
# (C) All rights reserved by ROBERT BOSCH GMBH, STUTTGART                                          #
#                                                                                                  #
####################################################################################################
#                                                                                                  #
#    __   __   ___  __                                                                             #
#   /_ / /  / /__  /    /__/                                                                       #
#  /__/ /__/ __ / /__  /  /                                                                        #
#                                                                                                  #
#                                                                                                  #
####################################################################################################

# author: Martin Kandler
# version: 1.1



# ********** global variables **********************************************************************

$versionFile = "version.xml";
$outputFile = "version_customer.html";
%data = undef;


# ********** MAIN PROGRAM **************************************************************************

if(@ARGV == 2)
{
    $versionFile = $ARGV[0];
    $outputFile = $ARGV[1];
}
elsif(@ARGV != 0)
{
    die "\nversion documentation script\n\nuse: .>perl version_customer.pl [<version.xml>] [<output.html>]\n";
}

if(!(-f $versionFile))
{
    die "\nERROR version.xml file not found: '$versionFile'\n";
}
if(-f $outputFile)
{
    die "\nERROR output.html file allready exists: '$outputFile'\n";
}


$fileContent = readFileIntoString($versionFile);

print "input file read\n";

# remove comments
$fileContent =~ s/\<\!\-\-.*?\-\-\>//g;

@versions = $fileContent =~ /<TOOL-VERSION>.+?<\/TOOL-VERSION>/g;

foreach $version (@versions)
{
  @allchanges = undef;
  my @v;

  @v = $version =~ /<VERSION>(.+?)<\/VERSION>/g;

  $versionNumber = @v[0];

  @customerDoc = $version =~ /<CUSTOMER>.+?<\/CUSTOMER>/g;

  foreach $entry (@customerDoc)
  {
    (@changes) = $entry =~ /<CHANGE>(.+?)<\/CHANGE>/g;
    push(@allchanges, @changes);
    push(@allchanges, "<!-- next change --->");
  }

  pop(@allchanges);

  $data{$versionNumber}{version} = $versionNumber;

  my $i = 0;

  foreach $change (@allchanges)
  {
    $line = sprintf("%04d", $i);
    $data{$versionNumber}{changes}{$line} = $change;
    $i ++;
  }

}

print "input file parsed\n";

writeCustomerDoc($outputFile, \%data);

print "output file written\n";

# ********** END main program **********************************************************************


# ********** SUB PROCESSES *************************************************************************

sub readFileIntoString
{
  my $f = $_[0];
  my $fileContent = "";

  open(readFile, $f) or die "\nERROR cannot open file: '$f'\n";

  while(<readFile>)
  {
    chomp($_);
    $fileContent .= $_;
  }

  close readFile;

  return $fileContent;
}



sub writeCustomerDoc
{
  my $f = $_[0];
  my %h = %{$_[1]};

  open(writeFile, ">$f") or die "\nERROR cannot write file: '$f'\n";

  print writeFile "<html>\n<head>\n<title>VWAudi Use Case Versionhistory</title>\n</head>\n\n<body bgcolor=\"#ffffff\">\n\n";

  foreach $version (sort {$b cmp $a} keys(%h))
  {
    if($h{$version}{version} ne "")
    {
      $index = 1;
      print writeFile "<p><font size=\"+1\" color=\"#000088\">Version: $h{$version}{version}</font></p>\n\n<table border=\"0\" cellspacing=\"10\" align=\"center\" width=\"98%\">\n  <tr>\n    <td align=\"right\" valign=\"top\">$index.</td>\n    <td>\n";

      my $a = $h{$version}{changes};

      foreach $change (sort keys(%$a))
      {
        if($h{$version}{changes}{$change} ne "")
        {
          if($h{$version}{changes}{$change} eq "<!-- next change --->")
          {
            $index ++;
            print writeFile "    </td>\n  </tr>\n  <tr>\n    <td align=\"right\" valign=\"top\">$index.</td>\n    <td>\n";
          }
          else
          {
            print writeFile "$h{$version}{changes}{$change}<br>\n";
          }
        }
      }

      print writeFile "    </td>\n  </tr>\n</table>\n\n";
    }
  }

  print writeFile "</body>\n</html>\n";

  close writeFile;

}
