#!/usr/bin/perl -w
#
# $Id: URLGet,v 1.3 2002/06/22 18:33:31 cvs Exp $
#
# URLGet: Mirror URLS...
#
# Miguel Armas <kuko@ulpgc.es>
#

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

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

#--------------------------------------------[ Configuration Section ]----- 
my $priority = "Warning";
my $alarm = "URLGet";
my $rsynccmd = "rsync -avz -e ssh --delete --max-delete=25 --stats";

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

# Get ids from arglist
my @ids=@ARGV;

# 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" : "");

##
## Check package list
##
my @urls = readObjectFiles($alarm);

my %url;
foreach my $line (@urls) {
   my ($id,$type,$value);
   if ($line =~ /(\w+)\s+(\w+)\s+(.*)\s*$/) {
      ($id,$type,$value) = ($1,$2,$3);
   }
   else { next; };
   $url{$id}{$type}="$value";
}

# If no id given as argument, mirror all
if (! @ids) {
   @ids = keys %url;
}

foreach my $url (@ids) {
   if (!exists $url{$url}) {
      print "ERROR: $url doesn't exist. SKIPPING";
      next;
   }
   my $cmd = $rsynccmd;
   # If "desc" doesn't exist, use the ID
   $url{$url}{desc} = $url if (! exists $url{$url}{desc});
   # Skip URL with skip set
   if (exists $url{$url}{skip}) {
      if ($verbose) {
         print "\n\n";
         print "###########################################\n";
         print " Skipping $url{$url}{desc}: $url{$url}{skip} \n";
         print "###########################################\n";
      }
      next;
   }
   if ($verbose) {
      print "\n\n";
      print "###########################################\n";
      print " Mirroring: $url{$url}{desc} \n";
      print "###########################################\n";
   }

   # Compose rsync command
   $cmd .= " -P" if ($verbose);
   $cmd .= " --exclude-from $url{$url}{exfile}" if (exists $url{$url}{exfile});
   $cmd .= " '$url{$url}{url}' $url{$url}{dir} ";
   
   if ($verbose) {
      system("$cmd");
   }
   else {
      my $files = `$cmd | grep "files transferred" `;
      chomp $files;
      if ($files =~ /^.*files transferred:\s*(\d+)\s*/) {
         $files = $1;
	 if ($files) {
	    print "$url{$url}{desc}: $files new files \n";
         }
      }
      else {
         print "ERROR: $url{$url}{desc} \n";
      }
   }
}

