#!/usr/bin/perl # Copyright (c) 2007 David Caldwell -*- cperl -*- # This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. use warnings; use strict; use IPC::Run qw(run); use File::Basename; use File::Temp qw(tempdir); sub expand($) { my ($archive) = @_; if ($archive =~ /^(.*)(?:\.t(?:ar\.)?(gz|bz2?)|\.tar|(\.zip))$/) { my @args = (!defined $2 ? () : $2 eq 'gz' ? '-z' : '-j'); my $basename = basename $1; my $unpackdir = tempdir("uz-XXXXXXXXXX", DIR=>'.' ); my $error; if (($3||'') eq '.zip') { run(['unzip', $archive, '-d', $unpackdir], '>', \$error) or die "unzip failed inexplicably: $error"; } else { run(['tar', '-x', @args, '-f', $archive, '-C', $unpackdir], '>', \$error) or die "tar failed inexplicably: $error"; } my @toplevel = glob("$unpackdir/*"); if (scalar @toplevel == 1) { rename $toplevel[0], $basename or die "Uh-oh, couldn't rename $toplevel[0] => $basename\n"; rmdir $unpackdir; } else { rename $unpackdir, $basename or die "Uh-oh, couldn't rename $unpackdir => $basename\n"; } } elsif ($archive =~ /\.bz2$/) { run ['bunzip2', $archive]; } elsif ($archive =~ /\.gz$/) { run ['gunzip', $archive]; } } die "usage: $0 [ ...]\n" unless scalar @ARGV; for my $a (@ARGV) { -f $a or die "Can't find '$a'\n"; expand($a); }