#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;

use Picture::Lock::CLI ();

exit Picture::Lock::CLI->run(@ARGV);

__END__

=head1 NAME

piclock - lock a directory, a file or bytes with pictures and a handful of clicks

=head1 SYNOPSIS

    piclock lock Accounts --image harbour.jpg
    piclock unlock Accounts.plock --image harbour.jpg

    piclock lock notes.txt --image harbour.jpg --image cat.png --bake
    piclock unlock notes.txt.plock

    echo 'the code is 4812' | piclock lock - --to code.plock --image harbour.jpg
    piclock unlock code.plock --to - --image harbour.jpg

    piclock lock   PATH [--to FILE] --image IMG [clicks] [passphrase] [--bake]
                        [--radius PX] [--memory MIB] [--passes N] [--remove]
    piclock lock   -     --to FILE  --image IMG [clicks] [passphrase] [--bake] ...
    piclock unlock FILE [--to PATH] [--image IMG] [clicks] [passphrase]
    piclock verify FILE             [--image IMG] [clicks] [passphrase]
                        [--portable]
    piclock list   FILE             [--image IMG] [clicks] [passphrase]
    piclock rekey  FILE             [--image IMG] [clicks] [passphrase]
                        [--new-image IMG] [--new-clicks[=X,Y:X,Y:...]]
                        [--bake | --no-bake]
                        [--new-passphrase | --new-passphrase-fd N | --no-passphrase]
                        [--radius PX] [--memory MIB] [--passes N]
    piclock pictures FILE [--to DIR]
    piclock info   FILE
    piclock calibrate [--seconds N] [--passes N] [--most MIB]
    piclock --help | --version

=head1 DESCRIPTION

A directory and everything in it becomes one file, which opens with a picture
and an ordered handful of clicks on it. This is L<Picture::Lock> from a
command line. What a locked file keeps and does not keep, what each step
proves before the next, and what a picture and clicks are worth as a secret,
are all said there.

What is locked may also be one file, or whatever comes from standard input.
What opens it may be up to eight pictures in an order, each with clicks of
its own. And the pictures may be put inside the locked file, which then opens
with clicks alone.

B<The picture is its bytes.> Keep the file that was used, exactly as it is.
The same picture saved again, resized or stripped of its metadata is another
picture, and it is the first that opens the file. The command says so at
every lock.

=head1 WHAT IT DOES

=over 4

=item lock PATH

Locks the directory into C<PATH.plock>, or into the file C<--to> names. The
file is written beside its name, read back whole with the same picture and
clicks, and only then given its name. With C<--remove> the directory is taken
away afterwards, each thing in it only if it is byte for byte what the locked
file holds.

C<PATH> may be one file, and the command looks to see which it is. A file is
locked with its bytes, its permission bits and its time. Its name is not
kept, and C<--remove> is not for it.

=item lock -

Locks what comes from standard input, at most 64 MiB of it, into the file
C<--to> names. The clicks are then not read from standard input: they are
given with C<--clicks> or asked for by the picker.

    pg_dump accounts | piclock lock - --to accounts.plock --image harbour.jpg

=item unlock FILE

Unlocks the file into what it holds: C<FILE> with its C<.plock> taken off, or
the name C<--to> gives. A file that holds a directory makes a directory, one
that holds a file makes a file, and bytes make a file that is its owner's
alone. C<--to -> writes bytes to standard output and nothing else there.

    piclock unlock accounts.plock --to - --image harbour.jpg | psql accounts

A time in the file that this system's clock cannot hold is set to the nearest
it can. The command says so on standard error, and still ends with 0.

=item verify FILE

Reads the file whole and writes nothing.

With C<--portable> it also says what in the file another system would not
take as it is, a line for each after the line that says the file is sound:
two names that are one where capital and small letters are not told apart, a
name that Windows cannot hold, a link, a time that a clock of 32 bits has
not. It then ends with status 3. L<Picture::Lock/verify> has the word that
begins each line.

=item list FILE

What the file holds, a line an entry in the order of the tree: C<d>, C<f> or
C<l> for a directory, a file or a link, the permission bits, the size, when
it was last modified, and the path. The directory itself is C<.> and a link
has C< -E<gt> > and its target after it. A byte of a path that is a control
character or a backslash is written C<\xHH>.

A file that holds one file is one line, an C<f> with C<.> for its name. One
that holds bytes is one line, a C<b>, with dashes where bytes have no
permission bits and no time.

=item rekey FILE

Changes what opens the file and leaves its contents alone. C<--new-image> is
another picture, C<--new-clicks> other clicks, and what is not named anew
stays as it was, the passphrase too. C<--no-passphrase> takes the passphrase
off. New clicks are joined to their option, as C<--new-clicks=X,Y:X,Y:...>,
and C<--new-clicks> with nothing joined to it asks the picker for them.

A rekey changes the copy it is given. A copy of the file that was made before
it still opens with what opened it then.

C<--new-image> may be given several times, for a file that is to take several
pictures, and C<--new-clicks=...> then once for each. C<--bake> puts the
pictures inside the file and C<--no-bake> takes them out again. A directory
locked with one picture that stays outside is rekeyed where it stands, and
takes the same time whatever its size. Every other rekey makes a new file
beside the old, reads it back, and gives it the old file's name in one step.

=item pictures FILE

Writes the pictures a file has inside it into the directory C<--to> names, or
the one it is run in, as C<picture-1.png>, C<picture-2.jpg> and so on, each
with the ending of what it is. It takes no picture and no clicks, since the
pictures are not secret. It writes over nothing, and where one of the names
is taken it writes none of them.

=item info FILE

What the file says to anyone, with no picture and no clicks: what it holds,
how many pictures it takes, how many clicks and what radius each has, and
whether they are inside it.

=item calibrate

Tries this machine and changes nothing. It finds the memory at which one try
at opening a file takes about C<--seconds>, which is 1 unless it is given,
and prints that cost, the options that set it, and what the default takes
here.

    $ piclock calibrate
    881 MiB and 3 passes: one try at opening a file takes 0.93 seconds here.
      --memory 881 --passes 3
    The default is 256 MiB and 3 passes, which takes 0.30 seconds here.
    A file locked at a cost asks it of every machine that opens it. Choose for the slowest that will.

That was an Apple M5. Its tries are made in one process, and a command that
opens a file touches its memory for the first time: at that cost it took
1.05 seconds there.

It asks for no more memory than C<--most>, in MiB, nor for more than half of
what the system says is free, and it says so where one of those stopped it
short of the time asked. C<--passes> is the passes the file will have. It
takes about five times the seconds asked.

B<A locked file asks its cost of every machine that opens it.> Run this on
the slowest machine that will open the file, and lock with what it says
there.

=back

Nothing that is there is ever written over, by any of them.

=head1 PICTURES

C<--image> is given once for each picture, up to eight, in the order they are
to be asked for again. One picture alone takes 4 to 16 clicks. One of several
takes 1 to 16, and all of them together 4 or more. Every picture and every
click goes into one key: a wrong picture and a wrong click, first or last,
fail the same way and after the same work, and nothing says which it was. The
file says how many pictures it takes and how many clicks on each. Which
picture goes where is yours to remember.

    piclock lock Accounts --image harbour.jpg --image cat.png --image map.webp

C<--bake> puts the pictures inside the locked file. It then takes no
C<--image>, and C<piclock unlock FILE> is the whole command.

B<A picture inside the file can be seen by anyone who has the file>, with
C<piclock pictures>, and its clicks are what keep it shut. The command says
so at every such lock. With its pictures inside and no
passphrase a file takes 8 clicks or more, and asks for 1 GiB of memory unless
C<--memory> says otherwise. A picture that goes inside is at most 32 MiB, and
all of them 64 MiB together.

=head1 CLICKS

A click is a pixel of the picture as it is stored: C<x> counted from its left
edge and C<y> from its top, both from 0. They come from the first of these
that there is.

=over 4

=item --clicks X,Y:X,Y:...

On the command line, once for each picture and in their order. Where a file
is opened it may be given once with the clicks of every picture one after
another, and the file, which says how many each takes, deals them out. B<An
argument stays in the shell's history, and anyone on the machine can see it
in the list of what is running.> It is there for scripts and for tests, and
the command says so when it is used at a terminal.

=item --pick

The picker, whatever standard input is.

=item standard input

When it is not a terminal: one C<X,Y> a line, and a line with nothing on it
between the clicks of two pictures.

    piclock unlock Accounts.plock --image harbour.jpg < clicks

=item the picker

When standard input is a terminal. The picture is shown in the terminal, as
a picture where the terminal can show one and in characters where it cannot,
and is clicked with the mouse or with the keys: see
L<Picture::Lock::Picker>. The first press brings the place pressed closer
and the second is the click. At a lock the clicks are asked for twice. Where
there are several pictures each is shown in turn, and at a lock each in turn
and then each again, so that the order is rehearsed with the places. A file
that has its pictures inside shows its own.
C<--marks> draws a mark where each click fell, which helps the hand: it is for
a screen that nobody else can see. C<--zoom> looks closer still at the first
press: 2, 4 or 8 times.

=back

Clicks that are not written that way are refused before anything else is
done.

=head1 A PASSPHRASE

C<--passphrase> asks for one at the terminal and shows nothing of what is
typed. At a lock it asks twice. C<--passphrase-fd N> reads one line from an
open descriptor:

    piclock lock Accounts --image harbour.jpg --passphrase-fd 3 3< phrase

A passphrase is 1 to 1024 bytes and is used exactly as given. It is never
taken from an argument. With one, the picture, the clicks and the passphrase
are all needed to open the file.

=head1 WHAT A GUESS COSTS

C<--memory>, in MiB, and C<--passes> are the cost of each try at opening the
file: 256 MiB and 3 unless they are given. C<--radius> is how many pixels off
a later click may be, and is chosen from the picture's size unless it is
given. C<piclock calibrate> says what a cost takes on the machine it is run
on. L<Picture::Lock/What a lock costs> has the rest.

=head1 EXIT STATUS

=over 4

=item C<0>

Done.

=item C<1>

What was offered does not open the file: the wrong picture, the wrong clicks
or the right ones in the wrong order, the wrong passphrase, or one where none
is expected. So are another number of pictures than the file takes, and a
picture given to a file that has its own.

=item C<2>

The file is damaged, or is not a locked file, or was made by a later version.

=item C<3>

Refused, and nothing was made: a name that is taken, a directory that holds
what cannot be kept, clicks that cannot be enrolled, a picture that cannot be
read or cannot go inside, a file or a directory asked for on standard output,
a picker that was left before the clicks were made. And C<verify --portable>
of a file that holds something another system would not take.

=item C<4>

Usage: the command line or the form of the clicks is wrong, or there is no
way here to ask for clicks.

=item C<5>

The machine: a file could not be read or written, memory could not be had,
or there were no random bytes.

=back

A script can tell a wrong secret, which is 1, from a broken backup, which is
2.

=head1 ENVIRONMENT

C<PICLOCK_PICTURES> and C<NO_COLOR> change how the picker draws. Both are in
L<Picture::Lock::Picker/ENVIRONMENT>.

=head1 SEE ALSO

L<Picture::Lock>, L<Picture::Lock::CLI>, L<Picture::Lock::Picker>,
L<Picture::Lock::Format>

=head1 AUTHOR

LNATION <email@lnation.org>

=head1 LICENSE AND COPYRIGHT

This software is Copyright (c) 2026 by LNATION <email@lnation.org>.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut
