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