"rpmx"+"unzipx" replaced by the general "exx" (.rpm/.zip += .a/.deb)
[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 fastgetcwd);
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         };
17
18 die "Syntax: $0 <pathname((".join("|",map(".$_",sort keys %{+FORMATS})).")|=<ext>)>..."
19                 if !@ARGV;
20
21 my $origdir=fastgetcwd;
22 for my $fname (@ARGV) {
23         $fname=~m#([^/]+)([.=])(\L[^./]+\E)$# or die "Extension not found for archive: $fname";
24         my($path,$base,$ext)=($`,$1,$3);
25         my($pathname)=($2 eq "=" ? "$path$base" : $fname);
26         my $cmd=FORMATS->{$ext} or die "Extension \"$ext\" not known for archive: $fname";
27         -r $pathname && !-d $pathname or die "Archive not readable: $pathname";
28         my($extdir)=(-e $base && !-d $base ? "$base.dir" : $base);
29         -d $extdir or mkdir $extdir or die "Unable to create directory \"$extdir\": $!";
30         chdir $extdir or die "Unable to chdir to \"$extdir\": $!";
31         $pathname="../$pathname" if $pathname!~m#^/#;
32         $pathname=~s/'/'\\''/g;
33         $pathname="'$pathname'";
34         $cmd=~s/!/$pathname/g;
35         $cmd="set -ex;$cmd";
36         print "\t$extdir/:\n"; STDOUT->flush();
37         my $rc;
38         $rc=system $cmd and die "$cmd (rc=".($rc>>8)."): $!";
39         }
40         continue {
41                 chdir $origdir;
42                 }
43 exit 0;