package App::dozo;

our $VERSION = "1.01";

1;
=encoding utf-8

=for html <p align="center"><img src="https://raw.githubusercontent.com/tecolicom/App-dozo/main/images/dozo-logo.png" width="400"></p>

=head1 NAME

dozo - Dôzo, Docker with Zero Overhead

=head1 SYNOPSIS

dozo -I IMAGE [ options ] [ command ... ]

    -h, --help         show help
        --version      show version
    -d, --debug        debug mode (show full command)
    -x, --trace        trace mode (set -x)
    -q, --quiet        quiet mode
    -n, --dryrun       dry-run mode

    -I, --image=#      Docker image (required unless -D)
    -D, --default      use default image
    -E, --env=#        environment variable to inherit (repeatable)
    -W, --mount-cwd    mount current working directory
    -H, --mount-home   mount home directory
    -U, --unmount      do not mount any directory
        --mount-mode=# mount mode (rw or ro, default: rw)
    -R, --mount-ro     mount read-only (shortcut for --mount-mode=ro)
    -V, --volume=#     additional volume to mount (repeatable)
    -B, --batch        batch mode (non-interactive)
    -L, --live         use live (persistent) container
    -N, --name=#       live container name
    -K, --kill         kill and remove existing container
    -P, --port=#       port mapping (repeatable)
    -O, --other=#      additional docker options (repeatable)

=head1 VERSION

Version 1.01

=head1 USAGE

When executed without arguments, Dôzo starts an interactive shell
inside the container.  When arguments are given, they are executed as
a command.

    dozo -I alpine                  # start shell
    dozo -I alpine ls -la           # run command

By setting C<-D> or your favorite image with C<-I> in F<~/.dozorc>,
you can simply run Dôzo without specifying an image.  Since the git
top directory is automatically mounted, git commands work as expected
from anywhere in the tree.

    $ dozo                          # start shell
    $ dozo git log -p               # run git log -p

With C<-L> option, you can use a persistent container.  Tools
installed in the container will remain available for subsequent use.

    $ dozo -L                       # start shell and create container
    # apt update && apt install -y cowsay
    # exit
    $ dozo -L /usr/games/cowsay Dôzo
     ______
    < Dôzo >
     ------
            \   ^__^
             \  (oo)\_______
                (__)\       )\/\
                    ||----w |
                    ||     ||

=cut
=head1 INSTALLATION

Using L<cpanminus|https://metacpan.org/pod/App::cpanminus>:

    cpanm -n App::dozo

To install the latest version from GitHub:

    cpanm -n https://github.com/tecolicom/App-dozo.git

Alternatively, you can simply place C<dozo> and C<getoptlong.sh> in
your PATH.

B<Dôzo> requires Bash 4.4 or later, as does the C<getoptlong.sh>
script it uses.

=head1 DESCRIPTION

B<Dôzo> is a generic Docker runner that simplifies running commands in
Docker containers.  The name comes from the Japanese word "dôzo"
(どうぞ) meaning "please" or "go ahead", and also stands for "B<D>ocker
with B<Z>ero B<O>verhead".  The command name is C<dozo> for ease of
typing.

It automatically configures the tedious Docker options such as volume
mounts, environment variables, working directories, and interactive
terminal settings, so you can focus on the command you want to run.

B<Dôzo> is distributed as a standalone module and can be used as a
general-purpose Docker runner. It was originally developed as part of
L<App::Greple::xlate> and is used by L<xlate> for Docker operations.

B<Dôzo> uses L<getoptlong.sh|https://github.com/tecolicom/getoptlong>
for option parsing.

=head2 Key Features

=over 4

=item B<Git Friendly>

If you are working in a git environment, the git top directory is
automatically mounted. Otherwise the current directory is mounted.
When the current directory is under the git top directory, the
corresponding subdirectory in the container is used as the working
directory.

=item B<Live Container>

Use C<-L> to create or attach to a persistent container that survives
between invocations. Container names are automatically generated from
the image name and mount directory.

=item B<Environment Inheritance>

Common environment variables are automatically inherited: C<LANG>,
C<TZ>, proxy settings, terminal settings, and API keys for AI/LLM
services (DeepL, OpenAI, Anthropic, Perplexity).

=item B<Flexible Mounting>

The directory is mounted on F</work> in the container, and it is used
as the working directory. Various mount options are available: current
directory (C<-W>), home directory (C<-H>), additional volumes (C<-V>),
read-only mode (C<-R>), or no mount (C<-U>).

=item B<X11 Support>

When C<DISPLAY> is set, the address of the interface used for the
default route is detected and passed to the container as C<DISPLAY>,
enabling GUI applications.

=item B<Configuration File>

Use C<.dozorc> to set default options. Loaded from the home directory,
the git top directory, and the current directory, in that order, so
that a closer file takes precedence.

=item B<Standalone Operation>

B<Dôzo> can operate independently of L<xlate>. The C<getoptlong.sh>
script is provided by the L<Getopt::Long::Bash> CPAN distribution,
which is installed as a dependency. Alternatively, you can place
C<getoptlong.sh> in your C<PATH> manually.

=back

=head1 OPTIONS

Repeatable options (B<-E>, B<-V>, B<-P> and B<-O>) can be given more
than once, and a single value is also split into multiple elements at
spaces, tabs and commas.  Thus C<-P 8000,9000> gives two port mappings,
and B<-O> with the value C<--memory 2g> gives two docker options.  As a
consequence, a value containing a space cannot be given as a single
element: B<-E> with the value C<MSG=hello world> defines two variables,
C<MSG=hello> and C<world>.

=over 7

=item B<-h>, B<--help>

Show help message.

=item B<--version>

Show version number.

=item B<-d>, B<--debug>

Enable debug mode. Shows the full docker command line that will be executed.

=item B<-x>, B<--trace>

Enable trace mode (set -x).

=item B<-q>, B<--quiet>

Quiet mode. Suppress the informational line printed to standard error
before the container is started:

    docker run image=alpine env=13 command=echo hi

It summarizes the image name, the number of environment variables to be
inherited, the container name (with B<-L>) and the command to be
executed. This line is shown regardless of B<-n>; use B<-d> to see the
whole docker command line instead.

=item B<-n>, B<--dryrun>

Dry-run mode. Show docker commands without executing them.
Useful for testing and debugging.

=item B<-I> I<image>, B<--image>=I<image>

Specify Docker image. Required unless C<-D> is given, but you can put
it in F<.dozorc> so you don't have to type it every time.

=item B<-D>, B<--default>

Use the default Docker image. If C<DOZO_DEFAULT_IMAGE> environment
variable is set, use that image. Otherwise, use C<tecolicom/xlate>,
that is, its C<latest> tag. See L</DEFAULT IMAGE> section for details
about the default image.

=item B<-E> I<name>[=I<value>], B<--env>=I<name>[=I<value>]

Specify environment variable to pass to the container. If I<value> is
omitted, the value is inherited from the host environment. Repeatable.

=item B<-W>, B<--mount-cwd>

Mount current working directory.

=item B<-H>, B<--mount-home>

Mount home directory. The C<HOME> environment variable in the
container is set to the mount point (F</work>).

=item B<-U>, B<--unmount>

Do not mount any directory.

=item B<--mount-mode>=I<mode>

Set mount mode of the automatically mounted directory. I<mode> is
either C<rw> (read-write, default) or C<ro> (read-only). This does not
affect volumes given by B<-V>; append C<:ro> to them instead.

=item B<-R>, B<--mount-ro>

Mount directory as read-only. Shortcut for C<--mount-mode=ro>.

=item B<-V> I<path>, B<-V> I<from>:I<to>, B<--volume>=I<from>:I<to>

Specify additional directory to mount. If only I<path> is given
(without C<:>), it is mounted to the same path in the container; a
relative path is resolved against the current directory. Repeatable.

=item B<-B>, B<--batch>

Run in batch mode (non-interactive).

=item B<-L>, B<--live>

Use live (persistent) container.

=item B<-N> I<name>, B<--name>=I<name>

Specify container name explicitly.

=item B<-K>, B<--kill>

Kill and remove existing container.

=item B<-P> I<port>, B<-P> I<host>:I<container>, B<--port>=I<host>:I<container>

Specify port mapping. If only I<port> is given (without C<:>), it is
mapped to the same port in the container (e.g., C<-P 8000> becomes
C<8000:8000>, and C<-P 8000/udp> becomes C<8000:8000/udp>).
Repeatable.

=item B<-O> I<option>, B<--other>=I<option>

Specify additional docker options. Repeatable.

=back

=head1 INTERACTIVE MODE

Unless the B<-B> (batch) option is given, the container is started
with Docker's C<--interactive> option, and C<--tty> is added as well
when the standard input is a terminal (TTY). The same rule is applied
to C<docker exec> in live container mode.

This allows seamless interactive use when attaching to containers or
running interactive commands.

=head1 LIVE CONTAINER

The C<-L> option enables live (persistent) container mode. Unlike
normal mode where containers are removed after execution (C<--rm>),
live containers persist between invocations, allowing you to maintain
state and reduce startup overhead.

=head2 Container Lifecycle

When C<-L> is specified, B<Dôzo> behaves as follows:

=over 4

=item 1. B<Container does not exist>

Create a new persistent container (without C<--rm> flag).

=item 2. B<Container exists and is running>

If a command is given, execute it using C<docker exec>. Otherwise,
attach to the container using C<docker attach>.

=item 3. B<Container exists but is paused>

Unpause the container with C<docker unpause>, then proceed as above.

=item 4. B<Container exists but is not started>

If the container is in the C<created> or C<exited> state, start it
with C<docker start>, then proceed as above.

=back

=head2 Container Naming

Container names are automatically generated in the format:

    <image_name>.<mount_directory>

For example, if you run:

    dozo -I tecolicom/xlate -L

from C</home/user/project>, the container name would be
C<xlate.project>.

You can override the auto-generated name using the C<-N> option:

    dozo -I tecolicom/xlate -L -N mycontainer

=head2 Managing Live Containers

=over 4

=item B<Attach to existing container>

    dozo -I myimage -L

If no command is given, attaches to the container's main process.

=item B<Execute command in existing container>

    dozo -I myimage -L ls -la

Runs the command in the existing container using C<docker exec>.
Environment variables are passed just as for a new container, and when
the current directory is under the mounted directory, the
corresponding subdirectory is used as the working directory.

=item B<Kill and recreate container>

    dozo -I myimage -KL

The C<-K> option removes the existing container before C<-L> creates
a new one. Useful when you need a fresh container state.

=item B<Kill container only>

    dozo -I myimage -K

Without C<-L>, the container is removed and the command exits.

=back

=head1 CONFIGURATION FILE

C<.dozorc> files are loaded from the following locations in order:

=over 4

=item 1. Home directory C<.dozorc>

=item 2. Git top directory C<.dozorc> (if different)

=item 3. Current directory C<.dozorc>

=item 4. Command line arguments

=back

For single-value options (like C<-I>, C<-N>), later values override
earlier ones. For repeatable options (like C<-E>, C<-V>, C<-P>, C<-O>),
all values are accumulated in order.

You can use any command line option in the configuration file:

    # Example .dozorc
    -I tecolicom/xlate:latest
    -E CUSTOM_VAR=value
    -V /data:/data

Lines starting with C<#> are treated as comments.  A C<#> in the middle
of a line does B<not> start a comment, and a trailing comment is not
supported.

Each line is split into arguments in the same way as the shell does, so
quotation marks have to be balanced.  A line which cannot be parsed is
an error, and B<Dôzo> stops after showing the file name, the line
number and the line itself.

Write nothing but options in the configuration file.  Its contents are
placed before the command line arguments, and option parsing stops at
the first argument which is not an option.  Everything after it,
including options given on the command line, is passed to the container
as the command to execute.  This is the rule which lets you write
C<dozo -I alpine ls -la> without C<-la> being taken as an option of
B<Dôzo>, but it also means that a stray word in F<.dozorc> disables
option parsing altogether.  With a trailing comment like this:

    -I alpine  # use alpine

C<dozo -n ls> does not show the docker command line but runs
C<# use alpine -n ls> in the container.

=head1 DOCKER-IN-DOCKER

To use Docker commands inside the container, mount the host's Docker
socket:

    # .dozorc for Docker-in-Docker
    -I docker
    -V /var/run/docker.sock

This allows you to run Docker commands from within the container using
the host's Docker daemon:

    $ dozo docker run --rm alpine uname -a

Or run it as a one-liner without C<.dozorc>:

    $ dozo -I docker -V /var/run/docker.sock docker run --rm alpine uname -a

=head1 DEFAULT IMAGE

The C<tecolicom/xlate> image is specifically designed for document
translation and text processing tasks, providing a comprehensive
environment with the following features:

=head2 Translation and AI Tools

=over 4

=item * B<DeepL CLI> - Command-line interface for DeepL translation API

=item * B<gpty> - GPT command-line tool for AI-powered text processing

=item * B<llm> - Unified LLM interface with plugins for multiple providers:
Gemini, Claude 3, Perplexity, and OpenRouter

=back

=head2 Text Processing Tools

=over 4

=item * B<greple> with xlate module - Pattern-based text extraction and
translation

=item * B<sdif> - Side-by-side diff viewer with word-level highlighting

=item * B<ansicolumn>, B<ansifold>, B<ansiexpand> - ANSI-aware text
formatting tools

=item * B<optex textconv> - Document format converter (PDF, Office, etc.)

=back

=head2 Greple Extensions

Multiple L<App::Greple> extension modules are pre-installed:

=over 4

=item * B<msdoc> - Microsoft Office document support

=item * B<xp> - Extended pattern syntax

=item * B<subst> - Text substitution with dictionary

=item * B<frame> - Frame-style output formatting

=back

=head2 Git Integration

The image includes a pre-configured git environment optimized for
document comparison and review. Since B<Dôzo> automatically mounts
the git top directory by default, git commands work seamlessly with
full repository context:

=over 4

=item * B<Side-by-side diff> - C<git diff>, C<git log>, and C<git show>
use B<sdif> for word-level side-by-side comparison

=item * B<Colorful blame> - C<git blame> uses B<greple> for enhanced
label coloring

=item * B<Office document diff> - Compare Word (.docx), Excel (.xlsx),
and PowerPoint (.pptx) files directly with git

=item * B<PDF diff> - View PDF metadata changes

=item * B<JSON diff> - Normalized JSON comparison using B<jq>

=back

=head2 Additional Utilities

=over 4

=item * B<MeCab> - Japanese morphological analyzer with IPA dictionary

=item * B<poppler-utils> - PDF processing tools (pdftotext, etc.)

=item * B<jq>, B<yq> - JSON and YAML processors

=back

=head2 Environment

=over 4

=item * Based on Ubuntu with Japanese locale (ja_JP.UTF-8)

=item * Perl and Python3 runtime environments

=item * Common API keys are automatically inherited from host
(DEEPL_AUTH_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.)

=back

=head1 ENVIRONMENT

=head2 Configuration Variables

=over 4

=item C<DOZO_DEFAULT_IMAGE>

Specifies the default Docker image used when C<-D> (C<--default>) option
is given. If not set, C<tecolicom/xlate> is used, which resolves to its
C<latest> tag.

=back

=head2 Inherited Variables

The following environment variables are inherited by default:

    LANG TZ
    HTTP_PROXY HTTPS_PROXY http_proxy https_proxy
    TERM_PROGRAM TERM_BGCOLOR COLORTERM
    DEEPL_AUTH_KEY OPENAI_API_KEY ANTHROPIC_API_KEY LLM_PERPLEXITY_KEY

=head2 Container Variables

The following environment variables are set inside the container:

=over 4

=item C<DOZO_RUNNING_ON_DOCKER=1>

Indicates the command is running inside a container started by Dôzo.

=item C<XLATE_RUNNING_ON_DOCKER=1>

For compatibility with xlate. Used to prevent recursive Docker
invocation when xlate is run inside the container.

=back

=head1 SEE ALSO

L<xlate>, L<App::Greple::xlate>

L<getoptlong.sh|https://github.com/tecolicom/getoptlong>

=head1 AUTHOR

Kazumasa Utashiro

=head1 LICENSE

Copyright © 2025-2026 Kazumasa Utashiro.

This software is released under the MIT License.
L<https://opensource.org/licenses/MIT>

=cut
