2 # InspIRCd -- Internet Relay Chat Daemon
4 # Copyright (C) 2013-2017 Peter Powell <petpow@saberuk.com>
6 # This file is part of InspIRCd. InspIRCd is free software: you can
7 # redistribute it and/or modify it under the terms of the GNU General Public
8 # License as published by the Free Software Foundation, version 2.
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 use warnings FATAL => qw(all);
30 use Exporter qw(import);
31 use File::Path qw(mkpath);
32 use File::Spec::Functions qw(rel2abs);
36 our @EXPORT = qw(create_directory
43 sub create_directory($$) {
44 my ($location, $permissions) = @_;
46 mkpath($location, 0, $permissions);
53 return %version if %version;
55 # Attempt to retrieve version information from src/version.sh
56 chomp(my $vf = `sh src/version.sh 2>/dev/null`);
57 if ($vf =~ /^InspIRCd-([0-9]+)\.([0-9]+)\.([0-9]+)(?:-(\w+))?$/) {
58 %version = ( MAJOR => $1, MINOR => $2, PATCH => $3, LABEL => $4 );
61 # Attempt to retrieve missing version information from Git
62 chomp(my $gr = `git describe --tags 2>/dev/null`);
63 if ($gr =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-\d+-g(\w+))?$/) {
64 $version{MAJOR} //= $1;
65 $version{MINOR} //= $2;
66 $version{PATCH} //= $3;
67 $version{LABEL} = $4 if defined $4;
70 # If the user has specified a distribution label then we use it in
71 # place of the label from src/version.sh or Git.
72 $version{LABEL} = shift // $version{LABEL};
74 # If any of these fields are missing then the user has deleted the
75 # version file and is not running from Git. Fill in the fields with
76 # dummy data so we don't get into trouble with undef values later.
77 $version{MAJOR} //= '0';
78 $version{MINOR} //= '0';
79 $version{PATCH} //= '0';
81 # If there is no label then the user is using a stable release which
82 # does not have a label attached.
83 if (defined $version{LABEL}) {
84 $version{FULL} = "$version{MAJOR}.$version{MINOR}.$version{PATCH}-$version{LABEL}"
86 $version{LABEL} = 'release';
87 $version{FULL} = "$version{MAJOR}.$version{MINOR}.$version{PATCH}"
93 sub module_installed($) {
102 $count = `sysctl -n hw.ncpu 2>/dev/null` || 1;
103 } elsif ($^O eq 'darwin') {
104 $count = `sysctl -n hw.activecpu 2>/dev/null` || 1;
105 } elsif ($^O eq 'linux') {
106 $count = `getconf _NPROCESSORS_ONLN 2>/dev/null` || 1;
107 } elsif ($^O eq 'solaris') {
108 $count = `psrinfo -p 2>/dev/null` || 1;
114 sub read_config_file($) {
117 open(my $fh, $path) or return %config;
118 while (my $line = <$fh>) {
119 next if $line =~ /^\s*($|\#)/;
120 my ($key, $value) = ($line =~ /^(\S+)(?:\s(.*))?$/);
121 $config{$key} = $value;
127 sub write_config_file($%) {
130 open(my $fh, '>', $path) or print_error "unable to write to $path: $!";
131 while (my ($key, $value) = each %config) {
133 say $fh "$key $value";