#!/usr/bin/perl -w

# Print list of packages on mageai mirror
# ---------------------------------------
# Skip all packages
#   - that have a time-stamp before a given reference date
#   - that are listed in a "skip" file
#
#   Options from command line:
#	--time <reference date> ... reference date
#		anything accepted by the perl Date::Parse module, e.g.
#			"Feb 22"
#			"Feb 22 09:38"
#			"Feb 22 2011"
#		default value = date of the Mageia iso
#
#	--mirror <url> 		... url of the mirror
#		default value = ftp://ftp.mandrivauser.de/mirrors/Mageia/distrib/cauldron/i586/media
#
#	--skip <skip-file>	... file with names of packages to be skipped
#					(omitted from the printed results)
#				    it is possible to use as a skip-file
#				    the output procuded at some earlier execution
#
#	-- help


use strict;
use warnings;

use Date::Parse;
use Date::Format;
use List::MoreUtils      qw ( first_index );


# List of command-line arguments
#	- normally refers to an argument-value pair
#	- no value if leading *

my @option_list = qw ( time mirror skip *help );

# For setting an intial date, argument should be something like
#	Feb 22
#	Feb 22 07:30
#	Feb 22 2011
#	etc	-  dont forget the time must be quoted!



# Analyse the command-line arguments, determine options
# -----------------------------------------------------

	# Check and detect type of argument list items
	#	if skip_flag is set: value field, already done

my ( $x_site, $x_path, $ref_time, $skip_file, $x_fid );

my $x_x = -1;				# item index
my $skip_flag = 0;
my $x_error ;
foreach my $x_item ( @ARGV ) {
    my ( $x_key, $x_value );

    $x_error = "Item \"$x_item\" not recognized"; # assume
    $x_x++;
    if ( $skip_flag ) { $skip_flag = 0 ; next }
    if ( $x_item =~ s/\-\-// ) {
	if ( ( grep { $_ =~ m{^\**$x_item} } @option_list ) > 0 ) {
	    if ( ( grep { $_ eq $x_item } @option_list ) > 0 ) {
		$x_value = $ARGV[$x_x+1];
		if ( defined $x_value ) {
		    $skip_flag = 1;
		    $x_error = undef;
		} else {
		    $x_error = "No value after item $x_item";
		}
	    } else {
		$skip_flag = 0;
		$x_error = undef;
	    }
	}
    }

	# The current argument looks OK, do what it indicates

    unless ( defined $x_error ) {
	if ( $x_item eq 'time' ) {

	    # Define the reference time

	    my $x_temp = str2time ( $x_value );
	    if ( defined $x_temp ) {
		$ref_time =$x_temp;
	    } else {
		$x_error = "Argument after --time must be valid date";
	    }
	} elsif ( $x_item eq 'mirror' ) {

	    # Define the mirror

	    if ( $x_value =~ m{^(http|ftp://[^/]+)/(.*)$} ) {
		$x_site = $1;
		$x_path = $2;
	    } else {
		$x_error = "Argument after --mirror is not a valid url";
	    }
	} elsif ( $x_item eq 'skip' ) {

	    # Define a list of files to be skipped

	    if ( -e $x_value ) {
		$skip_file = $x_value;
	    } else {
		$x_error = "Argument after --skip is not an existing file";
	    }

	} elsif ( $x_item eq 'help' ) {

	    # Provice help information

	    print "Recognized arguments:\n";
	    print "  --time <refence-time> ... earliest package date to consider\n";
	    print "  --mirror <url>        ... url of mirror to explore\n";
	    print "  --skip <skip-file>    ... file with package names to ignore\n";
	    print "  --help                ... obtain this message\n";
	    exit;
	}
    } # type-specific handling

    if ( defined $x_error ) {
	print "*** $x_error !\n";
	exit;
    }
}

	# If necesssary, use default mirror

unless ( defined $ref_time)
	{ $ref_time = str2time ( "Feb 15 12:00"); } # default (time of iso)

unless ( defined $x_site ) {
    $x_site = 'ftp://ftp.mandrivauser.de';
    $x_path = 'mirrors/Mageia/distrib/cauldron/i586/media';
}


print "Reference time = ".time2str ('%c', $ref_time)."\n".
					"Mirror = $x_site/$x_path\n";


# Create the list of files to be skipped
# --------------------------------------

my @skip_list = ();
if ( defined $skip_file ) {
    open ( $x_fid,  '<', $skip_file ) or die "$skip_file $!";
    while ( my $x_line = <$x_fid> ) {
	if ( $x_line =~ / = / ) { next; }
	if ( $x_line =~ /^\s*#/ ) { next; }
	$x_line =~ /([\S]+)\s*$/;
	push ( @skip_list, $1 );
    }
    close $x_fid;
}
##foreach (@skip_list){ print "$_\n"; }


# Create the list of installed packages
# -------------------------------------

my $x_rpms = `rpm -qa`;
my @x_list = ();
foreach ( split ( "\n", $x_rpms ) ) {
    my ( $x_file ) = $_ =~ /(.+)\-[^-]+\-[^-]+$/;
    $x_file =~ s/\.mga\d+$//;
    push ( @x_list, $1 );
}
my @loaded_list = sort ( @x_list );
##foreach (@loaded_list){ print "$_\n"; }


# Print the list of new packages
# ------------------------------

my $x_count = 0;
#my $data_file = '/home/harms/lftp_file';
#open ( $x_fid,  '<', $data_file ) or die "$data_file $!";
open ( $x_fid, "lftp -e \"ls /$x_path/core/release ; quit\" $x_site  2>&1 |" );
while ( my $x_line = <$x_fid> ) {
    unless ( $x_line =~ /.rpm$/ ) { next }
    my @x_list = split ( /\s+/, $x_line );
    my $file_name = $x_list[8];
    my $time_string = "$x_list[5] $x_list[6] $x_list[7]";
    my $file_time = str2time ( $time_string );
    if ( $file_time <= $ref_time ) { next; }
    my ( $package_name ) = $file_name =~ m{([^/]+)\.[^.]+\.rpm\s*$};
    $package_name =~ s/\.mga\d+$//;
    $package_name =~ s/\-[^-]+\-[^-]+$//;
    if ( ( grep { $_ eq $package_name } @loaded_list ) > 0 ) { next; }
    if ( ( grep { $_ eq $package_name } @skip_list ) > 0 ) { next; }
##    if ( $x_count++ > 10 ) { last; }
    print "$time_string    $package_name\n";
}
close ( $x_fid );
