lots of new stuff, good working version
[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 if [[ -f /etc/fedora-release ]]; then
18 echo fedora
19 elif [[ -e /etc/os-release ]]; then
20 sed -rn 's/^ID=(.*)/\1/p' /etc/os-release
21 else
22 echo "unknown distro"
23 return 1
24 fi
25 }
26
27 distro-name-ver() {
28 echo `distro-name``debian-archive`
29 }
30
31 debian-archive() {
32 isdeb || return 0
33 local archive
34 local policy="${1:-$(apt-cache policy)}"
35 # a = archive
36 # n = codename
37 # o = origin
38 # c = component (licensing component)
39 # l = label (Debian{,-Security,-Updates})
40 { for archive in stable testing unstable; do
41 # print priority + archive name. priority is in
42 # the previous line after finding the archive.
43 read -rd '' expression <<EOF ||:
44 /o=Debian,a=$archive,.*l=Debian,c=main/{x;s/^ *([-0-9]+).*/\1 $archive/p;q};h
45 EOF
46 echo "$policy" | sed -rn "$expression"
47 done
48 } | sort -rn | head -n1 | grep -oE "\w+$"
49 }
50
51
52 isdebian-testing() {
53 [[ $(debian-archive) == testing ]]
54 }
55 # I only do testing or stable.
56 isdebian-stable() {
57 [[ $(debian-archive) == stable ]]
58 }
59
60 debian-codename() {
61 isdeb || return 0
62 local policy="$(apt-cache policy)"
63 archive=$(debian-archive "$policy")
64 echo "$policy" | sed -rn "s/^.*a=$archive,n=([a-z]+).*/\1/p;T;q"
65 }
66 isfedora() {
67 [[ $(distro-name) == fedora ]]
68 }
69 isdebian() {
70 [[ $(distro-name) == debian ]]
71 }
72 isarch() {
73 [[ $(distro-name) == arch ]]
74 }
75 # is debian/apt based
76 isdeb() { command -v apt-get &>/dev/null; }
77 isubuntu() {
78 [[ $(distro-name) == ubuntu ]]
79 }