#!/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-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/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);
    }
}
if ( my $msg = BackupPC::XS::Lib::logMsgGet() ) {
    print join("", @$msg);
}
if ( length($attrib->digest()) ) {
    printf("Attrib digest is %s\n", unpack("H*", $attrib->digest()));
}
$Data::Dumper::Indent   = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Pad      = "  ";
$Data::Dumper::Terse    = 1;
my $idx = 0;
my $info;
print("\$attrib = {\n");

while ( 1 ) {
    ($info, $idx) = $attrib->iterate($idx);
    last if ( !defined($info) );
    next if ( defined($specificInode) && $info->{name} ne $specificInodeHexStr );
    $info->{digest} = unpack("H*", $info->{digest}) if ( defined($info->{digest}) );
    my $str = Data::Dumper->Dump([$info]);
    $str =~ s/\n$//;
    $str =~ s/^ *//;
    print "  '$info->{name}' => ", $str, ",\n";
}
print("};\n")
