#!/usr/bin/perl
#======================================================================
# Copyright (c) 1998 Daniele Giacomini daniele@pluto.linux.it
#
# 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.
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
# rpmpuri
#
# Estrae i file rpm con una versione gestibile.
#======================================================================

$file = "";
$versione = "";
$rilascio = "";

#----------------------------------------------------------------------
# Carica l'elenco dei file dalla directory corrente.
#----------------------------------------------------------------------
open ( ELENCO, "ls *.rpm | sort |" );

#----------------------------------------------------------------------
# Scandisce nome per nome.
#----------------------------------------------------------------------
while ( $file = <ELENCO> ) {

    #------------------------------------------------------------------
    # Estrae gli elementi che compongono il nome del file.
    #------------------------------------------------------------------
    $file =~ m{^(.*)-([^-]*)-([^-]*)\.([^._-]*)\.rpm};

    #------------------------------------------------------------------
    # Distribuisce i vari pezzi a variabili pi comprensibili.
    #------------------------------------------------------------------
    $versione = $2;
    $rilascio = $3;

    #------------------------------------------------------------------
    # Se l'ultima cifra della versione  alfabetica, e la penultima
    #  numerica, quella alfabetica viene
    # trasformata in numerica per facilitare il confronto.
    #------------------------------------------------------------------
    if ( $versione =~ m{^.*\d[A-Za-z]$} ) {

	#--------------------------------------------------------------
	# L'ultimo elemento della versione  una lettera alfabetica.
	# Converte la lettera in numero.
	#--------------------------------------------------------------
    	$versione =~ m{^(.*)([A-Za-z])$};
	$versione = $1 . ord $2;
    }

    #------------------------------------------------------------------
    # Si verifica che la versione e il rilascio siano numerici.
    #------------------------------------------------------------------
    if ( $versione =~ m{^[0-9.]*$} && $rilascio =~ m{^\d*$} ) {

	#--------------------------------------------------------------
	# OK, il nome va bene.
	#--------------------------------------------------------------
	system( "mkdir rpmpuri 2> /dev/null" );
	chomp ($file);
	system( "mv $file rpmpuri/$file" );

    }

};

#----------------------------------------------------------------------
# Il lavoro  terminato; viene chiuso l'elenco dei file.
#----------------------------------------------------------------------
close ( ELENCO );

#======================================================================

