Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
101 rodolico 1
#! /usr/bin/env perl
2
 
3
our $VERSION = '1.0';
4
 
5
use strict;
6
use warnings;
7
 
8
use FindBin;
9
use File::Spec;
10
use lib File::Spec->catdir($FindBin::Bin);
11
 
12
my $sourceDir = File::Spec->catdir($FindBin::Bin);
13
chdir $sourceDir;
14
 
15
my $verbose = 0;
16
 
17
# prompt the user for a response, then allow them to enter it
18
# the first response is considered the default and is printed
19
# in all caps if more than one exists
20
# first answer is used if they simply press the Enter
21
# key. The response is returned all lower case if more than one
22
# exists.
23
# it is assumed 
24
sub getAnswer {
25
   my ( $prompt, @answers ) = @_;
26
   $answers[0] = '' unless defined( $answers[0] );
27
   my $default = $answers[0];
28
   my $onlyOneAnswer = scalar( @answers ) == 1;
29
   print $prompt . '[ ';
30
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
31
   print join( ' | ', @answers ) . ' ]: ';
32
   my $thisAnswer = <>;
33
   chomp $thisAnswer;
34
   $thisAnswer = $default unless $thisAnswer;
35
   return $thisAnswer;
36
}
37
 
38
 
39
sub yesno {
40
   my ( $prompt, $default ) = @_;
41
   $default = 'yes' unless $default;
42
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
43
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
44
}
45
 
46
 
47
# checks various flags to 
48
sub determineOS {
49
   my $operatingSystems = shift;
50
   my $os = shift;
51
   my $osType;
52
   # first, look through our entries for fileexists
53
   unless ( $os ) {
54
      foreach $osType ( keys %$operatingSystems ) {
55
         next unless defined $operatingSystems->{$osType}->{'fileexists'};
56
         if (  -e $operatingSystems->{$osType}->{'fileexists'} ) {
57
            return $osType;
58
         } # if
59
      } # foreach
60
   } # unless
61
   # next, look for uname and see if that gives us a clue
62
   unless ( $os ) {
63
      my $osString = `uname -a`;
64
      foreach $osType ( keys %$operatingSystems ) {
65
         next unless defined $operatingSystems->{$osType}->{'regex'};
66
         print "Checking if OS is $osType in $osString\n" if $verbose > 2;
67
         if ( $osString =~ m/$$operatingSystems{$osType}{'regex'}/i ) {
68
            print "Yes, it is $osType\n" if $verbose > 2;
69
            return $osType;
70
         }
71
      } # foreach
72
   } # unless
73
   return '';
74
}
75
 
76
 
77
# hash to set up os specific rules
78
my %operatingSystems = (
79
                  'debian' => {
80
                     'regex'  => '(debian|mx|devuan)',
81
                     'install' => 'apt-get -y install libyaml-tiny-perl',
82
                  },
83
                  'ipfire' => {
84
                     'regex'  => 'ipfire',
85
                     'install' => 'mkdir YAML ; cp Tiny.pm.gz YAML; gunzip YAML/Tiny.pm.gz',
86
                  },
87
                  'freebsd' => {
88
                     'regex' => 'freebsd',
102 rodolico 89
                     'install' => 'echo y | pkg install p5-YAML-Tiny',
101 rodolico 90
                  },
91
                  'opnsense' => {
92
                     'fileexists' => '/conf/config.xml',
93
                     'install' => 'gunzip Tiny.pm.gz ; mkdir YAML ; mv Tiny.pm YAML',
94
                  },
95
 
96
                );
97
 
98
 
99
my $os = determineOS( \%operatingSystems );
100
 
101
die "Could not determine operating system, manual installation required\n" unless $os;
102
 
103
eval( 'use YAML::Tiny' );
104
if ( $@ ) { # we need to install YAML::Tiny
105
   if ( &yesno( "You need YAML::Tiny but it is not installed\nI can install it with the command\n$operatingSystems{$os}{install}\nOk" ) ) {
106
      print "Please wait while I do the installation\nIt will take a minute\n";
107
      `$operatingSystems{$os}{'install'}`;
108
   }
109
}
110
 
111
print "perl install-sysinfo-client.pl os=$os\n";
112
#exec "perl install-sysinfo-client.pl os=$os";