#!perl

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2020-10-27'; # DATE
our $DIST = 'App-DiffXlsText'; # DIST
our $VERSION = '0.003'; # VERSION

use 5.010001;
use strict;
use warnings;
use Log::ger;
#use Log::ger::Screen;

use File::chdir;
use File::Temp qw(tempdir);
use File::Which qw(which);
use Getopt::Long;
use IPC::System::Options 'system', -log=>1, -die=>1;

unless (which "xls2csv") {
    die "diff-xls-text: This tool requires xls2csv from CPAN module Spreadsheet::Read\n";
}

my @files;
Getopt::Long::Configure("gnu_getopt", "no_ignore_case", "pass_through");

my @diff_argv;
my $code_push_opt     = sub { my ($cb, $optval) = @_; my $optname = $cb->name; push @diff_argv, (length($optname) > 1 ? "--" : "-").$optname };
my $code_push_opt_val = sub { my ($cb, $optval) = @_; my $optname = $cb->name; push @diff_argv, (length($optname) > 1 ? "--" : "-").$optname, $optval };
my $fail;
my $opt_transpose_csv;

my $tempdir;
my %basenames;

GetOptions(
    normal => $code_push_opt,
    'brief|q' => $code_push_opt,
    'report-identical-files|s' => $code_push_opt_val,
    'c' => $code_push_opt,
    'context|C' => $code_push_opt,
    'u' => $code_push_opt,
    'unified|U' => $code_push_opt,
    'ed|e' => $code_push_opt,
    'rcs|n' => $code_push_opt,
    'side-by-side|y' => $code_push_opt,
    'width|W=i' => $code_push_opt_val,
    'left-column' => $code_push_opt,
    'suppress-common-lines' => $code_push_opt,
    'show-c-function|p' => $code_push_opt,
    'show-function-line|F=s' => $code_push_opt_val,
    'label=s' => $code_push_opt_val,
    'expand-tabs|t' => $code_push_opt,
    'initial-tab|T' => $code_push_opt,
    'tabsize=i' => $code_push_opt_val,
    'suppress-blank-empty' => $code_push_opt,
    'paginate|l' => $code_push_opt,
    'recursive|r' => $code_push_opt,
    'new-file|N' => $code_push_opt,
    'unidirectional-new-file' => $code_push_opt,
    'ignore-file-name-case!' => $code_push_opt,
    'exclude|x=s' => $code_push_opt_val,
    'exclude-from|X=s' => $code_push_opt_val, # filename
    'starting-file|S' => $code_push_opt_val, # filename
    'from-file=s' => $code_push_opt_val, # filename
    'to-file=s' => $code_push_opt_val, # filename
    'ignore-case|i' => $code_push_opt,
    'ignore-tab-expansion|E' => $code_push_opt,
    'ignore-trailing-space|Z' => $code_push_opt,
    'ignore-space-change|b' => $code_push_opt,
    'ignore-all-space|w' => $code_push_opt,
    'ignore-blank-lines|B' => $code_push_opt,
    'ignore-matching-lines|I=s' => $code_push_opt_val,
    'text|a' => $code_push_opt,
    'strip-trailing-cr' => $code_push_opt,
    'ifdef|D=s' => $code_push_opt_val,
    'GTYPE-group-format=s' => $code_push_opt_val,
    'line-format=s' => $code_push_opt_val,
    'LTYPE-line-format=s' => $code_push_opt_val,
    'minimal|d' => $code_push_opt,
    'horizon-lines=i' => $code_push_opt_val,
    'speed-large-files' => $code_push_opt,
    'help' => $code_push_opt,
    'version|v' => $code_push_opt,

    # our own options
    'transpose-csv' => \$opt_transpose_csv,

    '<>' => sub {
        my $filename = $_[0];
        unless (-f $filename) {
            warn "diff-xls-text: No such file or not a file: '$filename'\n";
            $fail++;
            return;
        }

        if (!$tempdir) {
            $tempdir = File::Temp::tempdir(CLEANUP => !$ENV{DEBUG});
        }

        (my $basename0 = $filename) =~ s!.+/!!;
        my $basename;
        my $i = 0;
        while (1) {
            $basename = $basename0 . ($i++ ? ".$i" : "");
            last unless $basenames{ $basename }++;
        }

        mkdir "$tempdir/$basename";
        system "xls2csv", "-A", "-o", "$tempdir/$basename/workbook", $filename;
        push @diff_argv, "$tempdir/$basename";

        if ($opt_transpose_csv) {
            log_trace "Transposing CSV files in $tempdir/$basename ...";
            local $CWD = "$tempdir/$basename";
            for my $file (glob "*.csv") {
                (my $newfile = $file) =~ s/\.csv$/.transpose.csv/;
                system({shell=>1}, "csv-transpose", $file, \">", $newfile);
                unlink $file;
            }
        }
    },
);

exit 1 if $fail;

require File::Which;
my $diff_cmd =
    $ENV{DIFF_XLS_TEXT_DIFF_CMD} //
    (File::Which::which("diffwc") ? "diffwc" : undef) // "diff";

require IPC::System::Options;
IPC::System::Options::system(
    {log=>1},
    $diff_cmd, "-ruN", @diff_argv,
);

# ABSTRACT: Diff the text of two Office spreadsheets (.ods, .xls, .xlsx) as two directories of CSV files
# PODNAME: diff-xls-text

__END__

=pod

=encoding UTF-8

=head1 NAME

diff-xls-text - Diff the text of two Office spreadsheets (.ods, .xls, .xlsx) as two directories of CSV files

=head1 VERSION

This document describes version 0.003 of diff-xls-text (from Perl distribution App-DiffXlsText), released on 2020-10-27.

=head1 SYNOPSIS

Use like you would use the Unix command B<diff>:

 % diff-xls-text [options] <FILE>...

=head1 DESCRIPTION

This is a wrapper for the Unix command B<diff>. It assumes that each input file
is an Office spreadsheet (.ods, .xls, or .xlsx) and tries to convert the file to
a directory of CSV files (where each CSV file is converted from a single
worksheet) using L<xls2csv>. It then passes the converted directories to C<<
diff -ruN >> command.

=head1 OPTIONS

These are options that are interpreted by B<diff-xls-text> and not passed to
B<diff>.

B<Please specify these options before file names.>

=over

=item * --transpose-csv

Transpose CSV first using L<csv-transpose> from L<App::CSVUtils>. This allows
you to do column-based instead of row-based diff.

=back

=head1 ENVIRONMENT

=head2 DEBUG

If set to true, do not cleanup temporary directories.

=head2 DIFF_XLS_TEXT_DIFF_CMD

String. Can be used to set path to diff command. The defaultl is L<diffwc> if
available in PATH, or C<diff>.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-DiffXlsText>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-DiffXlsText>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-DiffXlsText>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 SEE ALSO

Unix command L<diff>.

L<ssdiff> from L<Spreadsheet::Read> which also diffs two spreadsheets but
presents the result differently.

L<xls2csv> from L<Spreadsheet::Read>

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
