Subversion Repositories camp_sysinfo_client_3

Rev

Blame | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

our $VERSION = '1.0';

use strict;
use warnings;

use FindBin;
use File::Spec;
use lib File::Spec->catdir($FindBin::Bin);

my $sourceDir = File::Spec->catdir($FindBin::Bin);
chdir $sourceDir;

my $verbose = 0;

# prompt the user for a response, then allow them to enter it
# the first response is considered the default and is printed
# in all caps if more than one exists
# first answer is used if they simply press the Enter
# key. The response is returned all lower case if more than one
# exists.
# it is assumed 
sub getAnswer {
   my ( $prompt, @answers ) = @_;
   $answers[0] = '' unless defined( $answers[0] );
   my $default = $answers[0];
   my $onlyOneAnswer = scalar( @answers ) == 1;
   print $prompt . '[ ';
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
   print join( ' | ', @answers ) . ' ]: ';
   my $thisAnswer = <>;
   chomp $thisAnswer;
   $thisAnswer = $default unless $thisAnswer;
   return $thisAnswer;
}


sub yesno {
   my ( $prompt, $default ) = @_;
   $default = 'yes' unless $default;
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
}


# checks various flags to 
sub determineOS {
   my $operatingSystems = shift;
   my $os = shift;
   my $osType;
   # first, look through our entries for fileexists
   unless ( $os ) {
      foreach $osType ( keys %$operatingSystems ) {
         next unless defined $operatingSystems->{$osType}->{'fileexists'};
         if (  -e $operatingSystems->{$osType}->{'fileexists'} ) {
            return $osType;
         } # if
      } # foreach
   } # unless
   # next, look for uname and see if that gives us a clue
   unless ( $os ) {
      my $osString = `uname -a`;
      foreach $osType ( keys %$operatingSystems ) {
         next unless defined $operatingSystems->{$osType}->{'regex'};
         print "Checking if OS is $osType in $osString\n" if $verbose > 2;
         if ( $osString =~ m/$$operatingSystems{$osType}{'regex'}/i ) {
            print "Yes, it is $osType\n" if $verbose > 2;
            return $osType;
         }
      } # foreach
   } # unless
   return '';
}


# hash to set up os specific rules
my %operatingSystems = (
                  'debian' => {
                     'regex'  => '(debian|mx|devuan)',
                     'install' => 'apt-get -y install libyaml-tiny-perl',
                  },
                  'ipfire' => {
                     'regex'  => 'ipfire',
                     'install' => 'mkdir YAML ; cp Tiny.pm.gz YAML; gunzip YAML/Tiny.pm.gz',
                  },
                  'freebsd' => {
                     'regex' => 'freebsd',
                     'install' => 'pkg install p5-YAML-Tiny',
                  },
                  'opnsense' => {
                     'fileexists' => '/conf/config.xml',
                     'install' => 'gunzip Tiny.pm.gz ; mkdir YAML ; mv Tiny.pm YAML',
                  },
                  
                );


my $os = determineOS( \%operatingSystems );

die "Could not determine operating system, manual installation required\n" unless $os;

eval( 'use YAML::Tiny' );
if ( $@ ) { # we need to install YAML::Tiny
   if ( &yesno( "You need YAML::Tiny but it is not installed\nI can install it with the command\n$operatingSystems{$os}{install}\nOk" ) ) {
      print "Please wait while I do the installation\nIt will take a minute\n";
      `$operatingSystems{$os}{'install'}`;
   }
}

print "perl install-sysinfo-client.pl os=$os\n";
#exec "perl install-sysinfo-client.pl os=$os";

Generated by GNU Enscript 1.6.5.90.