lots of new stuff, good working version
[distro-functions] / src / identify-distros
index 0d7a8fe1a9a1eff9b93a178efbe4839871c40c41..f75cb0f36b3b936c3a9970a9c36300bf0d48ba43 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-distro_name() {
+distro-name() {
     if [[ -f /etc/fedora-release ]]; then
         echo fedora
+    elif [[ -e /etc/os-release ]]; then
+        sed -rn 's/^ID=(.*)/\1/p' /etc/os-release
     else
-        grep "^ID=.*" /etc/os-release | sed 's/^ID=//'
+        echo "unknown distro"
+        return 1
     fi
 }
+
+distro-name-ver() {
+    echo `distro-name``debian-archive`
+}
+
+debian-archive() {
+    isdeb || return 0
+    local archive
+    local policy="${1:-$(apt-cache policy)}"
+    # a = archive
+    # n = codename
+    # o = origin
+    # c = component (licensing component)
+    # l = label (Debian{,-Security,-Updates})
+    { for archive in stable testing unstable; do
+          # print priority + archive name. priority is in
+          # the previous line after finding the archive.
+          read -rd '' expression <<EOF ||:
+/o=Debian,a=$archive,.*l=Debian,c=main/{x;s/^ *([-0-9]+).*/\1 $archive/p;q};h
+EOF
+          echo "$policy" | sed -rn "$expression"
+      done
+    } | sort -rn | head -n1 | grep -oE "\w+$"
+}
+
+
+isdebian-testing() {
+    [[ $(debian-archive) == testing ]]
+}
+# I only do testing or stable.
+isdebian-stable() {
+    [[ $(debian-archive) == stable ]]
+}
+
+debian-codename() {
+    isdeb || return 0
+    local policy="$(apt-cache policy)"
+    archive=$(debian-archive "$policy")
+    echo "$policy" | sed -rn "s/^.*a=$archive,n=([a-z]+).*/\1/p;T;q"
+}
 isfedora() {
-    local d=$(distro_name)
-    [[ $d == fedora ]] || return 1
+    [[ $(distro-name) == fedora ]]
 }
 isdebian() {
-    local d=$(distro_name)
-    [[ $d == debian ]] || return 1
+    [[ $(distro-name) == debian ]]
 }
-isdeb() {
-    local d=$(distro_name)
-    [[ $d == debian || $d == ubuntu ]] || return 1
+isarch() {
+    [[ $(distro-name) == arch ]]
 }
+# is debian/apt based
+isdeb() { command -v apt-get &>/dev/null; }
 isubuntu() {
-    local d=$(distro_name)
-    [[ $d == ubuntu ]] || return 1
+    [[ $(distro-name) == ubuntu ]]
 }