#!/usr/bin/env perl
use warnings;
use strict;
use Carp;
use Getopt::Long;
use Devel::NYTProf::Reader;

our $VERSION = 0.01;

use constant NUMERIC_PRECISION    => 5;

my %opt;
GetOptions(\%opt, qw/file|f=s delete|d out|o=s help|h/);

if (defined($opt{help})) {
  &usage;
  exit;
}

# handle file selection option
if (! defined($opt{file})) {
  $opt{file} = 'nytprof.out';
}
if (! -r $opt{file}) {
  die "Unable to access $opt{file}\n";
}

# handle handle output location
if (! defined($opt{out})) {
  $opt{out} = './profiler';
} 
if (! -r $opt{file}) {
  die "Unable to access output directory $opt{file}\n";
}

# handle deleting old db's
if (defined($opt{'delete'})) {
  _delete();
}

print "Generating CSV report...\n";
my $reporter = new Devel::NYTProf::Reader($opt{file});

# place to store this
$reporter->outputDir($opt{out});

# set output options
$reporter->addRegexp('^\s*', ''); # trim leading spaces

# generate the files
$reporter->report();

# Delete the previous database if it exists
sub _delete {
	if (-d $opt{out}) {
		print "Deleting $opt{out}\n";
		unlink glob($opt{out}."/*");
		unlink glob($opt{out}."/.*");
		rmdir $opt{out} or confess "Delete of $opt{out} failed: $!\n";
	}
}

sub usage {
	print <<END
usage: [perl] nytprofcsv [opts]
 --file <file>, -f <file>  Use the specified file as Devel::NYTProf database
                            file. [default: ./nytprof.out]
 --out <dir>,   -o <dir>   Place generated files here [default: ./profiler]
 --delete,      -d         Delete the old fprofhtml output [uses --out]
 --help,        -h         Print this message

This script of part of the Devel::NYTProf package by Adam J Kaplan.
Copyright 2008 Adam J Kaplan, http://search.cpan.org/~akaplan, Released under
the same terms as Perl itself.
END
}

__END__

=head1 NAME

nytprofcsv - Devel::NYTProf::Reader CSV format implementation

=head1 SYNOPSIS

 $ nytprofcsv [-h] [-d] [-o <output directory>] [-f <input file>]

 C<perl -d:NYTProf some_perl_app.pl>
 C<nytprofcsv>
 Generating CSV Output...

=head1 DESCRIPTION

C<nytprofcsv> is a script that implements L<Devel::NYTProf::Reader> to
create comma-seperated value formatted reports from L<Devel::NYTProf>
databases.

See the L<Devel::NYTProf> Perl code profiler for more information.

=head1 COMMAND-LINE OPTIONS

These are the command line options understood by C<nytprofcsv>

=over 4

=item -f, --file <filename>

Specifies the location of the input file.  The input file must be the
output of L<fprofpp>. Default: nytprof.out

=item -o, --out <dir>

Where to place the generated report. Default: ./profiler/

=item -d, --delete

Purge any existing database located at whatever -o (above) is set to

=item -h, --help

Print the help message

=back

=head1 SAMPLE OUTPUT

# Profile data generated by Devel::NYTProf::Reader v.0.01
# Author: Adam Kaplan. More information at http://search.cpan.org/~akaplan
# Format: time,calls,time/call,code
0,0,0,#--------------------------------------------------------------------
0,0,0,# My New Source File!
0,0,0,#--------------------------------------------------------------------
0,0,0,# $Id: MNSF.pm,v 1.25 2008/01/29 19:46:31 lfg Exp $
0,0,0,#--------------------------------------------------------------------
0,0,0,
0,0,0,package NYT::Feeds::Util;
0.00047,3,0.000156666666666667,use Date::Calc qw(Add_Delta_DHMS);
0.00360,3,0.0012,use HTML::Entities;
0.00212,3,0.000706666666666667,use Encode;
0.00248,3,0.000826666666666667,use utf8;
0.00468,3,0.00156,use strict; 
0,0,0,
0.00000,1,0,require Exporter; 
... thats enough, get the picture? ...

Note: the output includes the tainted warning indicating that the source
file was touched since the profiler was last run.  Because of this, the 
source file might not line up with the values (the profiler should be run 
again for fresh results).

Note: The format line indicates what fields the numbers correspond with

=head1 SEE ALSO

L<Devel::NYTProf>
L<Devel::NYTProf::Reader>
L<nytprofhtml> is an HTML implementation of L<Devel::NYTProf::Reader>

=head1 AUTHOR

Adam Kaplan, akaplan at nytimes dotcom

=head1 COPYRIGHT AND LICENSE

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut
