+'.jar' extraction rule.
[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         };
28
29 die "Syntax: $0 <pathname((".join("|",map(".$_",sort keys %{+FORMATS})).")|=<ext>)>..."
30                 if !@ARGV;
31
32 my @extdirs;
33 my $origdir=cwd;
34 for my $fname (@ARGV) {
35         my @parsed;
36         for my $fmt (sort { length $b<=>length $a; } keys %{+FORMATS}) {
37                 last if @parsed=$fname=~m#^(.*?)([^/]+)([.=])(\Q$fmt\E)$#i;
38                 }
39         $parsed[3] or die "Extension not found for archive: $fname";
40         my($path,$base,$ext)=@parsed[0,1,3];
41         my($pathname)=($parsed[2] eq "=" ? "$path$base" : $fname);
42         my($cmdtry,$cmd)=(lc $ext);
43         do {
44                 $cmd=$cmdtry;
45                 $cmdtry=FORMATS->{$cmdtry};
46                 } while ($cmdtry);
47         -r $pathname && !-d $pathname or die "Archive not readable: $pathname";
48         my($extdir)=(-e $base && !-d $base ? "$base.dir" : $base);
49         # Extraction-overwriting cowardly not supported:
50                         # -d $extdir or ...
51         mkdir $extdir or die "Unable to create directory \"$extdir\": $!";
52         chdir $extdir or die "Unable to chdir to \"$extdir\": $!";
53         $pathname="../$pathname" if $pathname!~m#^/#;
54         $path    ="../$path"     if $path    !~m#^/#;
55         $cmd="set -ex;$cmd";
56         my %substvars=(
57                         "pathname"=>\$pathname,
58                         "path"    =>\$path,
59                         "base"    =>\$base,
60                         "ext"     =>\$ext,
61                         "extdir"  =>\$extdir,
62                         );
63         while (my($name,$var)=each %substvars) {
64                 ($_=$$var)=~s/'/'\\''/g;
65                 $cmd="$name='$_';$cmd";
66                 }
67         print "\t$extdir/:\n"; STDOUT->flush();
68         my $rc;
69         $rc=system $cmd and die "$cmd (rc=".($rc>>8)."): $!";
70         push @extdirs,$extdir;
71         }
72         continue {
73                 chdir $origdir;
74                 }
75 print "extdir=".$extdirs[0]."\n" if 1==@extdirs;
76 exit 0;