#!/usr/bin/perl -w
#
# WardenCommon.pm
#
# Copyright (C) 2011-2013 Cesnet z.s.p.o
#
# Use of this source is governed by a BSD-style license, see LICENSE file.

package WardenCommon;

use strict;
use File::Basename;
use Sys::Syslog qw(:DEFAULT setlogsock);
Sys::Syslog::setlogsock('unix');
use Carp;

our $VERSION = "2.2";

#-------------------------------------------------------------------------------
# succMsg - print message and exit seccessfully
#-------------------------------------------------------------------------------
sub succMsg
{
  my $msg = shift;
  $msg = trim($msg);
  print $msg . "\n";
  exit 0;
} # End of succMsg


#-------------------------------------------------------------------------------
# errMsg - print error message and exit unsuccessfully
#-------------------------------------------------------------------------------
sub errMsg
{
  my $msg = shift;
  $msg = trim($msg);
  print $msg . "\n";
  exit 1;
} # End of errMsg


#-------------------------------------------------------------------------------
# trim - remove whitespace from the start and end of the string
#-------------------------------------------------------------------------------
sub trim
{
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
} # End of trim


#-------------------------------------------------------------------------------
# sendMsg - sent message via syslog (SYS::Syslog) and to client (SOAP::Fault)
#-------------------------------------------------------------------------------
sub sendMsg
{
  my $syslog            = shift;
  my $syslog_verbose    = shift;
  my $syslog_facility   = shift;
  my $severity          = shift;
  my $syslog_msg        = shift;
  my $soap_msg          = shift;
  my $filename          = shift;

  if ($syslog_verbose == 1 && ($severity eq "err" || $severity eq "debug")) {
    $syslog_msg .= "\nStack info: " . Carp::longmess();
  }

  if ($syslog == 1 && defined $severity && defined $syslog_msg) {
    Sys::Syslog::openlog($filename, "cons,pid", $syslog_facility);
    Sys::Syslog::syslog("$severity", "$syslog_msg");
    Sys::Syslog::closelog();
  }

  if (defined $soap_msg) {
    die SOAP::Fault->faultstring($soap_msg);
  }
} # End of sendMsg


#-------------------------------------------------------------------------------
# loadConf - load configuration file
#-------------------------------------------------------------------------------
sub loadConf 
{
  my $conf_file	= shift;

  our $BASEDIR		= undef;
  our $SYSLOG		= undef;
  our $SYSLOG_VERBOSE	= undef;
  our $SYSLOG_FACILITY	= undef;
  our $DB_NAME          = undef;
  our $DB_USER          = undef;
  our $DB_PASS          = undef;
  our $DB_HOST          = undef;
  our $MAX_EVENTS_LIMIT	= 1000000;
  our %VALID_STRINGS 	= ();
  unless (do $conf_file) {
    die("Errors in config file '$conf_file': $@") if $@;
    die("Can't read config file '$conf_file': $!") unless defined $_;
    # if $_ defined, it's retvalue of last statement of conf, for which we don't care
  }
} # End of loadConf


#-------------------------------------------------------------------------------
# connectDB - connect to database and create DB handler
#-------------------------------------------------------------------------------
sub connectDB
{
  my $db_name	= shift;
  my $db_host	= shift;
  my $db_user	= shift;
  my $db_pass	= shift;

  my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",$db_user, $db_pass, {RaiseError => 1, mysql_auto_reconnect => 1}) || die "Could not connect to database '$db_name': $DBI::errstr";
  return $dbh;
} # End of connectDB

1;