Merge branch '3-remove-unused-fields-from-member-records' into 'master'
[memberdb.git] / shell / os-detect.sh
1 #!/bin/bash
2  
3 # Try and get debian operating system
4 # id, codename, and release
5
6 TYPE=$(echo "$1" | tr '[A-Z]' '[a-z]')
7 OS=$(uname)
8 ID="unknown"
9 CODENAME="unknown"
10 RELEASE="unknown"
11
12 if [ "$OS" == "Linux" ]; then
13     # detect centos
14     grep "centos" /etc/issue -i -q
15     if [ $? = '0' ]; then
16         ID="centos"
17         RELEASE=$(cat /etc/redhat-release | grep -o 'release [0-9]' | cut -d " " -f2)
18     # could be debian or ubuntu
19     elif [ $(which lsb_release) ]; then
20         ID=$(lsb_release -i | cut -f2)
21         CODENAME=$(lsb_release -c | cut -f2)
22         RELEASE=$(lsb_release -r | cut -f2)
23     elif [ -f "/etc/lsb-release" ]; then
24         ID=$(cat /etc/lsb-release | grep DISTRIB_ID | cut -d "=" -f2)
25         CODENAME=$(cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d "=" -f2)
26         RELEASE=$(cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2)
27     elif [ -f "/etc/issue" ]; then
28         ID=$(head -1 /etc/issue | cut -d " " -f1)
29         if [ -f "/etc/debian_version" ]; then
30           RELEASE=$(</etc/debian_version)
31         else
32           RELEASE=$(head -1 /etc/issue | cut -d " " -f2)
33         fi
34     fi
35 fi
36
37 declare -A info
38
39 info[id]=$(echo "$ID" | tr '[A-Z]' '[a-z]')
40 info[codename]=$(echo "$CODENAME" | tr '[A-Z]' '[a-z]')
41 info[release]=$(echo "$RELEASE" | tr '[A-Z]' '[a-z]')
42
43 if [ "$TYPE" ] ; then 
44     echo "${info[$TYPE]}"
45 else 
46     echo -e "ID\t${info[id]}"
47     echo -e "CODENAME\t${info[codename]}"
48     echo -e "RELEASE\t${info[release]}"
49 fi