#!/usr/bin/perl
#
# $Id$
#
# PBA syncchangelogs program
#
# (C) IBCL Informatik-Buero e.U. 2010
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

use strict;

require "/usr/lib/pba/pba-functions.pl";
require "/usr/lib/pba/devtools-functions.pl";

$main::scriptName = "pba-syncchangelogs";

$main::helpMessage = '
 Syntax:
  '.$main::scriptName.' [-o selector]

 Analyses and synchronizes changelog infos, i.e. package versions,
 among debian*/changelog, spec*/*.spec.

 Options are:
  -c configdir   Read configdir/* rather than /etc/pba/*
  -d             Debug mode, logs everything.
  -i             Install dependecies by calling sudo apt-get install
  -o selector    Restrict to Only debian.*selector/changelog, spec.*selector/*.spec
  -q             Quiet mode, supresses informative messages.
  -v             Verbose verboser, logs more.
';
#-f configfile    Read /etc/pba/configfile rather than /etc/pba/builder.conf

$main::optionList = "dc:io:qv";

prepareGetopts();

initPBAFunctions();

initDevToolsFunctions();

parseConfig();

# no need to check GPG key or DefaultBuildHost
#checkDevToolsConfig();

readDebianFiles();
readSpecFiles();

checkPackageVersions();

my $nrChangelogFiles = scalar(@main::changelogFiles);
my $nrSpecFiles = scalar(@main::specFiles);
if ($main::versionDifferences == 0) {
  print "The versions do not differ among all ".$nrChangelogFiles."/".$nrSpecFiles." changelog/spec files.\n";
  exit 0;
}

print $main::versionDifferences." versions differ, syncing from ".$main::topVersionChangelog." (".$main::topVersionVersion.") to ".($nrChangelogFiles-1)."/".$nrSpecFiles." changelog/spec files.\n";

foreach my $changelogFile (@main::changelogFiles) {
  if ($main::topVersionVersion eq $main::appVersionByChangelog{$changelogFile}) {
    ($main::verbosity > 1) && print "Not changing up-to-date ".$changelogFile."\n";
    next;
  }
  # update this one
  ($main::verbosity > 0) && print "Updating ".$changelogFile." to version ".$main::topVersionVersion.".\n";
  my $oldChangelogContent = readCompleteFile($changelogFile);
  my $newChangelogContent = $main::firstLineByChangelog{$changelogFile};
  my $searchEx = $main::appVersionByChangelog{$changelogFile};
  $searchEx =~ s/\+/\\+/g;
  $newChangelogContent =~ s/$searchEx/$main::appVersionByChangelog{$main::topVersionChangelog}/;
  $newChangelogContent .= "\n".$main::firstChangelogByChangelog{$main::topVersionChangelog}.$oldChangelogContent;
  printToFile($changelogFile, $newChangelogContent);
}
foreach my $specFile (@main::specFiles) {
  if ($main::topVersionVersion eq $main::appVersionsBySpecFile{$specFile}) {
    next;
  }
  # they differ, compare  
  ($main::verbosity > 0) && print "Updating ".$specFile." to version ".$main::topVersionVersion.".\n";
  my $oldSpecContent = readCompleteFile($specFile);
  my $newSpecContent = $oldSpecContent;
  ($main::verbosity > 2) && print "Changing\n  ^Version: $main::versionsBySpecFile{$specFile}\$\nto\n  Version: $main::upstreamVersionByChangelog{$main::topVersionChangelog}\n";
  $newSpecContent =~ s/^Version: $main::versionsBySpecFile{$specFile}$/Version: $main::upstreamVersionByChangelog{$main::topVersionChangelog}/m;
  my $searchEx = "^Release: ".$main::releasesBySpecFile{$specFile}.'$';
  $searchEx =~ s/\+/\\+/g;
  ($main::verbosity > 2) && print "Changing\n  $searchEx\nto\n  Release: $main::releaseVersionByChangelog{$main::topVersionChangelog}\n";
  $newSpecContent =~ s/$searchEx/Release: $main::releaseVersionByChangelog{$main::topVersionChangelog}/m;
  # changelog formats differ too much
  #$newSpecContent =~ s/^%changelog$/%changelog\n$main::firstChangelogByChangelog{$main::topVersionChangelog}/;
  # XBCS-PBA-BuildReportCc-Changelog
  if ($main::topVersionChangedBy ne "") {
    if ($newSpecContent =~ /^###XBCS-PBA-BuildReportCc-Changelog: /mi) {
      # existing entry
      ($main::verbosity > 2) && print "Changing existing '###XBCS-PBA-BuildReportCc-Changelog: ' entry to '###XBCS-PBA-BuildReportCc-Changelog: $main::topVersionChangedBy'\n";
      $newSpecContent =~ s/^###XBCS-PBA-BuildReportCc-Changelog: .*/###XBCS-PBA-BuildReportCc-Changelog: $main::topVersionChangedBy/mi;
    } else {
      # no existing entry, add after obligatory entry, or before mandatory-ish first empty line
      ($main::verbosity > 2) && print "Creating new '###XBCS-PBA-BuildReportCc-Changelog: ' entry '###XBCS-PBA-BuildReportCc-Changelog: $main::topVersionChangedBy'\n";
      $newSpecContent =~ s/^$/###XBCS-PBA-BuildReportCc-Changelog: $main::topVersionChangedBy\n/m;
    }
  }
  printToFile($specFile, $newSpecContent);
}

print "Done syncing from ".$main::topVersionChangelog." (".$main::topVersionVersion.") to ".($nrChangelogFiles-1)."/".$nrSpecFiles." changelog/spec files.\n";

#($main::verbosity > 0) && print "\n";

















