#!/usr/pkg/bin/perl 

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;
use Server::Starter qw(start_server restart_server stop_server);

my %opts = (
    port => [],
    path => [],
);

GetOptions(
    map {
        $_ => do {
            my $name = (split '=', $_, 2)[0];
            $name =~ s/-/_/g;
            $opts{$name} ||= undef;
            ref($opts{$name}) ? $opts{$name} : \$opts{$name};
        },
    } qw(port=s path=s interval=i log-file=s pid-file=s dir=s signal-on-hup=s signal-on-term=s
         backlog=i envdir=s enable-auto-restart daemonize auto-restart-interval=i kill-old-delay=i
         status-file=s restart stop help version),
) or exit 1;
pod2usage(
    -exitval => 0,
    -verbose => 1,
) if $opts{help};
if ($opts{version}) {
    print "$Server::Starter::VERSION\n";
    exit 0;
}

if ($opts{restart}) {
    restart_server(%opts);
    exit 0;
}

if ($opts{stop}) {
    stop_server(%opts);
    exit 0;
}

if ($opts{daemonize}) {
    die "Usage: --log-file option must be specified together with --deamonize\n"
        unless defined $opts{log_file};
}

# validate options
die "server program not specified\n"
    unless @ARGV;

start_server(
    %opts,
    exec => \@ARGV,
);

__END__

=head1 NAME

start_server - a superdaemon for hot-deploying server programs

=head1 SYNOPSIS

  start_server [options] -- server-prog server-arg1 server-arg2 ...

  # start Plack using Starlet listening at TCP port 8000
  start_server --port=8000 -- plackup -s Starlet --max-workers=100 index.psgi

=head1 DESCRIPTION

This script is a frontend of L<Server::Starter>.  For more information please refer to the documentation of the module.

=head1 OPTIONS

=head2 --port=(port|host:port|port=fd|host:port=fd)

TCP port to listen to (if omitted, will not bind to any ports)

If host is not specified, then the program will bind to the default address of IPv4 ("0.0.0.0").
Square brackets should be used to specify an IPv6 address (e.g. --port=[::1]:8080)

If fd is specified, then start_server allocates the socket at the given number.

The command binds to UDP ports if the port numbers are prefixed by "u" (e.g., --port=u443).

=head2 --path=path

path at where to listen using unix socket (optional)

=head2 --dir=path

working directory, start_server do chdir to before exec (optional)

=head2 --interval=seconds

minimum interval to respawn the server program (default: 1)

=head2 --signal-on-hup=SIGNAL

name of the signal to be sent to the server process when start_server receives a SIGHUP (default: SIGTERM). If you use this option, be sure to also use C<--signal-on-term> below.

=head2 --signal-on-term=SIGNAL

name of the signal to be sent to the server process when start_server receives a SIGTERM (default: SIGTERM)

=head2 --pid-file=filename

if set, writes the process id of the start_server process to the file

=head2 --status-file=filename

if set, writes the status of the server process(es) to the file

=head2 --envdir=ENVDIR

directory that contains environment variables to the server processes and superdaemon.
It is inspired by C<envdir> in C<daemontools>.
This can be overwritten by environment variable C<ENVDIR>.

=head2 --log-file=file

=head2 --log-file="| cmd args..."

if set, redirects STDOUT and STDERR to given file or command

=head2 --daemonize

deamonizes the server (by doing fork,setsid,fork).  Must be used together with C<--log-file>.

=head2 --enable-auto-restart

enables automatic restart by time.
This can be overwritten by environment variable C<ENABLE_AUTO_RESTART>.

=head2 --auto-restart-interval=seconds

automatic restart interval (default 360). It is used with C<--enable-auto-restart> option.
This can be overwritten by environment variable C<AUTO_RESTART_INTERVAL>.

=head2 --kill-old-delay=seconds

time to suspend to send a signal to the old worker. The default value is 5 when C<--enable-auto-restart> is set, 0 otherwise.
This can be overwritten by environment variable C<KILL_OLD_DELAY>.

=head2 --backlog=size

specifies a listen backlog parameter, whose default is SOMAXCONN (usually 128 on Linux). While SOMAXCONN is enough for most loads, large backlog is required for heavy loads.

=head2 --restart

this is a wrapper command that reads the pid of the start_server process from --pid-file, sends SIGHUP to the process and waits until the server(s) of the older generation(s) die by monitoring the contents of the --status-file

=head2 --stop

this is a wrapper command that reads the pid of the start_server process from --pid-file, sends SIGTERM to the process.

=head2 --help

prints this help

=head2 --version

prints the version number

=head1 AUTHOR

Kazuho Oku

=head1 SEE ALSO

L<Server::Starter>

=head1 LICENSE

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

=cut
