#!/bin/bash
#
# Shell script which detects the Linux distro it's running on
#
# Returned distro       Version the script was tested on
# ---------------------------------------------------------
# opensuse              openSuSE 11.0 (no lsb_release) and 11.2 (lsb_release)
# fedora                Fedora 12
# centos                CentOS 5.4
# kubuntu               Kubuntu 9.10
# debian                Debian 5.0.3
# arch                  Arch
# slackware             Slackware 13.0.0.0.0
# mandriva              Mandriva 2009.1
# debian		            Knoppix 6.2
# linuxmint		         Mint 8
#
# 10/02/17 framp at linux-tips-and-tricks dot de

detectedDistro="Unknown"
regExpLsbInfo="Description:[[:space:]]*([^ ]*)"
regExpLsbFile="/etc/(.*)[-_]"

if [ `which lsb_release 2>/dev/null` ]; then       # lsb_release available
   lsbInfo=`lsb_release -d`
   if [[ $lsbInfo =~ $regExpLsbInfo ]]; then
      detectedDistro=${BASH_REMATCH[1]}
   else
      echo "??? Should not occur: Don't find distro name in lsb_release output ???"
      exit 1
   fi

else                                               # lsb_release not available
   etcFiles=`ls /etc/*[-_]{release,version} 2>/dev/null`
   for file in $etcFiles; do
      if [[ $file =~ $regExpLsbFile ]]; then
         detectedDistro=${BASH_REMATCH[1]}
         break
      else
         echo "??? Should not occur: Don't find any etcFiles ???"
         exit 1
      fi
   done
fi

detectedDistro=`echo $detectedDistro | tr "[:upper:]" "[:lower:]"`

case $detectedDistro in
	suse) 	detectedDistro="opensuse" ;;
        linux)	detectedDistro="linuxmint" ;;
esac

echo "Detected distro: $detectedDistro"
