#!perl
#
#   Alien::SWIG - Command-line Alien::SWIG config-listing script
#
#   Copyright (c) 2011 Jason McManus
#
#   Full POD documentation after __END__
#

use Getopt::Long;
use strict;
use warnings;

# Ours.
use Alien::SWIG;

###
### Variables
###

use vars qw( $VERSION $TRUE $FALSE $PROGNAME $PROGDESC );
BEGIN {
	$VERSION = '0.02_01';
}

*TRUE      = \1;
*FALSE     = \0;
$PROGNAME  = 'aswig-config';
$PROGDESC  = 'Dump config information about the Alien::SWIG install';

$|=1;

# Argument tree
my @args = qw( path version executable includes module_dir cmd_line help );
my %args = (
    help    =>
    {
        optspec     => 'help|?',
        desc        => 'Display this help message.',
        required    => $FALSE,
    },
    path =>
    {
        optspec     => 'path',
        desc        => 'Print the absolute base path of the SWIG installation.',
        required    => $FALSE,
    },
    version =>
    {
        optspec     => 'version|swig_version|swigversion',
        desc        => 'Print the version of the installed copy of SWIG.',
        required    => $FALSE,
    },
    executable =>
    {
        optspec     => 'executable|x',
        desc        => 'Print the absolute path to the SWIG executable.',
        required    => $FALSE,
    },
    includes =>
    {
        optspec     => 'includes|incs',
        desc        => 'Print a list of SWIG -I include directives.',
        required    => $FALSE,
    },
    module_dir =>
    {
        optspec     => 'moduledir|module_dir|moddir|mods',
        desc        => 'Print the absolute path of the SWIG module directory.',
        required    => $FALSE,
    },
    cmd_line =>
    {
        optspec     => 'cmdline|cmd_line|command',
        desc        => 'Print a full, working command line, with all -I paths included.',
        required    => $FALSE,
    },
);

###
### Main body
###

my $opts = get_opts();

my $swig = Alien::SWIG->new();

if( $opts->{path} )
{
    print $swig->path();
}
elsif( $opts->{version} )
{
    print $swig->version();
}
elsif( $opts->{executable} )
{
    print $swig->executable();
}
elsif( $opts->{includes} )
{
    print ' ' . $swig->includes() . ' ';
}
elsif( $opts->{moduledir} )
{
    print $swig->module_dir();
}
elsif( $opts->{cmdline} )
{
    print $swig->cmd_line();
}
else
{
    usage( 2 );
}

print "\n" if( -t STDIN );

exit( 0 );

###
### Subs
###

sub prog_header
{
    return $PROGNAME . ' v' . $VERSION . ' - ' . $PROGDESC . "\n";
}

# Parse and process command-line arguments
sub get_opts
{
    my( %opt_results, %opt_retval );

    # get the parsed options from Getopt::Long
    GetOptions( \%opt_results, map { $args{$_}->{optspec} } keys( %args ) )
        or usage( 3 );

    # Asked for help?
    usage( 0 ) if( $opt_results{help} );

    # Start with defaults
    for( keys %args )
    {
        $opt_retval{$_} = $args{$_}->{default}
            if( exists( $args{$_}->{default} ) );
    }

    # Set actual values
    $opt_retval{$_} = $opt_results{$_}
        for( keys( %opt_results ) );

    return( \%opt_retval );
}

# Get rid of Pod::Usage::pod2usage formatting dependency
sub usage
{
    my $exitval = defined( $_[0] ) ? $_[0] : 42;

    print prog_header(), "\n";
    print "Usage:\n\t\$ $PROGNAME [OPTION]\n\n";
    print "Options:\n";

    for( @args )
    {
        my @opts = split( /\|/, $args{$_}->{optspec} );
        my( $single ) = grep { length( $_ ) == 1 } @opts;
        $single = substr( $opts[0], 0, 1 )
            unless( $single );
        printf( "  --%s | -%s:\n    %s\n\n",
                $opts[0], $single, $args{$_}->{desc} );
    }

    exit( $exitval );
}

__END__

=pod

=head1 NAME

aswig-config - Dump configuration information about the Alien::SWIG install

=head1 USAGE

    $ aswig-config [OPTION]

=head1 DESCRIPTION

This tool, as part of the L<Alien::SWIG> distribution, is for convenient
printing of the installed SWIG locations and configuration, for use in
Makefiles, shell scripts, etc.

=head1 OPTIONS

=head2 --path | -p

Print the absolute base path of the SWIG installation.

=head2 --version | -v

Print the version of the installed copy of SWIG (not this program).

=head2 --executable | -x

Print the absolute path of the installed SWIG executable.

=head2 --includes | -i

Print a list of -I include directives needed to use SWIG.

=head2 --module_dir | -m

Print the absolute base path of the SWIG modules directory.

=head2 --cmdline | -c

Print a full, working command line, with all -I paths included.

=head2 --help | -h | -?

Show a brief help message.

=head1 SEE ALSO

L<http://www.swig.org/> - SWIG, the Simplified Wrapper and Interface Generator

L<http://swig.org/doc.html> - The SWIG Documentation

The F<bin/> directory of this module's distribution

=head1 AUTHORS

Jason McManus, C<< <infidel at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to
C<bug-alien-swig at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Alien-SWIG>.  The authors will be notified, and then you'll
automatically be notified of progress on your bug as changes are made.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Alien:SWIG:

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Alien-SWIG>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Alien-SWIG>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Alien-SWIG>

=item * Search CPAN

L<http://search.cpan.org/dist/Alien-SWIG/>

=back

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2010-2011 Jason McManus

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

# END
