88933d06a932325f7f62d54b619b816f5b5ec9cc
[nethome.git] / bin / exx
1 #! /usr/bin/perl
2 #
3 #       $Id$
4
5 use strict;
6 use warnings;
7
8 use IO::Handle;
9 use Cwd qw(chdir cwd);
10
11 use constant FORMATS=>{
12         "rpm"=>'rpm2cpio !|cpio -id --quiet',   #-v #FIXME: --sparse doesn't work, why?
13         "zip"=>'unzip -Lq !',
14         "a"  =>'ar x !',
15         "deb"=>'ar x !;for i in *.tar.gz;do j=`basename $i .tar.gz`;mkdir -p $j;cd $j;tar xzf ../$i;cd ..;rm -f $i;done',
16         "arj"=>'unarj x !',
17         };
18
19 die "Syntax: $0 <pathname((".join("|",map(".$_",sort keys %{+FORMATS})).")|=<ext>)>..."
20                 if !@ARGV;
21
22 my $origdir=cwd;
23 for my $fname (@ARGV) {
24         $fname=~m#([^/]+)([.=])(\L[^./]+\E)$# or die "Extension not found for archive: $fname";
25         my($path,$base,$ext)=($`,$1,$3);
26         my($pathname)=($2 eq "=" ? "$path$base" : $fname);
27         my $cmd=FORMATS->{$ext} or die "Extension \"$ext\" not known for archive: $fname";
28         -r $pathname && !-d $pathname or die "Archive not readable: $pathname";
29         my($extdir)=(-e $base && !-d $base ? "$base.dir" : $base);
30         -d $extdir or mkdir $extdir or die "Unable to create directory \"$extdir\": $!";
31         chdir $extdir or die "Unable to chdir to \"$extdir\": $!";
32         $pathname="../$pathname" if $pathname!~m#^/#;
33         $pathname=~s/'/'\\''/g;
34         $pathname="'$pathname'";
35         $cmd=~s/!/$pathname/g;
36         $cmd="set -ex;$cmd";
37         print "\t$extdir/:\n"; STDOUT->flush();
38         my $rc;
39         $rc=system $cmd and die "$cmd (rc=".($rc>>8)."): $!";
40         }
41         continue {
42                 chdir $origdir;
43                 }
44 exit 0;