#!/usr/freeware/bin/perl 

# perldoc fw_prune.pl or scroll to bottom

my $VERSION = "1.1" ;
my $appname = ($0 =~ m/\/([0-9A-Za-z_.-]+)$/g)[-1];

print ( "$appname version $VERSION starting ..." ) ;

my @list = `find . -name 'fw*tardist' | sort` ;

##
# fw filenames are gacky; split simply into 'first' and 'last' parts
my $fileexp = "fw_(.*)\-(.*)\.tardist" ;

my %seen ;

my ($file, $rmfile);

print (" found ".(@list)." packages\n\n") ;

foreach $file ( @list ) {

  $file =~ s/.*\///g ;

  my $system = 0 ;

  chomp($file) ;

  # filename usually of the sort fw_pam_radius_auth-1.3.15.tardist 
  # see $fileexp for regexp above
  
  if ( $file =~ /$fileexp/ ) {

	my $fw_filename = $1 ;
	my $fw_version	= $2 ;
  
    	if ( exists ( $seen{$fw_filename} ) ) {
    
        	print ("Found $file\n\tp) remove previously seen package :\t$seen{$fw_filename}\n\tt) remove this package :\t\t$fw_version\n\tn) do nothing\n" ) ;

		$ans = <STDIN> ;
		chomp($ans) ;


		if ($ans eq "p") {
			$system = 1 ;
			$rmfile = "fw_".$fw_filename."-".$seen{$fw_filename}.".tardist" ;
			$seen{$fw_filename} = $fw_version ;
			print ("removing previous package $rmfile\n") ;
			
		
		} elsif ($ans eq "t" ) {
		        $system = 1 ;
			$rmfile = "fw_".$fw_filename."-".$fw_version.".tardist" ;
			print ("removing this package $rmfile\n") ;
		}

		if ($system) {
			my @rm_args = ("rm", $rmfile) ;
       		        system(@rm_args) == 0 or die "system @rm_args failed: $?" ;
		}

    	} else {
		# keep track of 'first' and 'last' parts
    
		$seen{$fw_filename} = $fw_version ;
    	}
  } else {
    	print ("confused about `$file', skipping.\n") ;
  }
}

=head1 NAME

fw_prune.pl - A freeware.sgi.com mirror manager

=head1 SYNOPSYS

fw_prune.pl 

=head1 DESCRIPTION

A simple script to compare an archive of freeware.sgi.com.

cd /my/freeware/
./fw_prune

The script is interactive, and will attempt to determin 
freeware packages, their version and prompt you whether 
you'd like to keep one over another.

You will be prompted for :

p) remove B<p>reviously seen package

t) remove B<t>his package

n) do B<n>othing

The reason why :

I, like many others, simply set my ftp client to download
the whole of freeware.sgi.com every so often, and in doing 
so add to my previously downloaded files.

I do this to neatly get around the freeware deps issues 
frequently moaned about on comp.sys.sgi.*

This leads to multiple versions of files which is either a 
good thing (in the case of apache/freetype) or a bad thing 
(in the case of most other version transient freeware)

It's bad however if you un-archive automatically using 
something like :

$ cd /my/freeware/dist

$ for file in `find ../ -name '*tardist'`; do tar -xvf $file ; done ;

because you run the chance of expanding older versions over
newer versions.

This script gives you the opportunity to prune out any 
unwanted files from the archive, save overwriting newer 
versions unbeknownst to you when you mass extract them.

=head1 REQUIRES

fw_perl

=head1 AUTHOR

Rob Fielding (F<rob@dsvr.net>)

=head1 COPYRIGHT

Copyright (C) 2003  Rob Fielding
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

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.

=head1 VERSION

1.1

=head1 HISTORY

1.1 (2003-03-08) bug fix

1.0 (2003-03-04) code cleanup, removed ReadKey dep

0.3 (2002-12-10) removed GNU extension '-print0' in find, 
                 replaced with a bit of perl

0.2 (2002-12-08) bug fix

0.1 (2002-12-08) first version, works for me.

=cut

