#! /usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
use Server::Starter qw(start_server);

sub usage {
    print <<"EOT";
Usage: $0 [options] server-prog server-arg1 server-arg2 ...

Example:
  start_server --port=80 -- my_server
                           listen to port 80 and start my_server

  start_server -- plackup -s FCGI --listen=/tmp/fcgi.sock ...
                           start Plack using FCGI backend binding to a unix
                           socket

Options:
  --port=(port|host:port)  TCP port to listen to (if omitted, will not bind
                           to any ports)
  --interval=seconds       minimum interval to respawn the server process
                           (default: 1)
  --pid-file=file.pid      write process id of start_server to the file
                           (default: do not write)

EOT
    exit 0;
}

my (@opt_port, $opt_interval, $opt_log_file, $opt_pid_file, $opt_help,
    $opt_version);

GetOptions(
    'port=s'     => \@opt_port,
    'interval=i' => \$opt_interval,
    'log-file=s' => \$opt_log_file,
    'pid-file=s' => \$opt_pid_file,
    help         => \$opt_help,
    version      => \$opt_version,
) or exit 1;
usage()
    if $opt_help;
if ($opt_version) {
    print "$Server::Starter::VERSION\n";
    exit 0;
}

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

start_server(
    exec => \@ARGV,
    port => \@opt_port,
    ($opt_interval ? (interval => $opt_interval) : ()),
    ($opt_log_file ? (log_file => $opt_log_file) : ()),
    ($opt_pid_file ? (pid_file => $opt_pid_file) : ()),
);

__END__

=head1 NAME

start_server - a superdaemon for hot-deploying server programs

=head1 SYNOPSIS

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

=head1 DESCRIPTION

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

Use --help to find out how to use the script.

=head1 AUTHOR

Kazuho Oku E<lt>kazuhooku@gmail.comE<gt>
Copyright (C) 2009 Cybozu Labs, Inc.

=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
