#!/usr/bin/perl use warnings; use strict; use File::Find; use File::Basename; use File::Spec; use Getopt::Long; my $delete = 0; my $unrar = "/sw/bin/unrar"; my $me = basename($0); my $bundle; if (defined $ENV{APP_BUNDLER} and $ENV{APP_BUNDLER} =~ /Platypus/) { $bundle = shift @ARGV; $me = basename($bundle); $unrar = "$bundle/Contents/Resources/unrar" if -f "$bundle/Contents/Resources/unrar"; print "Running $me ($unrar).\n"; } die "Could not find unrar executable" unless -f $unrar; $delete = 1 if $me =~ /delete/i; print "RAR files will be deleted when extraction is complete.\n" if $delete; GetOptions( 'delete|d' => \$delete, ) or die "usage: $me [--delete] files_or_directories ...\n"; my @dirs; my @files; while (my $path = shift @ARGV) { if (-d $path) { find({ wanted => sub { push @dirs, $_ if -d $_; }, no_chdir => 1, }, $path, ); } elsif (-f $path) { push @files, $path; } } my @allfiles; DIR: foreach my $d (@dirs) { opendir D, $d or die "$d: $!"; my $count = 0; while (my $f = readdir D) { $f = File::Spec->catfile($d, $f); if (-f $f and $f =~ /(\.rar|\.r01)$/) { push @files, $f unless $count; push @allfiles, $f; $count++; } } } unrar($_) for @files; sub unrar { my $f = shift; chdir(dirname($f)) or die "chdir(".dirname($f)."): $!"; my $rc = system($unrar, "x", "-y", basename($f)); die "unrar returned $rc" unless $rc == 0; } if ($delete) { foreach my $f (@allfiles) { print "Deleting $f...\n"; unlink $f or die "unlink($f): $!"; } } print @files ? "Finished\n" : "No files found\n";