#!/usr/bin/perl -w

#$Header: /home2/cvsroot/LogTrend/StorageServer/UpdateSource/UpdateSource,v 1.2 2001/12/10 16:58:34 lsimonneau Exp $
##******************************************************************************
## AddSource
##  Description  : to add a new source into LogTrend database
##  Project      : LogTrend 1.0.0.0 - Atrid Systemes
##  Author       : Sylvain Lhullier s.lhullier@atrid.fr (02.03.2001)
##******************************************************************************
#$Log: UpdateSource,v $
#Revision 1.2  2001/12/10 16:58:34  lsimonneau
#Use GnuPG::Interface instead of Crypt::RSA for perl 5.005 compatibility.
#
#Revision 1.1  2001/11/16 10:25:18  lsimonneau
#Major feature enhancement : Implementation of authentification with RSA signature.
#
#IMPORTANT : This StorageServer is incompatible with older version.
#
#Revision 1.4  2001/06/26 09:05:36  fdubuy
#Added display_version + some more help
#
#Revision 1.3  2001/06/07 14:25:41  slhullier
#
#Passage de  unshift @INC,'..';  aux packages Logtrend::....
#
#Revision 1.2  2001/03/07 15:15:30  slhullier
#
#LogDie mis en place sur le serveur et l'agent Linux
#
#Revision 1.1  2001/03/02 13:42:44  slhullier
#
#AddSource fonctionne.
#

use strict;
use LogTrend::StorageServer::UpdateSource::PostgreSQLSourceUpdater;

my $name = "UpdateSource";
my $version = "1.0.0.0";
my $programminformation = "$name version $version";


##******************************************************************************
## Function display_version 
##******************************************************************************
sub display_version
{
	print "\n";
	print "$programminformation\n";
	print "\n";
}


##******************************************************************************
## Function display_help_and_exit
##******************************************************************************
sub display_help_and_exit
{
   print "\n";
   print "Synopsis : Change the public key of a source.\n\n";
   print "Usage : $name -d <database> [-H <host>] [-P <port>] \\\n";
   print "                      -u <username> -p <dbpassword> \\\n";
   print "                      -k <public_key_file> -s <source id>\n";
   print "        $name --version\n";
   print "\n";
   print "Options :\n";
   print "   -d : database name.\n";
   print "   -H : host name of the database server (optional).\n";
   print "   -P : port number of the database server (optional).\n";
   print "   -u : database user.\n";
   print "   -p : database user's password.\n";
   print "   -k : the filename of the public key.\n";
   print "   -s : the number of the source to update \n";
   print "\n";
   exit( 0 );
}

##******************************************************************************
## Function main
##******************************************************************************
sub main
{
   my $db_name;
   my $db_host = "";
   my $db_port = "";
   my $db_username;
   my $db_password;
   my $src_pub_key_file;
   my $src_id;

   if( @ARGV == 0 )
   { display_help_and_exit(); }
   elsif( @ARGV == 1 )
   {
      if( $ARGV[0] eq "--version")
      { display_version(); }
      elsif( $ARGV[0] eq "--help" )
      { display_help_and_exit(); }
      else
      { display_help_and_exit(); }
   }
   else
   {
      for( my $i = 0; $i<(@ARGV-1); $i++ )
      {
         if( $ARGV[$i] eq "-d" )
         { $db_name = $ARGV[$i + 1]; }
         elsif ($ARGV[$i] eq "-H")
         { $db_host = $ARGV[$i + 1]; }
         elsif ($ARGV[$i] eq "-P")
         { $db_port = $ARGV[$i + 1]; }
         elsif ($ARGV[$i] eq "-u")
         { $db_username = $ARGV[$i + 1]; }
         elsif ($ARGV[$i] eq "-p")
         { $db_password = $ARGV[$i + 1]; }
         elsif ($ARGV[$i] eq "-k")
         { $src_pub_key_file = $ARGV[$i + 1]; }
         elsif ($ARGV[$i] eq "-s")
         { $src_id = $ARGV[$i + 1]; }
      }

      if( !defined($db_name)     || !defined($db_username) ||
          !defined($db_password) || !defined($src_pub_key_file) ||
	  !defined($src_id))
      { display_help_and_exit(); }

      my $updater = new LogTrend::StorageServer::UpdateSource::PostgreSQLSourceUpdater( $db_name, $db_host, $db_port, $db_username, $db_password );
      $updater->Update_Source( $src_pub_key_file, $src_id );
   }

}

##******************************************************************************
main();
