8321cf303a1aa28e7e1112dff8bf826ad6f5c064
[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 FORMAT_TAR=>'tar xf $pathname;i=`echo *`;echo "$i"|grep -q " " || test * != $base ||'
12                          .'(mv "$i" "$i".$$;(set +x;mv "$i".$$/* .);rmdir "$i".$$)';
13 use constant FORMATS=>{
14         "rpm"    =>'rpm2cpio $pathname|cpio -id --quiet',       #-v #FIXME: --sparse doesn't work, why?
15         "zip"    =>'unzip -Lq $pathname',
16         "jar"    =>'unzip -Lq $pathname',
17         "a"      =>'ar x $pathname',
18         "deb"    =>'ar x $pathname;'
19                    .'for i in *.tar.gz;do j=`basename $i .tar.gz`;mkdir -p $j;cd $j;tar xzf ../$i;cd ..;rm -f $i;done',
20         "arj"    =>'unarj x $pathname',
21         "tar"    =>FORMAT_TAR,
22         "tar.gz" =>do { $_=FORMAT_TAR; s/xf/xzf/; $_; },
23         "tgz"    =>"tar.gz",
24         "tar.bz2"=>do { $_=FORMAT_TAR; s/xf/xjf/; $_; },
25         "tar.bz" =>"tar.bz2",
26         "tbz"    =>"tar.bz",
27         "msi"    =>'cabextract -q $pathname',
28         };
29
30 die "Syntax: $0 <pathname((".join("|",map(".$_",sort keys %{+FORMATS})).")|=<ext>)>..."
31                 if !@ARGV;
32
33 my @extdirs;
34 my $origdir=cwd;
35 for my $fname (@ARGV) {
36         my @parsed;
37         for my $fmt (sort { length $b<=>length $a; } keys %{+FORMATS}) {
38                 last if @parsed=$fname=~m#^(.*?)([^/]+)([.=])(\Q$fmt\E)$#i;
39                 }
40         $parsed[3] or die "Extension not found for archive: $fname";
41         my($path,$base,$ext)=@parsed[0,1,3];
42         my($pathname)=($parsed[2] eq "=" ? "$path$base" : $fname);
43         my($cmdtry,$cmd)=(lc $ext);
44         do {
45                 $cmd=$cmdtry;
46                 $cmdtry=FORMATS->{$cmdtry};
47                 } while ($cmdtry);
48         -r $pathname && !-d $pathname or die "Archive not readable: $pathname";
49         my($extdir)=(-e $base && !-d $base ? "$base.dir" : $base);
50         # Extraction-overwriting cowardly not supported:
51                         # -d $extdir or ...
52         mkdir $extdir or die "Unable to create directory \"$extdir\": $!";
53         chdir $extdir or die "Unable to chdir to \"$extdir\": $!";
54         $pathname="../$pathname" if $pathname!~m#^/#;
55         $path    ="../$path"     if $path    !~m#^/#;
56         $cmd="set -ex;$cmd";
57         my %substvars=(
58                         "pathname"=>\$pathname,
59                         "path"    =>\$path,
60                         "base"    =>\$base,
61                         "ext"     =>\$ext,
62                         "extdir"  =>\$extdir,
63                         );
64         while (my($name,$var)=each %substvars) {
65                 ($_=$$var)=~s/'/'\\''/g;
66                 $cmd="$name='$_';$cmd";
67                 }
68         print "\t$extdir/:\n"; STDOUT->flush();
69         my $rc;
70         $rc=system $cmd and die "$cmd (rc=".($rc>>8)."): $!";
71         push @extdirs,$extdir;
72         }
73         continue {
74                 chdir $origdir;
75                 }
76 print "extdir=".$extdirs[0]."\n" if 1==@extdirs;
77 exit 0;