#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: update_rh,v 1.8 1998/12/17 23:30:27 ejb Exp $
# $Source: /home/ejb/scripts/RCS/update_rh,v $
# $Author: ejb $
#

require 5.003;
use strict;
use Cwd;

my $whoami = ($0 =~ m,([^/\\]*)$,) ? $1 : $0;
#my $dirname = ($0 =~ m,(.*)[/\\][^/\\]+$,) ? $1 : ".";

my $search = $ENV{'SEARCH'} || "/home/ejb";
push(@INC, "$search/source/perl/modules");

require FTP;
require FileInfo;

my $host = "updates.redhat.com";
my $dir = "/";
my $proxy = undef;

my $user = "ftp";
my ($login) = ($ENV{'USER'} || $ENV{'LOGNAME'} || (getpwuid($<))[0] ||
		"unknown");
chop(my $system = `hostname || uname -n`);
{
    # Get fully qualified domain name.  It doesn't really matter if this
    # isn't the right RHS of our email address since it's just being used
    # as an anon-ftp password...
    my @result = gethostbyname($system);
    if (@result)
    {
	# Sorry about the hardcoded numbers... 2 is INET, 4 is the position
	# of the address.  These are going to be the same on all POSIX systems.
	# This saves having to load the POSIX module.
	@result = gethostbyaddr($result[4], 2);
	if (@result)
	{
	    $system = $result[0];
	}
    }
}

my $pass = "$login\@$system";
my $pwd = getcwd;
my $version = undef;
if ($pwd =~ m,/([^/]*)-updates$,)
{
    $version = $1;
}

my $arg;
while (@ARGV)
{
    $arg = shift(@ARGV);
    if ($arg eq "-proxy")
    {
	defined($ARGV[0]) or die "$whoami: -proxy requires an argument\n";
	$proxy = shift(@ARGV);
    }
    elsif ($arg eq "-host")
    {
	defined($ARGV[0]) or die "$whoami: -host requires an argument\n";
	$host = shift(@ARGV);
    }
    elsif ($arg eq "-dir")
    {
	defined($ARGV[0]) or die "$whoami: -dir requires an argument\n";
	$dir = shift(@ARGV);
    }
    elsif ($arg eq "-pass")
    {
	defined($ARGV[0]) or die "$whoami: -pass requires an argument\n";
	$pass = shift(@ARGV);
    }
    elsif ($arg eq "-version")
    {
	defined($ARGV[0]) or die "$whoami: -version requires an argument\n";
	$version = shift(@ARGV);
    }
    else
    {
	&usage();
    }
}

if (! defined $version)
{
    die "$whoami: unable to infer version from working directory, " .
	"and -version not given on the commandline\n";
}

if (defined $proxy)
{
    $user = "$user\@$host";
    $host = $proxy;
}

my %ignore = ();
if (-f "IGNORE")
{
    open(I, "<IGNORE") or die "$whoami: open IGNORE failed: $!\n";
    while (<I>)
    {
	chop;
	next if m/^$/;
	$ignore{$_} = 1;
    }
    close(I);
}

my $ftp = new FTP();
$ftp->login($host, $user, $pass);
$ftp->cwd($dir);
$ftp->cwd("$version/i386");
my @result = new_from_ls FileInfo($ftp->list());
my @rh_files = map { [$_->name(), $_->size()] } @result;
my %local = ();
my $nfiles = 0;
for (<*.rpm>)
{
    $nfiles++;
    my @result = &parse_rpm_filename($_);
    if (@result)
    {
	$local{$result[0]} = $result[1];
    }
}
my @to_get = ();
my @to_remove = ();
for (@rh_files)
{
    my ($file, $size) = @$_;
    my @result = &parse_rpm_filename($file);
    if (@result)
    {
	my $get = 1;
	if (exists $ignore{$result[0]})
	{
	    $get = 0;
	}
	elsif (exists $local{$result[0]})
	{
	    if ($local{$result[0]} eq $result[1])
	    {
		$get = 0;
	    }
	    else
	    {
		push(@to_remove, $result[0] . $local{$result[0]});
	    }
	}
	push(@to_get, [$file, $size]) if $get;
    }
    else
    {
	next if (($file eq ".") || ($file eq ".."));
	push(@to_get, [$file, $size]);
	push(@to_remove, $file) if -f $file;
    }
}

for (@to_remove)
{
    print "remove $_\n";
}

for (@to_get)
{
    print "get ", $_->[0], " (", $_->[1], " bytes)\n";
}

exit 1 unless &yn("Proceed? ");

mkdir "remove", 0777;
for (@to_remove)
{
    rename $_, "remove/$_";
}

for (@to_get)
{
    my ($file, $size) = @$_;
    print "Retrieving $file ($size bytes)\n";
    eval { $ftp->retrieve($file, $size, $file) };
    if ($@)
    {
	warn "$whoami: retrieval of $file failed: $@";
    }
}

sub parse_rpm_filename
{
    my $file = shift;
    if ($file =~ m/^(.+)(-[^-]+-[^-]+\.\w+\.rpm)/)
    {
	($1, $2);
    }
    else
    {
	();
    }
}

sub yn
{
    my $prompt = shift;
    my $answer;
    print $prompt;
    do
    {
	chop($answer = <STDIN>);
	if ($answer eq "y")
	{
	    return 1;
	}
	elsif ($answer eq "n")
	{
	    return 0;
	}
	else
	{
	    print "Please enter y or n: ";
	}
    }
    while (($answer ne "y") && ($answer ne "n"));
}

sub usage
{
    die "Usage: $whoami [ -version v -host host -proxy proxy " .
	"-dir dir -pass password ]\n";
}
