#!/usr/bin/env perl

#
# find diff log files through difftool, and grep them
#

use warnings;
use strict;

use DBI;
use constant DB_SOCKET => '/var/run/mysqld/mysqld.sock'; # Socket for mysql
use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );

use IPC::Cmd 0.36 qw( can_run run );
use PS::IPP::Metadata::Config;
use PS::IPP::Metadata::List qw( parse_md_list );
use PS::IPP::Config 1.01 qw( :standard );

my $missing_tools;
my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
my $neblocate = can_run('neb-locate') or (warn "Can't find neb-locate" and $missing_tools = 1);

if ($missing_tools) {
    warn("Can't find required tools.");
    exit($PS_EXIT_CONFIG_ERROR);
}



my ($db_host, $dbname, $db_user, $db_pw, $dateobs_begin, $dateobs_end, $label, $filter, $data_group, $dist_group, $ra, $decl, $radius, $limit, $pstamp_order, $magicked, $not_destreaked, $destreaked, $grep, $diffandsky, $uselogname, $ippMonitor); # Database details
$db_host = 'ippdb01';
$db_pw = 'ipp';
$db_user = 'ipp';
GetOptions(
      ##     'dbhost=s' => \$db_host, # Database host name
          'dbname=s' => \$dbname, # Database name
      ##     'dbuser=s' => \$db_user, # Database user
     ##      'dbpass=s' => \$db_pw, # Database p/w
           'dateobs_begin=s' => \$dateobs_begin, # dateobs_begin
           'dateobs_end=s' => \$dateobs_end, # dateobs_end
           'label=s' => \$label, # label
           'filter=s' => \$filter,  # filter
           'data_group=s' => \$data_group, # data_group
           'dist_group=s' => \$dist_group, # dist_group
           'grep=s' => \$grep, #thing to grep for
           'ra=s' => \$ra, # ra
           'decl=s' => \$decl, # ra
           'radius=s' => \$radius, # ra
           'limit'   => \$limit, 
           'pstamp_order'   => \$pstamp_order, 
           'magicked'   => \$magicked,
           'not_destreaked'   => \$not_destreaked,
           'destreaked'   => \$destreaked,
           'diffandsky'  => \$diffandsky,
    'ippMonitor'  => \$ippMonitor,
    # No magicked inputs?
           ) or die "Unable to parse arguments.\n";
die "Unknown option: @ARGV\n" if @ARGV;
die "difflog_grep.pl uses difftool and grep to look for 'something' in many diffs logfiles.\n\nRequired options: --dbhost --dbname --dbuser --dbpass\n\nOptional options: --dateobs_begin, --dateobs_end, --label, --filter, --data_group, --dist_group, --ra, --decl, --radius, --limit, --pstamp_order, --magicked, --not_destreaked, --destreaked will select diff_ids from difftool to investigate\n\n--grep - if grep is not used will just list the log files \n--diffandsky will print the diff_id and sky_id instead of the logfilename\n--ippMonitor will add ippMonitor like output (bg, nsources, etc)\n"

    unless defined $db_host
    and defined $dbname
    and defined $db_user
    and defined $db_pw;



my $mdcParser = PS::IPP::Metadata::Config->new;


 my $fpaCommand1 = "$difftool -listrun";
    $fpaCommand1 .= " -dateobs_begin $dateobs_begin" if defined $dateobs_begin;
    $fpaCommand1 .= " -dateobs_end $dateobs_end" if defined $dateobs_end;
    $fpaCommand1 .= " -label $label" if defined $label;
    $fpaCommand1 .= " -filter $filter" if defined $filter;
    $fpaCommand1 .= " -data_group $data_group" if defined $data_group;
    $fpaCommand1 .= " -dist_group $dist_group" if defined $dist_group;
    $fpaCommand1 .= " -ra $ra" if defined $ra;
    $fpaCommand1 .= " -decl $decl" if defined $decl;
    $fpaCommand1 .= " -radius $radius" if defined $radius;
    $fpaCommand1 .= " -limit " if defined $limit;
    $fpaCommand1 .= " -pstamp_order " if defined $pstamp_order;
    $fpaCommand1 .= " -magicked " if defined $magicked;
    $fpaCommand1 .= " -not_destreaked " if defined $not_destreaked;
    $fpaCommand1 .= " -destreaked " if defined $destreaked;
    $fpaCommand1 .= " -dbname $dbname" if defined $dbname;


my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $fpaCommand1);

 unless ($success) {
       $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
       warn("Unable to list minidvodb database: $error_code\n");
       exit($error_code);
    }

if (defined(@$stdout_buf)){

    if (defined($diffandsky)) {
	print "diff_id skycell ";
    } else {print "logfile ";}

    if (defined($ippMonitor)) {
	print "bg bg_stdev dtime_diff dtime_match dtime_phot dtime_script stamps_num stamps_mean stamps_rms sources";
    }


	print "\n";
    my  $metadata2 = $mdcParser->parse(join "", @$stdout_buf) or
        die "Unable to parse metadata config";
    
    my   $components2 = parse_md_list($metadata2) or
        die "Unable to parse metadata list";
    my   $comp2 = $$components2[0];

    foreach my $comp2 (@$components2) {
    my $diff_id = $comp2->{diff_id};
   # print "$diff_id\n";

    #}
    
    #my $diff_id = 60382;

    if (!defined($diff_id)) {
        die "Unable to parse diff_id";

            }


# Database connection
my $db = DBI->connect( "DBI:mysql:database=$dbname;host=$db_host;mysql_socket=" . DB_SOCKET(),
                       $db_user,
                       $db_pw,
                       { RaiseError => 1, AutoCommit => 1 }
                       ) or die "Unable to connect to database: $DBI::errstr";

# Query to run
my $sql = "SELECT path_base, diffSkyfile.diff_id, skycell_id, diffSkyfile.bg,diffSkyfile.bg_stdev,diffSkyfile.dtime_diff,diffSkyfile.dtime_match,diffSkyfile.dtime_phot,diffSkyfile.dtime_script,diffSkyfile.stamps_num,diffSkyfile.stamps_mean,diffSkyfile.stamps_rms,diffSkyfile.sources from diffRun, diffSkyfile WHERE diffRun.diff_id = diffSkyfile.diff_id AND diffSkyfile.diff_id = $diff_id";

#print "$sql\n";

my $results = $db->selectall_arrayref( $sql ) or die "Unable to execute SQL: $DBI::errstr";
$db->disconnect;

foreach my $row ( @$results ) {
    my $path_base = $$row[0];        # Chip path
    my $diff_id1 = $$row[1];    # Class identifier
    my $skycell_id = $$row[2];         # Camera path

    my $logfile = $path_base.".log";

    if (defined($diffandsky)) {

print "$diff_id1 $skycell_id ";
    } else {
print "$logfile ";
    }
    

    if (defined($ippMonitor)) {

   if (defined($$row[3])) {printf "%2.4f ",$$row[3];} else {print "???     ";}
   if (defined($$row[4])) {printf "%2.4f ",$$row[4];} else {print "???     ";}
if (defined($$row[5])) {printf "%2.4f ",$$row[5];} else {print "???     ";}
if (defined($$row[6])) {printf "%2.4f ",$$row[6];} else {print "???     ";}
if (defined($$row[7])) {printf "%2.4f ",$$row[7];} else {print "???     ";}
if (defined($$row[8])) {printf "%d ",$$row[8];} else {print "???     ";}
if (defined($$row[9])) {printf "%d ",$$row[9];} else {print "???     ";}
if (defined($$row[10])) {printf "%2.4f ",$$row[10];} else {print "???     ";}
if (defined($$row[11])) {printf "%2.4f ",$$row[11];} else {print "???     ";}
if (defined($$row[12])) {printf "%d ",$$row[12];} else {print "???     ";}





# $$row[4] $$row[5] $$row[6] $$row[7] $$row[8] $$row[9] $$row[10] $$row[11] $$row[12] ";
	
    }
    
    #print "$logfile "; 
    if (defined($grep)) {
	my $comm = "$neblocate --path $logfile --server http://nebulous.mhpcc.ipp.ifa.hawaii.edu:80/nebulous";
	    
    my ( $nebsuccess, $neberror_code, $nebfull_buf, $nebstdout_buf, $nebstderr_buf ) =
        run(command => $comm);

	if (defined($nebstdout_buf)) {
	    	my $grepcomm = "grep '$grep' $$nebstdout_buf[0]";
	#	print "$grepcomm\n";
		my ( $grepsuccess, $greperror_code, $grepfull_buf, $grepstdout_buf, $grepstderr_buf ) =
		    run(command => $grepcomm);

		foreach my $grepout (@$grepstdout_buf) {
		    print $grepout;

		}

	}
	    

	}


    }

}


}

#}

# Pau.

