#!/usr/bin/perl -w

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

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

sub display_help {

    print "Usage : GetSourcesList [--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 $source;
    
    my %opt;
    my $last_id = -1;

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

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

    if(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 $sourcelist = $database->GetSourcesList();

    print "\n"._("Available sources")." : \n";

    foreach $source (@$sourcelist) {
	print "\n$source->[0]";
	for(my $i=1; defined $source->[$i]; $i+=2) {
	    print "   --   $source->[$i] : $source->[$i+1]";
	}
    }

    print "\n";
}

main();
