package LIMS::Local::Debug;

use strict;
use FileHandle;
use Exporter;
use Data::Dumper;

use vars qw( $fh @EXPORT );

@EXPORT = qw( DEBUG debug_path );

sub import { goto &Exporter::import }

sub DEBUG {
    # don't want t/*.t output unless specifically requested:
    return if ($ENV{HARNESS_ACTIVE} && ! $ENV{DEBUG_ON});

    my $path_to_app_root = find_home(); # warn Dumper $path_to_app_root;

    unless ($fh) {
        $fh = new FileHandle '>' . "$path_to_app_root/logs/debug.txt" or die $!;
    }

    warn Dumper @_; # send to stdout

    print $fh join '', map {
		ref $_ ? Dumper $_ : $_ . "\n";
	} grep $_, @_;

    $fh->flush; # for mod_perl
}

# can't use LIMS::Utils version as LT use's this package
sub find_home { # warn Dumper \@INC;
    my $path_to_app;

    foreach (@INC) {
        if (-e "$_/script/lims_server.pl") { # warn 'PATH:', $_;
            $path_to_app = $_ and last;
        }
    }

    return $path_to_app;
}

1;