#!/usr/bin/perl -w use strict; my @files = (); my $apt_installed = 0; foreach my $arg ( @ARGV ){ unless( -f $arg ){ #print "$arg is not a valid file ...\n"; next; } push @files, $arg; } unless( $#files > -1 ){ print "Need to specify at LEAST 1 rpmlist file!\n"; exit; } &handle_runtime_lock; my %rpms = (); if( -f "/usr/bin/apt-get" ){ $apt_installed = 1; } else { $apt_installed = 0; } open(RPM, "/bin/rpm -qa |"); while(){ chomp; s/-[\d\.\w]+-[\d\w\.]+$//; # strip everything but the name $rpms{$_}++; } close(RPM); my %list = (); foreach my $file ( @files ){ open(FILE,$file) or die "Can't open $file: $!"; while(){ chomp; next if /^#/; $list{$_}++; } close(FILE); } ## do this seperately than above so we can handle duplicates ## in the list file(s) foreach my $rpm ( keys %list ){ $rpms{$rpm}--; } ## All records in %rpms ## anything set to ## -1 = not installed IS in file (should install) ## 0 = is installed AND in file (do nothing) ## 1 = is installed NOT in file (should delete..or warn) ## my $install_list = ""; my $remove_list = ""; foreach my $rpm ( keys %rpms ){ next if $rpms{$rpm} == 0; if( $rpms{$rpm} == -1 ){ if( $apt_installed ){ $install_list .= "$rpm "; } } elsif( $rpms{$rpm} == 1 ){ $remove_list .= "$rpm "; } } if($install_list ne "") { print "Updating apt cache (apt-get update)\n"; system ("/usr/bin/apt-get update"); print "Installing (via apt-get) $install_list\n"; system ("/usr/bin/apt-get -qqy install $install_list"); } else { #print "Nothing to add\n"; } if($remove_list ne "") { print "WARNING: rpms $remove_list installed, but not in rpm list\n"; # print "Removing (via rpm -e) $remove_list\n"; # system ("/bin/rpm -e $remove_list"); } else { #print "Nothing to delete\n"; } ####### sub handle_runtime_lock { my $lock_file = "/tmp/rpm_sync_runtime.lock"; open( LOCK , ">$lock_file" ) or die "error opening lock file: $!"; if( !flock(LOCK, 6) ){ print "Cannot obtain runtime lock! Concurrent processes running...\n"; exit; } }