shellcheck fixes, including real bug
[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 debian-archive() {
48 isdeb || return 0
49 local archive expression pri name highpri shortest
50 local policy="${1:-$(apt-cache policy)}" || return $?
51 # a = archive
52 # n = codename
53 # o = origin
54 # c = component (licensing component)
55 # l = label (Debian{,-Security,-Updates})
56 local d=$(distro-name)
57 # goto b for archive lines we are interested in, a for lines we arent
58 # print priority + archive name. priority is in
59 # the previous line from the archive line.
60 # case insensitive, because $d is lower and we are matching with first char upper
61 read -rd '' expression <<EOF ||:
62 /o=$d/I!b a;/l=$d/I!b a;/c=main/!b a b b;
63 : a;s/^ *([-0-9]+).*/\1/;h;b;
64 : b;s/^.*a=([^,]+).*/ \1/;H;x;s/\n//;p
65 EOF
66 while read -r pri name; do
67 # in ubuntu, we get archives like flidas, flidas-updates, all the same pri,
68 # so just pick the shortest one.
69 if [[ ! $highpri ]]; then
70 highpri=$pri;
71 shortest=$name
72 continue
73 fi
74 if [[ $pri != $highpri ]]; then
75 break
76 fi
77 if (( ${#shortest} > ${#name} )); then
78 shortest=$name
79 fi
80 done < <(echo "$policy" | sed -rn "$expression" | sort -rn)
81 echo "$shortest"
82
83 }
84
85
86 isdebian-testing() {
87 [[ $(debian-archive) == testing ]]
88 }
89 # I only do testing or stable.
90 isdebian-stable() {
91 [[ $(debian-archive) == stable ]]
92 }
93
94 debian-codename() {
95 isdeb || return 0
96 local policy="$(apt-cache policy)" || return $?
97 archive=$(debian-archive "$policy")
98 echo "$policy" | sed -rn "s/^.*a=$archive,n=([a-z]+).*/\1/p;T;q"
99 }
100 debian-codename-compat() {
101 local n=$(debian-codename)
102 case $n in
103 flidas)
104 echo xenial
105 ;;
106 *)
107 echo $n
108 ;;
109 esac
110 }
111
112 isfedora() {
113 [[ $(distro-name) == fedora ]]
114 }
115 isdebian() {
116 [[ $(distro-name) == debian ]]
117 }
118 isarch() {
119 [[ $(distro-name) == arch ]]
120 }
121 isubuntu() {
122 [[ $(distro-name) == ubuntu ]]
123 }
124 istrisquel() {
125 [[ $(distro-name) == trisquel ]]
126 }
127 # is debian/apt based
128 isdeb() { command -v apt-get &>/dev/null; }