Subversion Repositories camp_sysinfo_client_3

Rev

Rev 21 | Blame | Last modification | View Log | RSS feed

#!/usr/bin/env perl

use warnings;
use strict;

our $VERSION = '1.0.1';

# find our location and use it for searching for libraries
BEGIN {
   use FindBin;
   use File::Spec;
   use lib File::Spec->catdir($FindBin::Bin);
}

use sysinfoconf; # a library of shared routines with install.pl


sub findSendEmail {
   my $currentLocation = shift;
   my @possibles = ( '/opt/sendEmail/sendEmail', '/opt/sendEmail/sendEmail.pl' );
   return $currentLocation if ( $currentLocation && -x $currentLocation );
   # well, we didn't find it, so look around some more
   # we install it in /opt, so try there
   foreach my $current ( @possibles ) {
      return $current if -x $current;
   }
   if ( &yesno( "You are asking for sendEmail, but I don't see it on the system\nWould you like me to automatically download and install" ) ) {
      $path = `perl getSendEmail.pl`;
      chomp $path;
      return $path;
   }
   return '';
}

sub userConfirmation {
   my $temp;
   
   $clientName = &getAnswer( "Client Name ", ($clientName) );
   $serialNumber = &getAnswer( "Serial Number ", ($serialNumber) );
   $hostname = &getAnswer( "Host Name ", ($hostname) );

   $temp = join( ',',@moduleDirs );
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
   @moduleDirs = split( ',', $temp );

   $temp = join( ',',@scriptDirs );
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
   @scriptDirs = split( ',', $temp );

}

sub setUpTransport {
   my ($priority, $transport, $fields, $type ) = @_;
   $priority = getAnswer( 'Priority', $priority );
   $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
   $$transport{'-name-'} = $type unless $$transport{'-name-'};
   my $allKeys = $$fields{'keys'};
   foreach my $key ( @$allKeys ) {
      if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
         $temp = &findSendEmail( $$transport{$key} );
         $$transport{$key} = $temp if $temp;
      }
      $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
   }
   return ( $priority, $transport );
}

sub showAllTransports {
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      print '='x40 . "\n";
      &showTransports( $priority, $$transports{ $priority } );
      print '='x40 . "\n";
   }
}   

sub showTransports {
   my ( $priority,$thisOne )  = @_;
   print $$thisOne{'-name-'} . " has priority $priority\n";
   foreach my $key ( sort keys %$thisOne ) {
      next if $key =~ m/^-.*-$/;
      print "$key = $$thisOne{$key}\n";
   }
}

sub doTransports {
   my ( $transports ) = @_;
   
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      if ( &yesno( $$transports{$priority}{'-name-'} . " has a priority of $priority, edit it?") ) {
         #print Dumper( $sendTypes{$$transports{$priority}{'-name-'}} );
         #die;
         my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'-name-'}} );
         if ( $newpriority != $priority ) {
            delete $$transports{$priority};
            $priority = $newpriority;
         }
         $$transports{$priority} = $temp;
      } # if
   }

   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      print '='x40 . "\n";
      &showTransports( $priority, $$transports{ $priority } );
      print '='x40 . "\n";
   }

   while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
      $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
      my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
      $$transports{$priority} = $temp;
   }

   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      print '='x40 . "\n";
      &showTransports( $priority, $$transports{ $priority } );
      print '='x40 . "\n";
   }
}


sub loadConfig {
   # See if existing configuration. If so, offer to load it
   if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
      print "Loading defaults from existing config $sysinfo3\n";
      my $client_name;
      open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
      my $temp = join( '', <SEED> );
      close SEED;
      eval( $temp );
   }
}

&processParameters( @ARGV );

&loadConfig();

unless ( $hostname ) {
   $hostname = `hostname -f`;
   chomp $hostname;
}

&userConfirmation();
&doTransports( $transports );

print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
print &showConf(  );
print '-'x40 . "\n";
print &writeConfig( $confDir, $confName, &showConf() ) if &yesno( "Write this configuration" );


print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running /opt/camp/sysinfo/config.pl\n";

1;