#!/usr/bin/perl
#############################################################################
#
# rx_* dispatcher and handlers for VFU File Manager
# (c) Vladi Belperchinov-Shabanski "Cade" 2002
# <cade@biscom.net> <cade.datamax.bg> http://cade.webbg.com
# $Id: rx_tar,v 1.4 2002/11/07 23:22:21 cade Exp $
#
# usage:
#   rx_* l archive directory   # list archive directory
#   rx_* v archive             # list entire archive
#   rx_* x archive files...    # extract one file
#   rx_* x archive @listfile   # extract list of files
#
#############################################################################
use strict;

my $cmd = lc shift @ARGV;
my $archive = shift @ARGV;
my $cache = "/tmp/$archive.rx.cache";
$cache =~ s/^(\/tmp\/)(.+)\/([^\/]+)$/$1$3/;

if ( $cmd eq "l" || $cmd eq "v" )
   {
   my $dir = shift @ARGV;
   if ( $dir ) { $dir .= "/" unless $dir =~ /\/$/; }

   if( ! -e $cache )
     {
     # cache not found--fill it
     system( "tar xvf   \"$archive\"             > \"$cache\"" ) if $archive =~ /\.tar$/i;
     system( "gzip  -dc \"$archive\" | tar tvf - > \"$cache\"" ) if $archive =~ /\.tar\.g?z(\.rx\.cache)?$/i;
     system( "gzip  -dc \"$archive\" | tar tvf - > \"$cache\"" ) if $archive =~ /\.tgz$/i;
     system( "xz    -dc \"$archive\" | tar tvf - > \"$cache\"" ) if $archive =~ /\.txz$/i;
     system( "bzip2 -dc \"$archive\" | tar tvf - > \"$cache\"" ) if $archive =~ /\.tar\.bz2?$/i;
     chmod oct(600), $cache; # a bit late but still... :)
     }
   else
     {
     utime time(), time(), $cache; # update last modification time of the cache
     }

   open( i, $cache );
   while(<i>)
      {
      chop;
      s/\s+->\s+\S+$//; # no symlinks support?
      my @D = split /\s+/;
      my $N = $D[5]; # name
      $N =~ s/^\.\///;
      $N =~ s/^\//\//;
      if ( $cmd eq "l" )
        {
        next unless $N =~ s/^$dir([^\/]+\/?)$//;
        $N = $1;
        }
      my $T = "$D[3]$D[4]"; # time
      $T =~ s/[\-\s\:]//g;
      $T = substr( $T, 0, 12 );
      print "NAME:$N\nSIZE:$D[2]\nMODE:$D[0]\nTIME:$T\n\n";
      }
   close( i );
   }
elsif ( $cmd eq "x" )
  {
  my $list;
  if ( $ARGV[0] =~ /^\@(.+)$/ )
    {
    $list = $1;
    }
  else
    {
    $list = "/tmp/$$.rx.list";
    open( o, ">$list" );
    chmod oct(600), $list;
    print o "$_\n" for @ARGV;
    close( o );
    }
  system( "tar xvf   $archive             -T $list" ) if $archive =~ /\.tar$/i;
  system( "gzip  -dc $archive | tar xvf - -T $list" ) if $archive =~ /\.tar\.g?z(\.rx\.cache)?$/i;
  system( "gzip  -dc $archive | tar xvf - -T $list" ) if $archive =~ /\.tgz$/i;
  system( "xz    -dc $archive | tar xvf - -T $list" ) if $archive =~ /\.txz$/i;
  system( "bzip2 -dc $archive | tar xvf - -T $list" ) if $archive =~ /\.tar\.bz2?$/i;
  unlink $list;
  print "gzip  -dc $archive | tar xvf - -T $list";
  }
else
  {
  die $0 . ": wrong command.\n";
  }

