#!/bin/sh
#
# Simple OS detection script
#
# Currently supported:
# * Red Hat 5,6 / CentOS 5,6
# * Fedora
# * OpenSuSE 11
#
# Authors: Christian Bayle <bayle@debian.org>
#          Alain Peyrat <aljeux@free.fr>
#
# Copyright 2014, Franck Villaume - TrivialDev
#
# FusionForge is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# FusionForge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with FusionForge; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

detect_os(){
	distrib=""
	if [ -f "/etc/redhat-release" ]
	then
		distrib=`awk '{print $1}' /etc/redhat-release`
		case $distrib in
			CentOS)
				os=centos
				;;
			Red)
				os=rhel
				;;
			Fedora)
				os=fedora
				;;
			openSUSE)
				os=opensuse
				;;
			*)
				os=redhat_unknown
				;;
		esac
	elif [ -f "/etc/SuSE-release" ]
	then
        	distrib=`awk '{print $1}' /etc/SuSE-release | head -n 1`
		if grep -q openSUSE /etc/SuSE-release
		then
			os="opensuse"
		else
        		os="suse"
		fi
	elif [ -f "/etc/ubuntu_version" ]
	then
        	distrib=`awk '{print $1}' /etc/ubuntu_version`
        	os="ubuntu"
	elif [ -f "/etc/debian_version" ]
	then
        	distrib=`awk '{print $1}' /etc/debian_version`
        	os="debian"
	else
		distrib="unknown"
		os="unknown"
	fi
	echo $os
}

detect_type()
{
	os=$(detect_os)
	case $os in
		suse|opensuse)
			type=suse
			;;
		debian|ark)
			type=debian	
			;;
		ubuntu)
			type=ubuntu
			;;
		centos|rhel|redhat_unknown|fedora)
			type=redhat
			;;
		*)
			type=unknown
			;;
	esac
	echo $type
}

detect_version()
{
	version=''
	os=$(detect_os)
	case $os in
		centos|rhel|redhat_unknown|fedora)
			release=$(rpm -q --queryformat='%{release}' rpm)
			version=${release##*.}
			;;
	esac
	echo $version
}
