#!perl

our $DATE = '2019-02-21'; # DATE
our $VERSION = '0.001'; # VERSION

use strict;
use warnings;

use Fcntl;
use Getopt::Long qw(:config gnu_compat bundling no_ignore_case no_permute);
use IPC::Run;

my %Opts = (
    backup         => "~",
);
my $Command;
my $File;

sub parse_cmdline {
    my $res = GetOptions(
        'backup|b:s'       => \$Opts{backup},
        'help|h'           => sub {
            print <<USAGE;
Usage:
  inplace [INPLACE_OPTS] <COMMAND> <FILE> [CMD_OPTS]...
  inplace --help
inplace options:
  --backup[=.ext], -b
For more details, see the manpage/documentation.
USAGE
            exit 0;
        },
    );
    @ARGV >= 2 or die "inplace: Usage: inplace [INPLACE_OPTS] <COMMAND> <FILE> [CMD_OPTS]...\n";
    $Command = shift @ARGV;
    if (grep {$_ eq "--"} @ARGV) {
        for (0..$#ARGV) {
            if ($ARGV[$_] eq '--') {
                if ($_ == $#ARGV) {
                    die "inplace: Must specify file after '--'\n";
                }
                $File = $ARGV[0];
                splice @ARGV, $_, 1;
                last;
            }
        }
    } else {
        $File = $ARGV[0];
    }

    exit 99 if !$res;
}

sub run {
    # check that file exists
    (-f $File) or die "inplace: File '$File' does not exist or not a file\n";

    # create a temporary file
    my ($tempfile, $tempfh);
    my $i = 0;
    while (1) {
        $i++;
        my $rand = sprintf("%06d", 10_000*rand());
        $tempfile = "$File.$rand";
        last if sysopen $tempfh, $tempfile, O_WRONLY | O_CREAT | O_EXCL;
        if ($i > 100) {
            die "inplace: Can't create tempfile '$tempfile' after 100 retries: $!\n";
        }
    }

    # run command
    IPC::Run::run([$Command, @ARGV], \*STDIN, $tempfh, \*STDERR)
        or die "inplace: Command '$Command' failed ($?), not replacing file '$File'\n";

    close $tempfh
        or die "inplace: Failed writing to tempfile '$tempfile': $!\n";

    # if there is a backup extension, move the original file to backup
    if (defined $Opts{backup} && $Opts{backup} ne '') {
        my $bakfile = "$File$Opts{backup}";
        rename $File, $bakfile
            or die "inplace: Failed moving '$File' to '$bakfile': $!\n";
    }

    # replace original file with temporary file
    rename $tempfile, $File
        or die "inplace: Failed replacing '$File' with '$tempfile': $!\n";
}

# MAIN

parse_cmdline();
run();

1;
# ABSTRACT: Replace file with output of command
# PODNAME: inplace

__END__

=pod

=encoding UTF-8

=head1 NAME

inplace - Replace file with output of command

=head1 VERSION

This document describes version 0.001 of inplace (from Perl distribution App-inplace), released on 2019-02-21.

=head1 SYNOPSIS

 % inplace [INPLACE_OPTS] <COMMAND> <FILE> [CMD_OPTS]...

Example:

 % inplace csv2ansitable myfile.txt

If command is successful, then F<myfile.txt> will contain the output of the
command. F<myfile.txt~> will contain the original content. The file to be
replaced must be specified as the first argument to the command.

If you do not want any backup:

 % inplace -b csv2ansitable myfile.txt

If you want another backup extension other than the default C<~>:

 % inplace -b.bak csv2ansitable myfile.txt

If the file is not the first argument of the command, you can use C<-->:

 % inplace csv2ansitable -t -- myfile.txt

=head1 DESCRIPTION

B<inplace> is a command wrapper to give "in-place editing" capability to another
command. Sometimes a command that manipulates file does not allow you to
overwrite an input file or even specify an output path; they output to stdout.
With B<inplace>, you can replace the content of the input file with the output
of the command in one go.

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 BUGS

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

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

B<sponge>, but with this command you need to specify the filename twice (but
useful when your command is a pipeline), e.g.:

 % sed '...' FILE | grep '...' | sponge FILE

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2019 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
