1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Helper library for detecting distro info and mapping to package manager.
5 # This should NOT be installed via `make install'.
6 # This is used by install/deps.perl and ci/profiles.perl
7 package PublicInbox
::InstallOS
;
9 use parent
qw(Exporter);
10 our ($ID, $PRETTY_NAME, $VERSION_ID); # same vars as os-release(5)
11 our @EXPORT = qw($ID $VERSION_ID pkg_fmt);
13 my ($release, $version); # from uname
14 if ($^O eq 'linux') { # try using os-release(5)
15 for my $f (qw(/etc/os-release /usr/lib/os-release)) {
18 qq{echo
"\$"$_" = qq[\$$_];"; }
19 } qw(ID PRETTY_NAME VERSION_ID);
20 # rely on sh(1) to handle interpolation and such:
21 my $vars = `sh -c '. $f; @echo'`;
22 die "sh \$?=$?" if $?
;
27 if ($ID eq 'debian' && $VERSION_ID eq '') {
28 if ($PRETTY_NAME =~ m!/sid\z!) {
31 open my $fh, '<', $f or die "open($f): $!";
32 my $msg = do { local $/; <$fh> };
34 ID=$ID, but no VERSION_ID
40 last if $ID ne '' && $VERSION_ID ne '';
42 $ID = 'linux' if $ID eq ''; # cf. os-release(5)
43 } elsif ($^O
=~ m!\A(?:free|net|open)bsd\z! || $^O
eq 'dragonfly') {
46 (undef, undef, $release, $version) = POSIX
::uname
();
47 $VERSION_ID = lc $release;
48 $VERSION_ID =~ s/[^0-9a-z\.\_\-]//sg; # cf. os-release(5)
49 } else { # only support POSIX-like and Free systems:
50 die "$^O unsupported";
52 $VERSION_ID //= 0; # numeric? could be 'sid', actually...
53 my %MIN_VER = ( # likely older versions work for many of these...
61 if (defined(my $min_ver = $MIN_VER{$^O
})) {
62 my $vid = $VERSION_ID;
63 $vid =~ s/-.*\z//s; # no dashes in v-strings
64 my $vstr = eval "v$vid";
65 die "can't convert VERSION_ID=$VERSION_ID to v-string" if $@
;
66 die <<EOM if $vstr lt $min_ver;
67 ID=$ID VERSION_ID=$VERSION_ID release=$release ($version) too old to support
72 if ($ID eq 'alpine') { 'apk' }
73 elsif ($ID =~ /\A(?:freebsd|dragonfly)\z/) { 'pkg' }
74 # *shrug*, as long as the (Net|Open)BSD names don't conflict w/ FreeBSD
75 elsif ($ID eq 'netbsd') { 'pkgin' }
76 elsif ($ID eq 'openbsd') { 'pkg_add' }
77 elsif ($ID =~ m!\A(?:debian|ubuntu)\z!) { 'deb' }
78 elsif ($ID =~ m!\A(?:centos|redhat|fedora)\z!) { 'rpm' }
79 else { warn "PKG_FMT undefined for ID=$ID"; undef }
83 PublicInbox
::InstallOS
->import;