#!/usr/pkg/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_zcat: uncompress files to stdout
#
# DESCRIPTION
#
#   Usage:
#         BackupPC_zcat file...
#         BackupPC_zcat MD5_digest...
#         BackupPC_zcat [-h host] [-n num] [-s share] clientPath...
#         BackupPC_zcat $TopDir/pc/host/num/fshare/mangledPath...
#
#   BackupPC_zcat is a command-line utility for uncompressing BackupPC
#   compressed files.
#
# AUTHOR
#   Craig Barratt  <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
#   Copyright (C) 2001-2020  Craig Barratt
#
#   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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
#========================================================================
#
# Version 4.4.0, released 20 Jun 2020.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================

use strict;
no utf8;

use lib "/usr/pkg/share/BackupPC/lib";
use Compress::Zlib;
use BackupPC::XS qw( :all );
use BackupPC::Lib;
use BackupPC::View;
use Getopt::Std;

die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
my $TopDir = $bpc->TopDir();
my %Conf   = $bpc->Conf();

my %opts;
if ( !getopts("h:n:s:", \%opts) ) {
    print STDERR <<EOF;
usage:
    BackupPC_zcat file...
    BackupPC_zcat MD5_digest...
    BackupPC_zcat $TopDir/pc/host/num/share/mangledPath...
    BackupPC_zcat [-h host] [-n num] [-s share] clientPath...
EOF
    exit(1);
}

my $ret = 0;
if ( @ARGV ) {
    while ( my $file = shift(@ARGV) ) {
        #
        # first case is an MD5 digest - try to find it in the pool (if it doesn't exist as a file)
        #
        if ( !defined($opts{h}) && !-f $file && $file =~ /^[\da-f]{16,}$/ ) {
            my $poolFile = $bpc->MD52Path(pack("H*", $file), $Conf{CompressLevel});
            $file = $poolFile if ( -f $poolFile );
        }
        #
        # second case is a path that exists
        #
        if (   !defined($opts{h})
            && -f $file
            && defined(my $fh = BackupPC::XS::FileZIO::open($file, 0, $Conf{CompressLevel})) ) {
            $ret ||= zcat($fh, $file);
            next;
        }
        #
        # now see if it's a client file path, or a mangled stored path.
        #
        my($Host, $Num, $Share, $View, @Backups, $i);
        if ( !defined($opts{h}) && $file =~ m{^\Q$TopDir\E/pc/([^/]*)/(\d+)/(.*)} ) {
            $Host = $1;
            $Num  = $2;
            $file = $3;
            if ( $file =~ m{(.*?)(/.*)} ) {
                $Share = $1;
                $file  = $2;
            } else {
                $Share = $file;
                $file  = "/";
            }
            $Share = $bpc->fileNameUnmangle($Share);
            $file  = $bpc->fileNameUnmangle($file);
        } else {
            if ( !defined($opts{h}) || !defined($opts{n}) || !defined($opts{s}) ) {
                print(STDERR "BackupPC_zcat: can't find $file\n");
                exit(1);
            }
            $Host  = $opts{h};
            $Num   = $opts{n};
            $Share = $opts{s};
        }
        @Backups = $bpc->BackupInfoRead($Host);
        $Num     = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
        for ( $i = 0 ; $i < @Backups ; $i++ ) {
            last if ( $Backups[$i]{num} == $Num );
        }
        if ( $i >= @Backups ) {
            print(STDERR "BackupPC_zcat: backup #$Num doesn't exist for host $Host\n");
            exit(1);
        }
        $View = BackupPC::View->new($bpc, $Host, \@Backups);
        my $a = $View->fileAttrib($Num, $Share, $file);
        if ( !defined($a) ) {
            print(STDERR "BackupPC_zcat: can't find file $file for host $Host, share $Share, backup number $Num\n");
            exit(1);
        }
        if ( $a->{type} == BPC_FTYPE_DIR ) {
            print(STDERR "BackupPC_zcat: $file is a directory\n");
            exit(1);
        }
        if ( !-f $a->{fullPath} ) {
            print(STDERR "BackupPC_zcat: can't find fullPath $a->{fullPath} for $file\n");
            exit(1);
        }
        if ( defined(my $fh = BackupPC::XS::FileZIO::open($a->{fullPath}, 0, $Backups[$i]{compress})) ) {
            $ret ||= zcat($fh, $file);
        } else {
            print(STDERR "BackupPC_zcat: can't open fullPath $a->{fullPath} for $file\n");
            exit(1);
        }
    }
} else {
    my $fh = BackupPC::XS::FileZIO::fdopen(*STDIN, 0, $Conf{CompressLevel});
    $ret ||= zcat($fh, "stdin");
}
exit($ret);

sub zcat
{
    my($fh, $fileName) = @_;
    my($data, $r, $ret);

    while ( ($r = $fh->read(\$data, 4 * 65536)) > 0 ) {
        print($data);
    }
    if ( $r < 0 ) {
        print(STDERR "$0: can't uncompress $fileName\n");
        $ret = 1;
    }
    $fh->close();
    return $ret;
}
