#!/usr/bin/env perl

# Copyright (C) 2016--2026 Karl Wette
#
# This file is part of App::PDFLibrarian.
#
# App::PDFLibrarian 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.
#
# App::PDFLibrarian 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 App::PDFLibrarian. If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;

use Carp;
use FindBin qw($Script);
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;

FindBin::again();

use App::PDFLibrarian;
use App::PDFLibrarian::BibTeX qw(read_bib_from_pdf generate_bib_keys format_bib write_bib_to_fh edit_bib_in_fh write_bib_to_pdf);
use App::PDFLibrarian::Library qw(pdf_is_in_library update_pdf_lib make_pdf_links cleanup_links);
use App::PDFLibrarian::Util qw(unique_list get_file_list find_pdf_files);

=pod

=head1 NAME

B<pdf-lbr-edit-bib> - Edit BibTeX bibliographic metadata in PDF files.

=head1 SYNOPSIS

B<pdf-lbr-edit-bib> B<--version>
B<pdf-lbr-edit-bib> B<--help>|B<-h>

B<pdf-lbr-edit-bib> I<links>|I<link-directories> ...

... I<links>|I<link-directories> ... B<|> B<pdf-lbr-edit-bib> ...

=head1 DESCRIPTION

B<pdf-lbr-edit-bib> reads BibTeX bibliographic metadata embedded in PDF files given by I<links> and/or within I<link-directories> in the PDF links directory. If I<links>|I<link-directories> are not given on the command line, they are read from standard input, one per line.

The BibTeX metadata is written to a temporary file, which is then opened in an editing program, given either by the B<$VISUAL> or B<$EDITOR> environment variables, or else the program B<editor>. The B<macros> section of the configuration file is parsed for custom BibTeX macros to define.

Any modifications are then written back to the PDF files given by the I<file> field in each BibTeX entry, and the PDF library links rebuilt as needed.

=head1 PART OF

App::PDFLibrarian

Copyright (C) 2016--2026 Karl Wette. Licensed under the GNU General Public License, version 3 or later.

=cut

# handle help options
my ($version, $help);
GetOptions(
           "version" => \$version,
           "help|h" => \$help,
          ) or croak "$Script: could not parse options";
if ($version) { print "App::PDFLibrarian version $App::PDFLibrarian::VERSION\n"; exit 1; }
pod2usage(-verbose => 2, -exitval => 1) if ($help);

# get list of PDF files
my @pdffiles = find_pdf_files(get_file_list());
croak "$Script: no PDF files to edit" unless @pdffiles > 0;
foreach my $pdffile (@pdffiles) {
  croak "$Script: '$pdffile' is not in the PDF library" unless pdf_is_in_library($pdffile);
}

# read BibTeX entries from PDF metadata
my @bibentries = read_bib_from_pdf(@pdffiles);

# generate initial keys for BibTeX entries
generate_bib_keys(@bibentries);

# coerse entries into BibTeX database structure
foreach my $bibentry (@bibentries) {
  $bibentry->silently_coerce();
}

# write formatted BibTeX entries to a temporary file for editing
my $fh = File::Temp->new(SUFFIX => '.bib', EXLOCK => 0) or croak "$Script: could not create temporary file";
binmode($fh, ":encoding(iso-8859-1)");
write_bib_to_fh({ fh => $fh }, format_bib({}, @bibentries));

# edit BibTeX entries in PDF files
@bibentries = edit_bib_in_fh($fh, @bibentries);

# regenerate keys for modified BibTeX entries
generate_bib_keys(@bibentries);

# write BibTeX entries to PDF metadata; return modified BibTeX entries
@bibentries = write_bib_to_pdf(@bibentries);

# ensure all PDF files are part of library
update_pdf_lib(@bibentries);

# update links in PDF links directory to real PDF files
make_pdf_links(@bibentries);

# cleanup PDF links directory
cleanup_links();

exit 0;
