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
9 # Copyright 2024 Ian Kelling
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
15 # http://www.apache.org/licenses/LICENSE-2.0
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.
25 if [[ -f /etc
/fedora-release
]]; then
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,,}
38 distro-name-compat
() {
52 printf "%s\n" "$(distro-name)$(debian-archive)"
57 os_id
=$
(awk -F = '$1 == "ID" { print $2 }' /etc
/os-release
)
58 ver_id
=$
(awk -F = '$1 == "VERSION_ID" { print $2 }' /etc
/os-release |
sed 's/"//g')
59 # of VERSION_ID.x, the x matters in ubuntu but not trisquel
60 if [[ $os_id == ubuntu
]]; then
61 printf "%s\n" "$ver_id"
63 printf "%s\n" "$ver_id" |
sed 's/\..*//'
69 local archive expression pri name highpri shortest
70 local policy
="${1:-$(apt-cache policy)}" ||
return $?
74 # c = component (licensing component)
75 # l = label (Debian{,-Security,-Updates})
78 # goto b for archive lines we are interested in, a for lines we arent
79 # print priority + archive name. priority is in
80 # the previous line from the archive line.
81 # case insensitive, because $d is lower and we are matching with first char upper
82 read -rd '' expression
<<EOF ||:
83 /o=$d/I!b a;/l=$d/I!b a;/c=main/!b a b b;
84 : a;s/^ *([-0-9]+).*/\1/;h;b;
85 : b;s/^.*a=([^,]+).*/ \1/;H;x;s/\n//;p
87 while read -r pri name
; do
88 # in ubuntu, we get archives like flidas, flidas-updates, all the same pri,
89 # so just pick the shortest one.
90 if [[ ! $highpri ]]; then
95 if [[ $pri != "$highpri" ]]; then
98 if (( ${#shortest} > ${#name} )); then
101 done < <(echo "$policy" |
sed -rn "$expression" |
sort -rn ||
[[ $?
== 141 ]])
105 # formatted for use in pfile() in package-manager-abstractions
108 local archive expression pri name highpri shortest policy
109 # In theory we might want a policy subset, we could alter this to pass
111 policy
="(apt-cache policy)"
115 # c = component (licensing component)
116 # l = label (Debian{,-Security,-Updates})
117 read -rd '' expression
<<EOF ||:
118 /^ *([-0-9]+).*/{s/^ *([-0-9]+).*/\1/;h}
119 /^.*o=([^,]+).*/{s/^.*o=([^,]+).*/ \1/;H;x;s/\n//;p}
122 while read -r pri name
; do
123 if (( pri
> 0 )); then
124 if [[ ! $origins ]]; then
130 done < <(echo "$policy" |
sed -rn "$expression" |
sort -rn ||
[[ $?
== 141 ]])
135 [[ $
(debian-archive
) == testing
]]
137 # I only do testing or stable.
139 [[ $
(debian-archive
) == stable
]]
145 policy
="$(apt-cache policy)"
146 archive
=$
(debian-archive
"$policy")
147 printf "%s\n" "$policy" |
sed -rn "s/^.*a=$archive,n=([a-z]+).*/\1/p;T;q" ||
[[ $?
== 141 ]]
149 debian-codename-compat
() {
175 [[ $
(distro-name
) == fedora
]]
178 [[ $
(distro-name
) == debian
]]
181 [[ $
(distro-name
) == arch
]]
184 [[ $
(distro-name
) == ubuntu
]]
187 [[ $
(distro-name
) == trisquel
]]
189 # is debian/apt based
190 isdeb
() { command -v apt-get
&>/dev
/null
; }