#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_attribPrint: print the contents of attrib files.
#
# DESCRIPTION
#  
#   Usage: BackupPC_attribPrint attribPath
#
# AUTHOR
#   Craig Barratt  <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
#   Copyright (C) 2005-2019  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.3.1, released 14 Jul 2019.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================

use strict;
no  utf8;
use lib "/usr/share/backuppc/lib";

use BackupPC::Lib;
use BackupPC::XS;
use Data::Dumper;

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

if ( @ARGV != 1 ) {
    print STDERR "Usage: $0 attribPath\n";
    print STDERR "       $0 inodePath/inodeNum\n";
    exit(1);
}

my($dir, $file, $specificInode, $specificInodeHexStr);

if ( $ARGV[0] =~ m{(.*/inode)/(\d+)$} ) {
    $specificInode = $2;
    $dir  = sprintf("%s/%02x", $1, ($specificInode >> 17) & 0x7f);
    $file = sprintf("attrib%02x", ($specificInode >> 10) & 0x7f);
    do {
        $specificInodeHexStr .= sprintf("%02x", $specificInode & 0xff);
        $specificInode = $specificInode >> 8;
    } while ( $specificInode > 0 );
    print("Reading inode attrib file $dir/$file; inodeName = $specificInodeHexStr\n");
} elsif ( $ARGV[0] =~ m{(.+)/(.+)} ) {
    $dir  = $1;
    $file = $2;
} elsif ( -f $ARGV[0] || $ARGV[0] eq "attrib" ) {
    $dir = ".";
    $file = $ARGV[0];
} else {
    $dir = $ARGV[0];
    $file = "attrib";
}

#
# We don't know if compression is on or off for a particular attrib file, so we try
# the opposite if the global setting fails.
#
my $attrib = BackupPC::XS::Attrib::new($Conf{CompressLevel});

if ( !$attrib->read($dir, $file) ) {
    my $digestStr = sprintf(" (%s)", unpack("H*", $attrib->digest())) if ( length($attrib->digest()) );
    $attrib = BackupPC::XS::Attrib::new(!$Conf{CompressLevel});
    if ( !$attrib->read($dir, $file) ) {
        $digestStr = sprintf(" (%s)", unpack("H*", $attrib->digest())) if ( length($attrib->digest()) );
        print STDERR "BackupPC_attribPrint: cannot read attrib file $ARGV[0]$digestStr\n";
        exit(1);
    }
}
my $info = $attrib->get();
#
# convert digest to a hex string
#
foreach my $file ( keys(%$info) ) {
    next if ( !defined($info->{$file}{digest}) );
    $info->{$file}{digest} = unpack("H*", $info->{$file}{digest});
}
if ( length($attrib->digest()) ) {
    printf("Attrib digest is %s\n", unpack("H*", $attrib->digest()));
}
$Data::Dumper::Indent   = 1;
$Data::Dumper::Sortkeys = 1;
if ( defined($specificInode) ) {
    print Dumper($info->{$specificInodeHexStr});
} else {
    print Dumper($info);
}
