#!/usr/bin/perl -w 

#$Header: /home2/cvsroot/LogTrend/ComplexAlarm/BuildDescFileSquel,v 1.5 2001/08/27 14:37:08 lsimonneau Exp $
##*****************************************************************************
##  BuildDescFileSquel 
##  Description  : Build a complex alarm description file squeleton
##
##  Project      : LogTrend 1.0.0.0 - Atrid Systemes
##  Author       : Laurent Simonneau l.simonneau@atrid.fr
##*****************************************************************************
#$Log: BuildDescFileSquel,v $
#Revision 1.5  2001/08/27 14:37:08  lsimonneau
#GetSourcesList return values have changed.
#
#Revision 1.4  2001/06/11 16:04:21  lsimonneau
#Minor bugfixes
#
#Revision 1.3  2001/06/11 13:54:10  lsimonneau
#Suppression du fichier databasecreation.sql puisqu'il a t inclus dans LogTrend/StorageServer/databasecreation.sql
#
#Minor bugfixes.
#
#Revision 1.2  2001/06/07 14:41:04  lsimonneau
#Passage du unshift @INC, '..' au LogTrend::
#
#Revision 1.1  2001/05/30 09:36:57  lsimonneau
#Premire version du module d'alarmes complexes dans le CVS.
#Toutes les fonctionnalits ont t testes et correctement.
#


use strict;
use LogTrend::ComplexAlarm::DataBase::ComplexAlarmPostgreSQLDataBase;
use Getopt::Long;
use Locale::gettext;
use POSIX;     # Needed for setlocale()
use XML::Writer;

*_ = \&gettext; # _ is equivalent to gettext

sub display_help {

    print "Usage : BuildDescFileSquel filename\n";
    print "                           [--database database_name]\n";
    print "                           [--host host_name]\n";
    print "                           [--port server_port]\n";
    print "                           [--help][-h]\n\n";
}

sub main {
    my @level_list = ("Info", "Warning", "Error");
    
    my %opt;

    setlocale(LC_MESSAGES, "");
    bindtextdomain("LogTrend", "./intl");
    textdomain("LogTrend");

    GetOptions(\%opt, "database=s", "host=s","port=i","help|h");
    
    if(!(defined $ARGV[0]) or defined $opt{help}) {
	display_help();
	exit 0;
    }
    
    my $file = new IO::File(">$ARGV[0]");
    my $xmlwriter = new XML::Writer(OUTPUT => $file);
    $xmlwriter->xmlDecl();
    $xmlwriter->startTag("ComplexAlarm");

    $opt{database} = "logtrend" unless defined $opt{database};
    $opt{host} = "localhost" unless defined $opt{host};
    $opt{port} = "5432" unless defined $opt{port};

    
    print _("Login : ");
    my $user = <STDIN>;
    chomp($user);
    system "stty -echo";
    print _("Password : ");
    my $password = <STDIN>;
    chomp($password);
    system "stty echo";
    print "\n";
    
    my $database = LogTrend::ComplexAlarm::DataBase::ComplexAlarmPostgreSQLDataBase->new($opt{database}, 
											 $opt{host},
											 $opt{port},
											 $user,
											 $password);

    $xmlwriter->emptyTag("DataBase", 
			 "Name" => $opt{database},
			 "Host" => $opt{host},
			 "Port" => $opt{port},
			 "User" => $user,
			 "Password" => $password);


    my $sourcelist = $database->GetSourcesList();
    my $source;

    while(1) {
	foreach $source (@$sourcelist) {
	    print "\n$source->[0]";
	    for(my $i=1; defined $source->[$i]; $i+=2) {
		print "   --   $source->[$i] : $source->[$i+1]";
	    }
	}
	
	print "\n"._("Choose a source : ");
	$source = <STDIN>;
	chomp($source);
	print "\n\n";
	
	if($source >= $sourcelist->[0][0] and $source <= $sourcelist->[$#$sourcelist][0]) {
	    last;
	}
    }

    $xmlwriter->startTag("Source");
    $xmlwriter->characters("$source");
    $xmlwriter->endTag();

    my $agentlist = $database->GetListOfAgentsOnSource($source);
    my $agent;

    if($#$agentlist == -1) {
	die "No Agent Registered on source $source";
    }
    
    while(1) {
	foreach $agent (@$agentlist) {
	    print "$agent->[0]   --   $agent->[1]\n";
	}

	print "\n"._("Choose an agent : ");
	$agent = <STDIN>;
	chomp($agent);
	print "\n\n";
	
	if($agent >= $agentlist->[0][0] and $agent <= $agentlist->[$#$agentlist][0]) {
	    last;
	}
    }

    $xmlwriter->startTag("Agent");
    $xmlwriter->characters("$agent");
    $xmlwriter->endTag();

    
    # Demander le numero
    print _("Enter the complex alarm number. This number must be different for each complex alarms of an agent : ");
    my $number = <STDIN>;
    chomp($number);
    print "\n\n";

    $xmlwriter->startTag("Number");
    $xmlwriter->characters("$number");
    $xmlwriter->endTag();

    # Nom
    print _("Enter the alarm's name : ");
    my $name = <STDIN>;
    chomp($name);
    print "\n\n";

    $xmlwriter->startTag("Name");
    $xmlwriter->characters("$name");
    $xmlwriter->endTag();

    # Niveau
    my $index;
    for($index = 0; $index <= $#level_list; $index++) {
	 print "$index : $level_list[$index]\n";
    }

    print _("Choose the alarm's level : ");
    my $level = <STDIN>;
    chomp($level);
    print "\n\n";

    $xmlwriter->startTag("Level");
    $xmlwriter->emptyTag("$level_list[$level]");
    $xmlwriter->endTag();
    
    # Actions 
    action($xmlwriter);

    $xmlwriter->startTag("Equation");
    
    $xmlwriter->endTag();
    
    $xmlwriter->endTag();
    $xmlwriter->end();
    
    # lister les noms variables.
    print _("Available variables on agent")." $agent "._("on source")." $source : \n";
    
    foreach my $var_name (@{$database->GetListOfDataNameOnAgent($source, $agent)}) {
	print "$var_name\n";
    }

    $file->close;

    open(FILE, "$ARGV[0]");
    my $line = <FILE>;
    $line = <FILE>;
    close(FILE);

    $line =~ s/></>\n</g;
    
    open(FILE, ">$ARGV[0]");
    print FILE $line;
    close(FILE);
}


sub action {
    my $xmlwriter = shift;

    $xmlwriter->startTag("Action");
    
    while(1) {
	print "1 - "._("Send a Mail")."\n";
	print "2 - "._("Send a SMS")."\n";
	print "3 - "._("Add a message in system logs with Syslog")."\n";
	print "4 - "._("Display a message on screen with xmessage")."\n";
	print "0 - "._("No more action, continue configuration")."\n\n";

	print _("Choose an action : ");
	my $action = <STDIN>;
	chomp($action);
	print "\n\n";

	if($action == 1) {
	    print _("Enter the address : ");
	    my $address = <STDIN>;
	    chomp($address);

	    print "\n"._("Enter the subject : ");
	    my $subject = <STDIN>;
	    chomp($subject);

	    print "\n"._("Enter the message : ");
	    my $message = <STDIN>;
	    chomp($message);

	    $xmlwriter->startTag("Mail",
				 "Address" => $address,
				 "Subject" => $subject);
	    $xmlwriter->characters($message);
	    $xmlwriter->endTag();	    
	    
	    print "\n";	    
	}
	elsif($action == 2){
	    print _("Enter the phone number in national format : ");
	    my $nat_phone = <STDIN>;
	    chomp($nat_phone);

	    print "\n"._("Enter the phone number in international format : ");
	    my $inter_phone = <STDIN>;
	    chomp($inter_phone);

	    print "\n"._("Enter the message : ");
	    my $message = <STDIN>;
	    chomp($message);

	    $xmlwriter->startTag("SMS",
				 "NationalPhone" => $nat_phone,
				 "InternationalPhone" => $inter_phone);
	    $xmlwriter->characters($message);
	    $xmlwriter->endTag();	    
	    
	    print "\n";	    
	}
	elsif($action == 3) {	   
	    print "\n"._("Enter the message : ");
	    my $message = <STDIN>;
	    chomp($message);

	    $xmlwriter->startTag("Syslog");
	    $xmlwriter->characters($message);
	    $xmlwriter->endTag();	    
	    
	    print "\n";	    
	}
	elsif($action == 4) {	   
	    print _("Enter the display number : ");
	    my $display = <STDIN>;
	    chomp($display);

	    print "\n"._("Enter the message : ");
	    my $message = <STDIN>;
	    chomp($message);

	    $xmlwriter->startTag("xmessage", "Display" => $display);
	    $xmlwriter->characters($message);
	    $xmlwriter->endTag();	    
	    
	    print "\n";	    
	}
	elsif($action == 0) {
	    last;
	}
    }

    $xmlwriter->endTag();
}

main();
