#!/usr/bin/env perl
use Mojo::Base -signatures, -strict;

use Mojo::Util qw(extract_usage getopt);
use Mojo::File qw(path);

my $daemon_class = $ENV{MOJODCTL_CONTROL_CLASS} || 'Mojo::Server::DaemonControl';
die $@ unless eval "require $daemon_class; 1";
my $daemon = $daemon_class->new;

my $maybe = sub ($daemon, $name, $val) { $daemon->can($name) && $daemon->$name($val) };

getopt
  'h|help'                 => \my $help,
  'm|mode=s'               => \$ENV{MOJO_MODE},
  'G|graceful-timeout=i'   => sub { $daemon->$maybe(graceful_timeout   => $_[1]) },
  'I|heartbeat-interval=i' => sub { $daemon->$maybe(heartbeat_interval => $_[1]) },
  'H|heartbeat-timeout=i'  => sub { $daemon->$maybe(heartbeat_timeout  => $_[1]) },
  'l|listen=s'             => \my @listen,
  'P|pid-file=s'           => sub { $daemon->$maybe(pid_file => path($_[1])) },
  'R|reload'               => \my $reload,
  'w|workers=i'            => sub { $daemon->$maybe(workers => $_[1]) };

die extract_usage if $help || !(my $app = shift);

$daemon->listen([map { Mojo::URL->new($_) } @listen]) if @listen;
_exit($reload ? $daemon->reload($app) : $daemon->run($app));

sub _exit ($code) { $code //= 0; defined wantarray ? $code : exit $code }

=encoding utf8

=head1 NAME

=head1 SYNOPSIS

  Usage: mojodctcl [OPTIONS] [APPLICATION]

    # Start a manager
    mojodctcl ./script/my_app
    mojodctcl -m production -l http://127.0.0.1:8080 ./myapp.pl
    mojodctcl -P /tmp/myapp.pid ./myapp.pl

    # Hot deploy a running manager
    mojodctcl -R -P /tmp/myapp.pid ./myapp.pl

  Options:
    -G, --graceful-timeout <seconds>     Graceful timeout, defaults to 120.
    -I, --heartbeat-interval <seconds>   Heartbeat interval, defaults to 5
    -H, --heartbeat-timeout <seconds>    Heartbeat timeout, defaults to 50
    -l, --listen <location>              One or more locations you want to listen on,
                                         defaults to "http://*:8080"
    -m, --mode <name>                    Operating mode for your application,
                                         defaults to the value of
                                         MOJO_MODE/PLACK_ENV or "development"
    -P, --pid-file <path>                Path to process id file, defaults to
                                         "mojodctl.pid" in a temporary directory
    -R, --reload                         Used to hot deploy a running manager
    -w, --workers <number>               Number of workers, defaults to 4
    -h, --help                           Show this summary of available options

=cut
