#!/usr/bin/perl -w
#
# $Id: APTRep,v 1.2 2002/01/13 14:05:46 cvs Exp $
#
# APTRep: Generates APT Repository 
#
# Miguel Armas <kuko@optyma.net>
#

#--------------------------------------------[ Initialization Section ]----

use strict;
use Getopt::Std;
use pifia;

#--------------------------------------------[ Configuration Section ]----- 
my $priority = "Warning";
my $alarm = "APTRep";

# APT Repositories directory
my $aptrepdir = '<#$aptrepdir#>';

# Repositories to generate (can be more than one separated by commas)
my $aptreps = '<#$genaptreps#>';

#--------------------------------------------[ Code Section ]----

# proactive flag indicates if we should take proactive measures, that is, 
# install/upgrade packages or just notify. Default is 0 but the global 
# variable proactive takes precedence
my $proactive='<#$proactive#>';

# Set a safe PATH
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/sbin:/root/bin";

# Option declaration
use vars qw($opt_v $opt_n $opt_p $opt_q);
getopts('vnpq');

# Verbosity level. If not verbose, we shouldn't write any output unless 
# there is a problem. If quiet is set, we won't write ANY output (even if 
# there where errors). verbose has precedence
my $quiet = 1 if ($opt_q);
my $verbose = 1 if ($opt_v);
$quiet = 0 if ($verbose);

# proactive flag handling (parameters has precedence over global variable)
$proactive=1 if ($opt_p);
$proactive=0 if ($opt_n);

# Flag to show that the situation was corrected (in the output)
my $corrected=($proactive ? "CORRECTED" : "");

foreach my $rep (split(",",$aptreps)) {
   if ($verbose) {
      print "\n\n";
      print "###########################################\n";
      print " Generating: $rep \n";
      print "###########################################\n";
   }
   
   my $cmd="genbasedir --bloat --topdir=$aptrepdir $rep";
   
   # Hmmm genbasedir doesn't return exit codes correctly...
   if ($verbose) {
      system("$cmd");
   }
   else {
      my @out = `$cmd 2>&1`;
      if (grep(/error/,@out)) {
         print "ERROR: generating $rep \n";
      }
   }
}
