#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use Pod::Usage;
use Archive::Tar::Wrapper;
use Sysadm::Install qw(:all);

use vars qw($CVSVERSION);

$CVSVERSION = '$Revision: 1.1 $';

getopts( "hv", \my %opts );

pod2usage() if $opts{h};

if ( $opts{v} ) {
    my ($version) = $CVSVERSION =~ /(\d\S+)/;
    die "$0 $version\n";
}

my ( $target_dir, @tarfiles ) = @ARGV;

unless ( defined($target_dir) or @tarfiles ) {
    pod2usage("Wrong number of arguments");
}

unless ( -d $target_dir ) {
    my $create = ask "$target_dir doesn't exist. Create it [y]", "y";
    mkd $target_dir if $create =~ /[yY]/;
}

my $arch = Archive::Tar::Wrapper->new();

for my $tarfile (@tarfiles) {
    $arch->read($tarfile);
}

$arch->list_reset();

while ( my $entry = $arch->list_next() ) {
    my ( $tar_path, $phys_path ) = @$entry;
    if ( -f $phys_path ) {
        cp $phys_path, $target_dir;
    }
}

__END__

=head1 NAME

    tarflat - Unpack tarfiles and put their content flat in a single directory

=head1 SYNOPSIS

    tarflat target_dir tarfile ...

=head1 OPTIONS

=over 8

=item B<-h>

Prints this manual page in text format.

=item B<-v>

Prints the script version.

=back

=head1 DESCRIPTION

C<tarflat>

=head1 EXAMPLES

  # Unpack foo.tgz and bar.tgz and store all of their files
  # (anywhere in the directory hierarchy) in the directory
  # /tmp/junk.
  $ mkdir /tmp/junk
  $ tarflat /tmp/junk foo.tgz bar.tgz

=head1 LEGALESE

This software is copyright (c) 2005 of Mike Schilli.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
Archive-Tar-Wrapper. If not, see L<http://www.gnu.org/licenses/>.

=head1 AUTHOR

2005, Mike Schilli <cpan@perlmeister.com>

=head1 MAINTAINER

2018, Alceu Rodrigues de Freitas Junior <arfreitas@cpan.org>

=cut
