#!/usr/bin/perl -w
#
# Copyright 2006 Taco IJsselmuiden <taco@varda.nl>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;
use warnings;

my $SUITE="centos4";
my $ARCH="i386";
my $UPSTREAM_MIRROR = "http://mirror.centos.org/centos/4/os/$ARCH/CentOS/RPMS/";

my %pkgs = ();

# get the list of upstream available packages
my @available = `wget -q -O - $UPSTREAM_MIRROR | cut -d'"' -f6 | grep \.rpm`;
for(my $i=0;$i<=$#available; $i++){
	my $pkg = $available[$i];
	chomp($pkg);
	my ($pkg_name, $pkg_ver, $pkg_rel, $pkg_arch) = ("","","","");
	if ( $pkg =~ m/^(.+)-(.+)-(.+)\.(x86_64|i[3456]86|noarch)\.rpm$/ ){
		$pkg_name = $1;
		$pkg_ver = $2;
		$pkg_rel = $3;
		$pkg_arch = $4;
	}
	$pkgs{"$pkg_name"} = $pkg;
}

# read the list of wanted packages...
my @wanted = `sh -c "export ARCH=$ARCH; . /usr/lib/rpmstrap/scripts/$SUITE;  work_out_rpms; suite_details"`;
for(my $i=0;$i<=$#wanted;$i++){
	chomp($wanted[$i]);
	my ($nr, $pkg) = split(':', $wanted[$i], 2);
	my ($pkg_name, $pkg_ver, $pkg_rel, $pkg_arch) = ("","","","");
	if ( $pkg =~ m/^(.+)-(.+)-(.+)\.(x86_64|i[3456]86|noarch)\.rpm$/ ){
		$pkg_name = $1;
		$pkg_ver = $2;
		$pkg_rel = $3;
		$pkg_arch = $4;
	}
#	if ( $pkg ne $pkgs{"$pkg_name"} ){
#		print STDERR "$pkg\t=>\t".$pkgs{"$pkg_name"}."\n";
#	}
	print STDOUT $nr.":".$pkgs{"$pkg_name"}."\n";
#	print "$pkg_name\t\t$pkg_ver\t\t$pkg_rel\t\t$pkg_arch\n";
}
