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