]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - make/utilities.pm
Remove unused motd and rules field from ServerConfig
[user/henk/code/inspircd.git] / make / utilities.pm
index 9bafe372ecd0d58a4bd0413d7f51ac11c32a1e1e..48713fe9e9936bc7035ca718047c28296ebbe577 100644 (file)
 #
 
 
-package make::utilities;
+BEGIN {
+       require 5.8.0;
+}
 
-require 5.8.0;
+package make::utilities;
 
 use strict;
 use warnings FATAL => qw(all);
 
 use Exporter 'import';
-use POSIX;
-use Getopt::Long;
 use Fcntl;
-our @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
+use File::Path;
+use File::Spec::Functions qw(rel2abs);
+use Getopt::Long;
+use POSIX;
+
+our @EXPORT = qw(module_installed prompt_bool prompt_dir prompt_string get_cpu_count make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
 
 # Parse the output of a *_config program,
 # such as pcre_config, take out the -L
@@ -42,6 +47,62 @@ our @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pk
 my %already_added = ();
 my $if_skip_lines = 0;
 
+sub module_installed($)
+{
+       my $module = shift;
+       eval("use $module;");
+       return !$@;
+}
+
+sub prompt_bool($$$) {
+       my ($interactive, $question, $default) = @_;
+       my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n');
+       return $answer =~ /y/i;
+}
+
+sub prompt_dir($$$) {
+       my ($interactive, $question, $default) = @_;
+       my ($answer, $create) = (undef, 'y');
+       do {
+               $answer = rel2abs(prompt_string($interactive, $question, $default));
+               $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y');
+               my $mkpath = eval {
+                       mkpath($answer, 0, 0750);
+                       return 1;
+               };
+               unless (defined $mkpath) {
+                       print "Error: unable to create $answer!\n\n";
+                       $create = 0;
+               }
+       } while (!$create);
+       return $answer;
+}
+
+sub prompt_string($$$) {
+       my ($interactive, $question, $default) = @_;
+       return $default unless $interactive;
+       print $question, "\n";
+       print "[\e[1;32m$default\e[0m] => ";
+       chomp(my $answer = <STDIN>);
+       print "\n";
+       return $answer ? $answer : $default;
+}
+
+sub get_cpu_count {
+       my $count = 1;
+       if ($^O =~ /bsd/) {
+               $count = `sysctl -n hw.ncpu`;
+       } elsif ($^O eq 'darwin') {
+               $count = `sysctl -n hw.activecpu`;
+       } elsif ($^O eq 'linux') {
+               $count = `getconf _NPROCESSORS_ONLN`;
+       } elsif ($^O eq 'solaris') {
+               $count = `psrinfo -p`;
+       }
+       chomp($count);
+       return $count;
+}
+
 sub promptstring($$$$$)
 {
        my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;