#!/usr/bin/perl -w

#$Header: /home2/cvsroot/LogTrend/ComplexAlarm/GetListOfAgentsOnSource,v 1.2 2001/07/25 12:27:16 lsimonneau Exp $
##*****************************************************************************
##  GetListOfAgentsOnSource 
##  Description  : Implement a complex alarm
##
##  Project      : LogTrend 1.0.0.0 - Atrid Systemes
##  Author       : Laurent Simonneau l.simonneau@atrid.fr
##*****************************************************************************
#$Log: GetListOfAgentsOnSource,v $
#Revision 1.2  2001/07/25 12:27:16  lsimonneau
#Utilise LogTrend::DataBaseAccess au lieu de LogTrend::ComplexAlarm::DataBase
#
#Revision 1.1  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::DataBaseAccess::PostgreSQLDataBase;
use Getopt::Long;
use Locale::gettext;
use POSIX;     # Needed for setlocale()

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

sub display_help {

    print "Usage : GetListOfAgentsOnSource --source source_number\n";
    print "                               [--database database_name]\n";
    print "                               [--host host_name]\n";
    print "                               [--port server_port]\n";
    print "                               [--help][-h]\n\n";
}

sub main {
    my $user = "";
    my $password = "";

    my $database;
    my $agent;
    
    my %opt;

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

    GetOptions(\%opt, "source|s=i", "database=s",
	       "host=s","port=i","help|h");
    

    if(!(defined $opt{source}) or defined $opt{help}) {
	display_help();
	exit 0;
    }
    
    $opt{database} = "logtrend" unless defined $opt{database};
    $opt{host} = "localhost" unless defined $opt{host};
    $opt{port} = "5432" unless defined $opt{port};

    
    print _("Login : ");
    $user = <STDIN>;
    system "stty -echo";
    print _("Password : ");
    $password = <STDIN>;
    system "stty echo";
    print "\n";

    $database = LogTrend::DataBaseAccess::PostgreSQLDataBase->new($opt{database}, 
								  $opt{host},
								  $opt{port},
								  $user,
								  $password);
  

    my $agentlist = $database->GetListOfAgentsOnSource($opt{source});    

    print "\n"._("Available agents on source")." $opt{source} : \n\n";

    foreach $agent (@$agentlist) {
	print "$agent->[0]   --   $agent->[1]\n";
    }
}

main();
