#!/usr/bin/env perl
#: gvmake
#: Command-line driver for Makefile::GraphViz

use strict;
use warnings;

use Getopt::Std;
use Makefile::GraphViz;

my %opts;
getopts('hf:o:s:a', \%opts) or
    die "Use -h to see the usage\n";

if ($opts{h}) {
    print <<'_EOC_';
Usage:
    gvmake [options] [target]*

Options:
    -a              Plot all the goals.
    -f <filename>   Use filename as Makefile.
    -h              Print this help.
    -s <w>x<h>      Specify the size (both for width and height)
    -o <filename>   Use filename as output PNG file.

_EOC_
    exit(0);
}

my $size = $opts{s};
my ($width, $height);
($width, $height) = split 'x', $opts{s} if $opts{s};

my $parser = Makefile::GraphViz->new;

my $makefile = $opts{f} || 'Makefile';
warn "parsing $makefile...\n";
$parser->parse($makefile) or die $parser->error;

my $outfile;
if ($opts{a}) {
    my $gv = $parser->plot_all;
    $outfile = "$makefile.png";
    $gv->as_png($outfile);
} else {
    my $tar = shift @ARGV || $parser->target;
    warn "plotting target $tar...\n";
    my $gv = $parser->plot(
        $tar,
    init_args => {
        width => $width, height => $height,
        },
    );
    $outfile = $opts{o} || "$tar.png";
    $gv->as_png($outfile);
}
warn "$outfile generated.\n";

__END__

=head1 NAME

gvmake - A make tool that generates pretty graphs from Makefile

=head1 SYNOPSIS

    # print usage info to stdout:
    gvmake -h

    # if the default target is 'all', the following
    # command will generate all.png
    gvmake

    # this command will generate 'test.png' where
    # 'test' is a target defined in the Makefile:
    gvmake test

    # override the default output file name:
    gvmake -o make.png test

    # specify the Makefile name explicitly:
    gvmake -f t/Makefile.old install

    # generate Makefile.png which contains all the goals
    gvmake -a

    # specify the size of the output image:
    gvmake -s 5x8    # width is 5 inch, and height is 8 inch

=head1 DESCRIPTION

This is a make tool that generates pretty graphs for the building
process according to user's Makefile instead of actually building
something. It is a simple command-line frontend for the
L<Makefile::GraphViz> module.

Currently only PNG format and the default settings for the graph
style are used. This inflexible design will be changed soon.

=head1 TODO

=over

=item *

Add more command-line options to control the graph appearance

=item *

To support more output file format

=item *

Add support for multiple goals passed in via command-line.

=back

=head1 BUGS

Please report bugs or send wish-list to
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Makefile-GraphViz>.

=head1 SEE ALSO

L<Makefile::GraphViz>, L<Makefile::Parser>.

=head1 AUTHOR

Agent Zhang, E<lt>agentzh@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2005, 2006, 2007 by Agent Zhang. All rights reserved.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

