#!/usr/bin/env perl

use strict;
use warnings;

use File::Basename 'dirname';
use File::Spec;

push @INC, join('/', File::Spec->splitdir(dirname(__FILE__)), '..', 'lib');
unshift @INC, join('/', File::Spec->splitdir(dirname(__FILE__)), '..', 'lib')
  if $ENV{TEST_HYPNOTOAD};

# Check if Mojolicious is installed
die <<EOF unless eval 'use Mojo::Server::Hypnotoad; 1';
It looks like you don't have the Mojolicious framework installed.
Please visit http://mojolicio.us for detailed installation instructions.

EOF

use Getopt::Long qw/GetOptions :config no_auto_abbrev no_ignore_case/;

# "Hey sexy mama, wanna kill all humans?"
my $toad   = Mojo::Server::Hypnotoad->new;
my $config = 'hypnotoad.conf';
my $help;
GetOptions(

  # DEPRECATED in Leaf Fluttering In Wind!
  'c|config=s' => sub { $config = $_[1] },

  'f|foreground' => sub { $ENV{HYPNOTOAD_FOREGROUND} = 1 },
  'h|help'       => sub { $help                      = 1 },
  's|stop'       => sub { $ENV{HYPNOTOAD_STOP}       = 1 },
  't|test'       => sub { $ENV{HYPNOTOAD_TEST}       = 1 }
);
$help = 1 unless my $app = shift || $ENV{HYPNOTOAD_APP};

# Usage
die <<"EOF" if $help;
usage: $0 [OPTIONS] [APPLICATION]

  hypnotoad script/myapp
  hypnotoad myapp.pl
  hypnotoad -f myapp.pl

These options are available:
  -f, --foreground      Keep manager process in foreground.
  -h, --help            Show this message.
  -s, --stop            Stop server gracefully.
  -t, --test            Test application/configuration and exit.
EOF

# "This is it.
#  The moment we should've trained for."
$toad->run($app, $config);

__END__

=head1 NAME

hypnotoad - Hypnotoad HTTP 1.1 and WebSocket server

=head1 SYNOPSIS

  $ hypnotoad --help
  $ hypnotoad myapp.pl

=head1 DESCRIPTION

Start L<Mojolicious> and L<Mojolicious::Lite> applications with the
L<Mojo::Server::Hypnotoad> web server.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.

=cut
