package DMart::DieHandler;
#===================================================================#

#======================================#
# Version Info                         #
#===================================================================#

$DMart::DieHandler::VERSION = '1.00';

#======================================#
# Dependencies                         #
#===================================================================#

#--------------------------------------#
# Standard Dependencies

use strict;
use warnings;

use CGI::Carp;
use HTML::Template;
use Data::Dumper;

my $dab = '';
my $tmpl_path = '';
sub new
{
  $dab = DMart::AppBase->get_dab;
  
  my $pkg = shift;
  my $self = {};
  bless( $self, $pkg );
  
  my $mref = sub{ $self->my_die_handler( @_ ) };
  $SIG{__DIE__} = $mref;
  
  return( $self );
}

sub my_die_handler
{
  my $self = shift;
  my $die_mess = scalar shift;
  
  #print STDERR "/--------------------------------------------\\\n";
  #print STDERR "    conf: \n\n";
  #print STDERR Dumper( $dab->{obs}{conf} );
  
  print STDERR "\n/--------------------------------------------\\\n";
  print STDERR "    die_mess1: \n";
  carp $die_mess;
  print STDERR "\n\\--------------------------------------------/";
  
  my $mref2 = sub{ $self->do_nothing_handler( @_ ) };
  $SIG{__DIE__} = $mref2;
  
  my $lt = localtime;
  
  # my $tmpl_main = $dab->{obs}{conf}->val( 'tmpl', 'host_html_tmpl_root' ) . "/" . $dab->{obs}{conf}->val( 'tmpl', 'pp_main_tmpl' );
  my $ob_tmpl = HTML::Template->new( filename => $dab->{obs}{conf}->val( 'tmpl', 'pp_main_tmpl' ),
                                     path     => [ $dab->{obs}{conf}->val( 'tmpl', 'host_html_tmpl_root' ),
                                                   $dab->{obs}{conf}->val( 'tmpl', 'sys_html_tmpl_root' )
                                                 ]
                                   );
  my $tmpl_err = $dab->{obs}{conf}->val( 'tmpl', 'host_html_tmpl_root' ) . "/" . $dab->{obs}{conf}->val( 'tmpl', 'pp_err_tmpl' );
  my $el_tmpl = HTML::Template->new( filename => $dab->{obs}{conf}->val( 'tmpl', 'pp_err_tmpl' ),
                                     path     => [ $dab->{obs}{conf}->val( 'tmpl', 'host_html_tmpl_root' ),
                                                   $dab->{obs}{conf}->val( 'tmpl', 'sys_html_tmpl_root' )
                                                 ]
                                   );
  
  $el_tmpl->param( error_msg => $die_mess,
                   err_input_display => 'Not available.',
                   help_msg => 'The previous request generated a fatal error.  The error has been logged and a system administrator has been notified.  We apologize for any inconvenience.'
                 );
                   
  $ob_tmpl->param( body_tmpl => $el_tmpl->output() );
  
  #print _STDERR Dumper( @_ );
  my $body = $ob_tmpl->output;

  print <<EOT;
HTTP/1.1 200 OK    
Date: $lt
Server: Apache/1.3 (Unix) mod_perl/1.26
Connection: close
Content-Type: text/html

$body  
EOT

  exit;
}

sub do_nothing_handler
{
  my $self = shift;
  my $die_mess = scalar shift;
  print STDERR "\n/--------------------------------------------\\\n";
  print STDERR "    die_mess2: \n";
  carp $die_mess;
  print STDERR "\n\\--------------------------------------------/";
  
  exit;
}

#===================================================================#
1;
