#!/bin/sh
#
# Report System Information
#
# Copyright (c) 2007 Avira GmbH
#

print_help()
{
	echo "\`getsysteminfo' retrieves system information."
	echo ""
	echo "Usage: ./getsysteminfo [OPTION]"
	echo ""
	echo "Options:"
	echo " -h, --help            display this help and exit"
	echo " -c, --classification  output binary classification"
	echo " -f, --fallback        output fallback information"
	echo ""
	echo "Report bugs to <support@avira.com>."
}

do_classification()
{
	CLASS="unknown"

	case "${OS}" in
		linux)
			CLASS=""

			if [ -f "/lib/libc.so.6" ]
			then
				grep "C Library.*version 2.0" /lib/libc.so.6 > /dev/null
				RET=$?

				if [ $RET -ne 0 ]
				then
					grep "C Library.*version 2.1" /lib/libc.so.6 > /dev/null
					RET=$?
				fi

				if [ $RET -eq 0 ]
				then
					CLASS="linux_glibc20"
				fi
			elif [ -f "/lib/libc.so.5" ]
			then
				CLASS="linux_libc5"
			fi

			if [ -z "$CLASS" ]
			then
				# fallback

				CLASS="linux_glibc22"

				uname -a | grep " s390 " > /dev/null
				
				RET=$?
				if [ $RET -ne 0 ]
				then
					uname -a | grep " s390x " > /dev/null
					RET=$?
				fi

				if [ $RET -eq 0 ]
				then
					CLASS="linux_glibc22_s390"
				fi

				uname -a | grep " sparc " > /dev/null
				
				RET=$?
				if [ $RET -ne 0 ]
				then
					uname -a | grep " sparc64 " > /dev/null
					RET=$?
				fi

				if [ $RET -eq 0 ]
				then
					CLASS="linux_glibc22_sparc"
				fi

				uname -a | grep " ppc " > /dev/null
				
				RET=$?
				if [ $RET -ne 0 ]
				then
					uname -a | grep " ppc64 " > /dev/null
					RET=$?
				fi

				if [ $RET -eq 0 ]
				then
					CLASS="linux_glibc22_ppc"
				fi

				if [ $FALLBACK_CLASSIFY -ne 1 ]
				then
					uname -a | grep " x86_64 " > /dev/null
				
					RET=$?
					if [ $RET -eq 0 ]
					then
						CLASS="linux_glibc22_x86_64"
					fi
				fi
			fi
			;;
		freebsd)
			uname -r | grep "^4\." > /dev/null
			RET=$?

			if [ $RET -eq 0 ]
			then
				CLASS="freebsd"
			else
				CLASS="freebsd5"
			fi
			;;
		sunos)
			CLASS=""

			uname -a | grep " i386 " > /dev/null
			RET=$?

			if [ $RET -eq 0 ]
			then
				CLASS="solaris_x86"
			fi

			if [ -z "$CLASS" ]
			then
				# fallback
				CLASS="solaris_sparc"
			fi
			;;
		openbsd)
			CLASS=""

			VERSION=`uname -r | sed -e s/\\\.// -e s/[^0-9].*//`

			if [ ! -z "$VERSION" ]
			then
				if [ $VERSION -lt 34 ]
				then
					CLASS="openbsd"
				fi
			fi

			if [ -z "$CLASS" ]
			then
				# fallback
				CLASS="openbsd_elf"
			fi
			;;
		netbsd)
			CLASS="netbsd"
			;;
		darwin)
			uname -a | grep " powerpc" > /dev/null
			if [ $? -eq 0 ]
			then
				CLASS="macosx_ppc"
			fi
			;;
	esac

	echo $CLASS
}

do_distribution()
{
	DISTRO=""
	VERSION=""

	case "${OS}" in
		linux)
			if [ -f /etc/redhat-release ]
			then
				DISTRO="redhat"

			elif [ -f /etc/mandrake-release -o -f /etc/mandrakelinux-release ]
			then
				DISTRO="mandrake"

			elif [ -f /etc/fedora-release ]
			then
				DISTRO="fedora"

			elif [ -f /etc/SuSE-release ]
			then
				DISTRO="suse"
				VERSION=`grep VERSION /etc/SuSE-release | sed -e s/.*=//`

			elif [ -f /etc/debian_version ]
			then
				DISTRO="debian"
				VERSION=`cat /etc/debian_version`

			elif [ -f /etc/slackware-version ]
			then
				DISTRO="slackware"

			elif [ -f /etc/gentoo-release ]
			then
				DISTRO="gentoo"

			elif [ -f /etc/turbolinux-release ]
			then
				DISTRO="turbolinux"

			fi
			;;
		*)
			VERSION=`uname -r`
			;;
	esac

	if [ -z "$DISTRO" ]
	then
		DISTRO="$OS"
	fi

	TEMP=$VERSION
	for item in $TEMP
	do
		VERSION=$item
		break
	done

	echo $DISTRO $VERSION
}

#main()

FALLBACK_CLASSIFY=0
CLASSIFY=0
ERROR=0

for arg in $@
do
	case $arg in
		-h|--help)
			print_help
			exit 0
			;;

		-c|--classification)
			CLASSIFY=1
			;;

		-f|--fallback)
			FALLBACK_CLASSIFY=1
			;;

		*)
			echo "unknown argument: $arg"
			ERROR=1
			;;
	esac
done

if [ $ERROR -eq 1 ]
then
	print_help
	exit 1
fi

OS=`uname | env LANG=C LC_ALL=C LC_CTYPE=C tr '[:upper:]' '[:lower:]'`

if [ $CLASSIFY -eq 1 ]
then
	do_classification
else
	do_distribution
fi

exit 0
