a3bc4028f26c6559e4d4901cddc5aef2a17da82d
[distro-functions] / src / identify-distros
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 distro-name() {
17 local x
18 if [[ -f /etc/fedora-release ]]; then
19 echo fedora
20 elif [[ -e /etc/os-release ]]; then
21 sed -rn 's/^ID=(.*)/\1/p' /etc/os-release
22 elif type -p lsb_release &>/dev/null; then
23 # well, this is more standard, but it's 2 ms vs 200 ms
24 x=$(lsb_release -si); echo ${x,,}
25 else
26 echo "unknown distro"
27 return 1
28 fi
29 }
30
31 distro-name-compat() {
32 local x=$(distro-name)
33 case $x in
34 trisquel)
35 echo ubuntu
36 ;;
37 *)
38 echo $x
39 ;;
40 esac
41 }
42
43 distro-name-ver() {
44 echo $(distro-name)$(debian-archive)
45 }
46
47 distro-num() {
48 # Subshell keeps environment clean.
49 ( . /etc/os-release
50 # in ubuntu the .x matters, trisquel it doesnt
51 if [[ $ID == ubuntu ]]; then
52 echo $VERSION_ID
53 else
54 echo ${VERSION_ID%%.*}
55 fi
56 )
57 }
58
59 debian-archive() {
60 isdeb || return 0
61 local archive expression pri name highpri shortest
62 local policy="${1:-$(apt-cache policy)}" || return $?
63 # a = archive
64 # n = codename
65 # o = origin
66 # c = component (licensing component)
67 # l = label (Debian{,-Security,-Updates})
68 local d=$(distro-name)
69 # goto b for archive lines we are interested in, a for lines we arent
70 # print priority + archive name. priority is in
71 # the previous line from the archive line.
72 # case insensitive, because $d is lower and we are matching with first char upper
73 read -rd '' expression <<EOF ||:
74 /o=$d/I!b a;/l=$d/I!b a;/c=main/!b a b b;
75 : a;s/^ *([-0-9]+).*/\1/;h;b;
76 : b;s/^.*a=([^,]+).*/ \1/;H;x;s/\n//;p
77 EOF
78 while read -r pri name; do
79 # in ubuntu, we get archives like flidas, flidas-updates, all the same pri,
80 # so just pick the shortest one.
81 if [[ ! $highpri ]]; then
82 highpri=$pri;
83 shortest=$name
84 continue
85 fi
86 if [[ $pri != $highpri ]]; then
87 break
88 fi
89 if (( ${#shortest} > ${#name} )); then
90 shortest=$name
91 fi
92 done < <(echo "$policy" | sed -rn "$expression" | sort -rn || [[ $? == 141 ]])
93 echo "$shortest"
94 }
95
96 # formatted for use in pfile() in package-manager-abstractions
97 positive-origins() {
98 isdeb || return 0
99 local archive expression pri name highpri shortest
100 local policy="${1:-$(apt-cache policy)}" || return $?
101 # a = archive
102 # n = codename
103 # o = origin
104 # c = component (licensing component)
105 # l = label (Debian{,-Security,-Updates})
106 read -rd '' expression <<EOF ||:
107 /^ *([-0-9]+).*/{s/^ *([-0-9]+).*/\1/;h}
108 /^.*o=([^,]+).*/{s/^.*o=([^,]+).*/ \1/;H;x;s/\n//;p}
109 EOF
110 origins=
111 while read -r pri name; do
112 if (( pri > 0 )); then
113 if [[ ! $origins ]]; then
114 origins=$name
115 else
116 origins+=,$name
117 fi
118 fi
119 done < <(echo "$policy" | sed -rn "$expression" | sort -rn || [[ $? == 141 ]])
120 echo $origins
121 }
122
123 isdebian-testing() {
124 [[ $(debian-archive) == testing ]]
125 }
126 # I only do testing or stable.
127 isdebian-stable() {
128 [[ $(debian-archive) == stable ]]
129 }
130
131 debian-codename() {
132 isdeb || return 0
133 local policy="$(apt-cache policy)" || return $?
134 archive=$(debian-archive "$policy")
135 echo "$policy" | sed -rn "s/^.*a=$archive,n=([a-z]+).*/\1/p;T;q" || [[ $? == 141 ]]
136 }
137 debian-codename-compat() {
138 local n=$(debian-codename)
139 case $n in
140 flidas)
141 echo xenial
142 ;;
143 etiona)
144 echo bionic
145 ;;
146 nabia)
147 echo focal
148 ;;
149 aramo)
150 echo jammy
151 ;;
152 *)
153 echo $n
154 ;;
155 esac
156 }
157
158 isfedora() {
159 [[ $(distro-name) == fedora ]]
160 }
161 isdebian() {
162 [[ $(distro-name) == debian ]]
163 }
164 isarch() {
165 [[ $(distro-name) == arch ]]
166 }
167 isubuntu() {
168 [[ $(distro-name) == ubuntu ]]
169 }
170 istrisquel() {
171 [[ $(distro-name) == trisquel ]]
172 }
173 # is debian/apt based
174 isdeb() { command -v apt-get &>/dev/null; }