#!/usr/local/bin/perl
# ########################################
# $Id: mod,v 1.2 2007/05/18 23:42:25 ask Exp $
# $Source: /opt/CVS/Modwheel/utils/mod,v $
# $Author: ask $
# $HeadURL$
# $Revision: 1.2 $
# $Date: 2007/05/18 23:42:25 $
#################################################

use strict;
use warnings;
use Modwheel::REPL;
use Modwheel::Install::Everything;
use Modwheel::Configure;
use Getopt::Euclid;
use Readonly;
use version; our $VERSION = qv('0.3.1');

Readonly my $DEFAULT_PROMPT => q{mod > };

undef @ARGV;

my $prefix     = $ARGV{-prefix};
my $configfile = $ARGV{-config};
my $site       = $ARGV{-site};
$prefix      ||= '/opt/modwheel';
$configfile  ||= 'config/modwheelconfig.yml';

my $REPL_config = {
    class  => mod->new(),
    prompt => $DEFAULT_PROMPT,
};

if (@ARGV) {
    my $r = Modwheel::REPL->new($REPL_config);
    $r->do(join(q{ }, @ARGV), @ARGV);
}
else {
    welcome();
    Modwheel::REPL->new($REPL_config)->run;
}

sub welcome {
    print q{ }, q{-+} x 32, qq{\n};
    print
        "Welcome to mod v$VERSION! The friendly Modwheel read-eval-print-loop.",
        qq{\n};
    print q{ }, q{-+} x 32, qq{\n\n};
    print
        "Type `help` for some basic commands or `quit` to exit the program.\n\n";
    return;
}

package mod;
use Readonly;

sub new {
    return bless {}, shift;
}

sub install {
    my ($self) = @_;
    return "Use modstrap (perldoc modstrap)";
}

sub child_help {
    my ($self)  = @_;
    my $spacing = 16;
    my $method_help = {'install'   => 'Install Modwheel into the prefix chosen at compilation time.',};
    while (my($sub, $description) = each %{$method_help}) {
        my $whitespace = q{ } x ($spacing - length $sub);
        print join $whitespace, ($sub, $description);
        print qq{\n};
    }
    return;
}

sub touch {
    my ($self, $file) = @_;
    open my $fh, ">$file" or return;
    close $fh;
}

__END__

=pod

=head1 NAME

mod - Modwheel utility command-line tool.

=head1 VERSION

This document refers to Modwheel version 0.3.1.

=head1 USAGE

    mod [option] [command] [cmd arg 1]..[cmd arg n]

=head1 REQUIRED ARGUMENTS

None.

=head1 OPTIONS

=over 4

=item -p[refix] <prefix>                                                                                                                       
                                                                                                                                               
Set the prefix for Modwheel.                                                                                                                   
Default: /opt/modwheel                                                                                                                         
                                                                                                                                               
=item -c[onfig] <configfile>                                                                                                                   
                                                                                                                                               
Use a different modwheel configfile than the default.                                                                                          
Default: $PREFIX/config/modwheelconfig.yml                                                                                                     
                                                                                                                                               
=item -s[ite] <site>                                                                                                                           
                                                                                                                                               
Use a different modwheel site than the default.                                                                                                
Default: defaultsite set in the config file.                                                                                                   
                                                                                                                                               
=item -logmode <logmode>                                                                                                                       
                                                                                                                                               
Use a logging mode other than stderr. (i.e off)                                                                                                
Default: stderr

=back

=head1 DESCRIPTION

To install modwheel into the current directory:

    mod install base

To configure modwheel:

    mod configure

To set up the database tables:

    mod install model

=head1 DIAGNOSTICS

Check that mod is using the right configuration file, and the right prefix.
Set them manually with -prefix and -configfile.

=head1 CONFIGURATION

mod can use the Modwheel configuration file and the Modwheel database.
See L<Modwheel::Manual::Config> and L<Modwheel::Manual> for more information.

=head1 EXIT STATUS

Returns zero on sucess, and any value above zero on failure.

=head1 DEPENDENCIES

=over 4

=item Modwheel

=item Getopt::Euclid

=item IO::Prompt

=item Params::Util

=item Term::ReadLine

=item Readonly

=item version

=back

=head1 INCOMPATIBILITIES                                                                                                                       
                                                                                                                                               
None known.                                                                                                                                    
                                                                                                                                               
=head1 BUGS AND LIMITATIONS                                                                                                                    
                                                                                                                                               
No bugs have been reported.                                                                                                                    
                                                                                                                                               
Please report any bugs or feature requests to                                                                                                  
C<bug-modwheel@rt.cpan.org>, or through the web interface at                                                                                   
                                                                                                                                               
=head1 ACKNOWLEDGEMENTS

Thanks to C<Matt Trout> for ideas.
(See his blog post C<Writing a perl read-eval-print loop (REPL)> here:
L<http://chainsawblues.vox.com/library/post/a-perl-read-excute-print-loop-repl.html>)

=head1 SEE ALSO                                                                                                                                
                                                                                                                                               
=over 4                                                                                                                                        
                                                                                                                                               
=item * L<Modwheel::Manual>                                                                                                                    
                                                                                                                                               
The Modwheel manual.                                                                                                                           
                                                                                                                                               
=item * L<http://www.0x61736b.net/Modwheel/>                                                                                                   
                                                                                                                                               
The Modwheel website.                                                                                                                          
                                                                                                                                               
=back                                                                                                                                          
                                                                                                                                               
=head1 VERSION                                                                                                                                 
                                                                                                                                               
v0.2.2                                                                                                                                         
                                                                                                                                               
=head1 AUTHOR                                                                                                                                  
                                                                                                                                               
Ask Solem, C<< ask@0x61736b.net >>.                                                                                                            
                                                                                                                                               
                                                                                                                                               
=head1 LICENSE AND COPYRIGHT                                                                                                                   
                                                                                                                                               
Copyright (c), 2007 Ask Solem C<< ask@0x61736b.net >>.                                                                                         
                                                                                                                                               
All rights reserved.                                                                                                                           
                                                                                                                                               
This library is free software; you can redistribute it and/or modify                                                                           
it under the same terms as Perl itself, either Perl version 5.8.6 or,                                                                          
at your option, any later version of Perl 5 you may have available.                                                                            
                                                                                                                                               
=head1 DISCLAIMER OF WARRANTY                                                                                                                  
                                                                                                                                               
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE                                                                 
SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE                                                                     
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE                                                                       
SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,                                                                    
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND                                                                   
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND                                                                        
PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE,                                                                  
YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.                                                                         
                                                                                                                                               
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY                                                                 
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE                                                                    
SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES,                                                                      
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING                                                                   
OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO                                                                  
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR                                                                   
THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER                                                                           
SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE                                                                          
POSSIBILITY OF SUCH DAMAGES.                                                                                                                   
                                                                                                                                               
=cut
