package Modules::ErrLog;

BEGIN {
	use Exporter ();
	@ISA = qw(Exporter);
	@EXPORT = qw(&error &addEvent &hard_error &soft_error);
}

use DBI;
use Carp;
use Carp qw/cluck/;

use strict "vars";

my $dbh;
my $package = 'Modules::ErrLog';
my $script = '';
my $debug = 0;
my $eventCount = 0;

sub DatabaseHandle {
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}
	if ( @_ ) {
		my $newDbh = shift;
		if ( ref($newDbh) =~ /DBI/ ) {
			$dbh = $newDbh;
		}
	}
	return $dbh;
}

sub Script {
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}
	if ( @_ ) {
		$script = shift;
	}
	return $script;
}

sub Debug {
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}
	if ( @_ ) {
		$debug = shift;
	}
	return $debug;
}

sub EventCount {
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}
	if ( @_ ) {
		$eventCount = shift;
	}
	return $eventCount;
}

sub error {
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}
	my ($message) = @_;
	print STDERR $message; 
	return 0;
}

# MySQLEvent(int code, string message, string anbieter, string kunde)
# writes event to db
# @return void
# writes new event into confixx' log table (db.ereignisse) from
# given values code, message, anbieter, kunde.
# anbieter and kunde are optional
sub addEvent{
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}

  my ($code, $message, $anbieter, $kunde) = @_;
  if ( $debug){
    my $subname = (caller(0))[3];
    print "SUB: $subname ($message)\n";
  }

	unless ( ref( $dbh ) ) {
		if ( ref($::dbh) ) {
			$dbh = $::dbh;
		}
	}

	unless ( $script ) {
		if ( $0 =~ /updatescript/i ) {
			$script = 'Update-Script';
		} elsif ( $0 =~ /ftptraffik/i) {
			$script = 'FTP-Traffic';
		} elsif ( $0 =~ /httpdtraffik/i) {
			$script = 'HTTP-Traffic';
		}
	}

  my $timestamp = time();
  if ( defined( $anbieter ) ) {
		unless( defined ( $kunde ) ) {
			$kunde = '';
		}
	} else {
    $anbieter = '';
    $kunde = '';
  }

	my $sql = "INSERT INTO ereignisse (timestamp, nachricht, code, script, anbieter, kunde) ".
		"VALUES (?, ?, ?, ?, ?, ?)";
	my $sth = $dbh->prepare( $sql );
	unless ( $sth &&
					 $sth->execute($timestamp, $message, $code, $script, $anbieter, $kunde) 
				 ) {
		my $msg = "error SQL-query: '$sql'\n$DBI::errstr";
		if ( $debug ) {
			confess( $msg );
		} else {
			croak( $msg );
		}
	}
	$eventCount++;
	return 1;
}

# hard_error(string message) severe errors routine
# @return die()
# called if error is severe.
# calls MySQLEvent() with code=1 and message, and then
# calls die() with message
sub hard_error{
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}

  if($debug){
    my $subname = (caller(0))[3];
    print "SUB: $subname\n";
  }

  my ( $msg, $exitCode, $exitMsg ) = @_;

  &addEvent( 1, $msg );

	if ( $exitCode ) {
		my $mess = $debug? longmess(): shortmess();
		print STDERR $mess,"\n";
		print STDERR $exitMsg? $exitMsg: $msg;

		exit $exitCode;

	} else {
		if ( $debug ){
			confess( $msg );
		} else {
			croak( $msg );
		}
	}
}


# soft_error(string message, string anbieter, string kunde) normal errors routine
# @return void
# calls MySQLEvent().
sub soft_error{
	my $this = shift;
	unless ( $this eq $package ) {
		unshift @_,$this;
		$this = undef;
	}

  if($debug){
    my $subname = (caller(0))[3];
    print "SUB: $subname\n";
  }

  my ($message, $anbieter, $kunde) = @_;

  if ( defined ( $anbieter ) ) {
		unless ( defined ( $kunde ) ) {
			$kunde = '';
		}
	} else {
    $anbieter = '';
    $kunde = '';
  }

  &addEvent(2, $message, $anbieter, $kunde);

}


return 1;

END {
	$dbh = undef;
}
