#!/usr/bin/perl
##########################################################
# This script is part of the Devel::NYTProf distribution
#
# Copyright, contact and other information can be found
# at the bottom of this file, or by going to:
# http://metacpan.org/release/Devel-NYTProf/
#
##########################################################

use warnings;
use strict;

use Carp;
use Getopt::Long;

use Devel::NYTProf::Reader;

our $VERSION = '6.15';

use constant NUMERIC_PRECISION => 5;

my %delimiters = (
    comma => ",",
    tab   => "\t",
);

my %opt = (
    file => 'nytprof.out',
    out  => 'nytprof',
    delim => 'comma',
);
GetOptions(\%opt, qw/file|f=s delete|d out|o=s help|h delim=s annotated|a/)
    or do {
        usage();
        exit 1;
    };

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

$opt{delim} = $delimiters{ $opt{delim} } if exists $delimiters{ $opt{delim} };

# handle file selection option
if (!-r $opt{file}) {
    die "$0: Unable to access $opt{file}\n";
}

# handle output location
if (!-e $opt{out}) {

    # will be created
}
elsif (!-d $opt{out}) {
    die "$0: Specified output directory `$opt{out}' is a file. whoops!\n";
}
elsif (!-w $opt{out}) {
    die "$0: Unable to write to output directory `$opt{out}'\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->output_dir($opt{out});

$reporter->set_param(mk_report_source_line => sub {
    my ($linenum, $line, $stats_for_line, $statistics, $profile, $filestr) = @_;
    $line =~ s/^\s*//; # trim leading spaces
    my $delim = $opt{delim};

	my $time  = $stats_for_line->{'time'} || 0;
	my $calls = $stats_for_line->{'calls'} || 0;
	$time  += $stats_for_line->{evalcall_stmts_time_nested} || 0;
	#$calls ||= 1 if exists $stats_for_line->{evalcall_stmts_time_nested};

    my $text = sprintf("%f%s%g%s%f%s%s\n",
        $time, $delim,
        $calls, $delim,
		($calls) ? $time/$calls : 0, $delim,
        $line,
    );
    return $text unless $opt{annotated};

    # srcline
    $text = "srcline$delim$text";

    return $text;
});

$reporter->set_param(mk_report_xsub_line => sub { "" });

# 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: ./nytprof]
 --delete,      -d         Delete the old fprofhtml output [uses --out]
 --help,        -h         Print this message

This script is part of the Devel::NYTProf package
See https://metacpan.org/pod/Devel::NYTProf
END
}

__END__

=head1 NAME

nytprofcsv - (DEPRECATED) L<Devel::NYTProf::Reader> CSV format implementation

=head1 SYNOPSIS

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

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

=head1 NOTE

B<nytprofcsv is deprecated and will be removed in a future release.>

=head1 DESCRIPTION

C<nytprofcsv> is a script that implements L<Devel::NYTProf::Reader> to
create comma-separated 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: ./nytprof/

=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
 # Format: time,calls,time/call,code
 0,0,0,#--------------------------------------------------------------------
 0,0,0,# My New Source File!
 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; 
 ... that's enough, get the picture? ...

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

Note2: If the source file is modified between profiling and report generation,
the source might be misaligned

=head1 SEE ALSO

Mailing list and discussion at L<http://groups.google.com/group/develnytprof-dev>

Public Github Repository and hacking instructions at L<https://github.com/timbunce/devel-nytprof/>

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
