#!/usr/bin/perl
use strict;
use warnings;
#ABSTRACT: Simply deploy PSGI applications
#PODNAME: padadoy

use 5.010;
use App::Padadoy;

use Getopt::Long;
use Pod::Usage;
use Cwd;

my ($help,$version,$config,$quiet);
GetOptions(
    'help|?'   => \$help,
    'version'  => \$version,
    'config:s' => \$config,
    'quiet'    => \$quiet,
) or pod2usage(2);
pod2usage(1) if $help;

if ($version) {
    say 'This is padadoy version '.($App::Padadoy::VERSION || '??').
        '. Use -h for help.';
    exit;
}

pod2usage('Please specify a command!') unless @ARGV; 

my $cmd = shift @ARGV;
pod2usage(1) if $cmd eq 'help';

($config) = grep { -r $_ } map { "$_/padadoy.conf" } 
    ( cwd, '/home/' . (getlogin || getpwuid($<)) ) unless $config;

my $regexp = qr{^([a-z]+)=(.*)$};
my %values = map  { $_ =~ $regexp; $1 => $2 } 
             grep { $_ =~ $regexp } @ARGV;
pod2usage("Unknown config value $_")
    for grep { !$App::Padadoy::configs } keys %values;
my $padadoy = App::Padadoy->new($config,%values);
$padadoy->{quiet} = 1 if $quiet;

pod2usage("Unknown command '$cmd'!")
    unless grep { $_ eq $cmd } @App::Padadoy::commands;

$padadoy->$cmd( grep { $_ !~ $regexp } @ARGV);


__END__
=pod

=head1 NAME

padadoy - Simply deploy PSGI applications

=head1 VERSION

version 0.122

=head1 SYNOPSIS

 padadoy [options] <command> [config=value]

   Commands:
     init            initialize environment on deployment machine
     start           start the application
     stop            stop the application
     restart         start or gracefully restart the application if running
     config          show configuration values
     status          show some status information
     create          create a boilerplate application
     deplist         list applications package dependencies
     cartontest      update dependencies (with carton) and run tests

   Options:
     -c|--config F   specify some configuration file F. By default first 
                     ./padadoy.conf and then ~/padadoy.conf is used.
     -q|--quiet      suppress status messages
     -h|--help       show this help message and exit
     -v|--version    show version number of padadoy and exit

   Configuration:
     Configuration values are read from padadoy.conf. In addition you can
     set config values as key=value pairs on the command line. See
     command `config` for a list of config values.

=head1 NAME

padadoy - Simply deploy PSGI applications

=head1 AUTHOR

Jakob Voß

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Jakob Voß.

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

