#!/usr/bin/perl
#
# $Id$
#
# PBA install program
#
# (C) IBCL Informatik-Buero e.U. 2010, ITEG IT-Engineers GmbH 2023
#
# 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-build-sources";

$main::helpMessage = '
 Syntax:
  '.$main::scriptName.' [CommandOptions] [Options]

 CommandOptions are:
  --help      Prints this help text and exits.
  --version   Prints the version info and exits.

 Without a special command option, pba-build-sources writes a sources.list line
 to \'./build-deps.sources\'

 Options are:
  -b             Build the packages before publishing, like calling pba-build before.
  -c configdir   Read configdir/* rather than /etc/pba/*
  -d             Debug mode, logs everything.
  -l             Create build-deps-sources.list instead of build-deps.sources
  -o selector    Restrict to Only debian.*selector/control
  -s             Create build-deps.sources instead of build-deps-sources.list
  -q             Quiet mode, supresses informative messages, for  feeding.
  -v             Verbose verboser, logs more.
';
#-f configfile    Read /etc/pba/configfile rather than /etc/pba/builder.conf

$main::optionList = "bdc:lo:qsv";

prepareGetopts();

initPBAFunctions();

initDevToolsFunctions();

parseConfig();

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

readDebianFiles();
readSpecFiles();

checkPackageVersions();
if ($main::versionDifferences > 0) {
  my $nrChangelogFiles = scalar(@main::changelogFiles);
  my $nrSpecFiles = scalar(@main::specFiles);
  print "ERROR: ".$main::versionDifferences." version differences among ".$nrChangelogFiles." debian*/changelog and ".$nrSpecFiles." spec*/*.spec files.\n";
  print "The top version from ".$main::topVersionChangelog." is ".$main::topVersionVersion.".\n";
  cleanupAndDie("FATAL: Refusing to release inconsistent versions. Try to use pba-syncchangelogs.\n");
  exit 1;
}

my $outputFormat = "list"; # backwards compatibility, we're used in some scripts deep in CIs and the like

if ( $main::opts{"l"} ) {
  $outputFormat = "list";
}
if ( $main::opts{"s"} ) {
  $outputFormat = "sources";
}

sourcesListToBuildPackages($outputFormat);

exit 0;

