#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use Getopt::Long;

our $VERSION = '0.03'; # VERSION

my %opts = (
    log_stderr_size      => 10*1024*1024,
    log_stderr_histories => 10,
    pid_dir              => "/var/run",
);
Getopt::Long::Configure("no_permute");
GetOptions(
    "--help"                   => \$opts{help},

    "--name=s"                 => \$opts{name},
    "--single-instance"        => \$opts{single_instance},
    "--log-stderr-dir=s"       => \$opts{log_stderr_dir},
    "--log-stderr-size=i"      => \$opts{log_stderr_size},
    "--log-stderr-histories=i" => \$opts{log_stderr_histories},
    "--log-stderr-period=s"    => \$opts{log_stderr_period},
    "--timeout=i"              => \$opts{timeout},
    "--pid-dir=s"              => \$opts{pid_dir},
    "--on-multiple-instance=s" => \$opts{on_multiple_instance},
);

if ($opts{help} || !@ARGV) {
    print <<_;
Run child process and govern its various aspects

Usage: $0 [OPTS] -- [COMMAND ...]

Options:
  --name=S                  Set process name.
  --single-instance         Prevent running multiple instances simultaneously.
  --log-stderr-dir=S        Enable logging of stderr and set log directory.
  --log-stderr-size=N       Set log file size limit, in bytes, default 10MB.
  --log-stderr-histories=N  Set number of rotated log files to keep, default 10.
  --log-stderr-period=S     Enable periodic rotation (daily/monthly/yearly).
  --timeout=N               Set execution time limit, in seconds.
  --pid-dir=S               Directory to put PID file, if --single-instance set.
  --on-multiple-instance=S  Can be set to 'exit' to silently exit when another
                              instance is already running.
_
    exit 0;
}

require Process::Govern;

my %ga = (
    command         => \@ARGV,
    name            => $opts{name},
    single_instance => $opts{single_instance},
    pid_dir         => $opts{pid_dir},
    timeout         => $opts{timeout},
    on_multiple_instance => $opts{on_multiple_instance},
);
if ($opts{log_stderr_dir}) {
    $ga{log_stderr} = {
        dir       => $opts{log_stderr_dir},
        size      => $opts{log_stderr_size},
        histories => $opts{log_stderr_histories},
        period    => $opts{log_stderr_period},
    };
}
Process::Govern::govern_process(%ga);

1;
# ABSTRACT: Run child process and govern its various aspects
# PODNAME: govproc


__END__
=pod

=head1 NAME

govproc - Run child process and govern its various aspects

=head1 VERSION

version 0.03

=head1 SYNOPSIS

 % govproc [opts] COMMAND ...

See C<govproc --help> for list of options.

=head1 DESCRIPTION

This script is a command-line interface for L<Process::Govern>.

=head1 SEE ALSO

L<Process::Govern>

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Steven Haryanto.

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

