#!/usr/bin/perl
#
# $Id$
#
# PBA builddeps 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-builddeps";

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

 Extracts the builddeps from debian*/control and lists them without , etc.
 for direct usage in an apt-get or aptitude call.

 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/control
  -q             Quiet mode, supresses informative messages, for  feeding.
  -v             Verbose verboser, logs more.
  
  Hint: Alternatively call
    sudo mk-build-deps -i -B debian*/control

';
#-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();

my $selectorPattern = 'debian.*'.$main::directorySelector;

if (scalar(@main::controlFiles) == 1) {
  ($main::verbosity > 0) && print "\nBuild-Dependencies are:\n";
  print $main::buildDeps."\n";
} else {
  if ($main::verbosity > 0) {
    print "\n";
    foreach my $controlFile (@main::controlFiles) {
      my ($debianDir, $controlFileBasename) = split /\//, $controlFile, 2;
      if (($main::directorySelector ne "") and ($debianDir !~ /$selectorPattern/)) {
        next;
      }
      print "Build-Dependencies in ".$controlFile." are:\n".$main::buildDepsByControlFile{$controlFile}."\n\n";
    }
    print "Total Build-Dependencies are:\n";
  }
  print $main::buildDeps."\n";
}
if ( $main::opts{"i"} ) {
  my $installCmd = "sudo apt-get install \\\n  ".$main::buildDeps;
  if ($main::verbosity > 0) { print "Going to install dependencies calling this:\n".$installCmd."\n"; }
  executeOrDie($installCmd);
  if ($main::verbosity > 0) { print "Successfully installed these dependencies:\n".$main::buildDeps."\n"; }
} else {
  if ($main::verbosity > 0) { 
    if ($main::directorySelector eq "") {
      print "\nTo make sure they all are installed call with -i or this:\nsudo apt-get install \$(pba-builddeps -q)\n";
    } else {
      print "\nTo make sure they all are installed, call with -i or this:\nsudo apt-get install \$(pba-builddeps -o ".$main::directorySelector." -q)\n";
    }
  }
}
($main::verbosity > 0) && print "\n";

