#!/bin/sh
#
# Report System Information
#
# Copyright (c) 2012 Avira Operations GmbH & Co. KG and its Licensors
#

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=""

# 			check for glibc5
			if [ -f "/lib/libc.so.5" ]
			then
				CLASS="linux_libc5"
			fi

# 			check for glibc21
			if [ -z "$CLASS" ]
			then
				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
				fi
			fi

# 			check for glibc22
			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_glibc24_x86_64"
					fi
				fi
			fi
			;;
		freebsd)
			CLASS="freebsd"

			uname -r | grep "^4\." > /dev/null
			RET=$?
			if [ $RET -eq 0 ]
			then
				CLASS="freebsd"
			fi

			uname -r | grep "^6\." > /dev/null
			RET=$?
			if [ $RET -eq 0 ]
			then
				CLASS="freebsd_v62"
			fi
			;;
		sunos)
			CLASS=""

			uname -r | grep 5.8 > /dev/null
			RET=$?
			if [ $RET -eq 0 ]
			then
				CLASS="solaris_sparc_v8"

				if [ $FALLBACK_CLASSIFY -ne 1 ]
				then
					isainfo -v | grep 64-bit > /dev/null
					RET=$?
					if [ $RET -eq 0 ]
					then
						CLASS="solaris_sparc64_v8"
					fi
				fi
			fi

			uname -r | grep 5.9 > /dev/null
			RET=$?
			if [ $RET -eq 0 ]
			then
				CLASS="solaris_sparc_v9"

				if [ $FALLBACK_CLASSIFY -ne 1 ]
				then
					isainfo -v | grep 64-bit > /dev/null
					RET=$?
					if [ $RET -eq 0 ]
					then
						CLASS="solaris_sparc64_v9"
					fi
				fi
			fi

			uname -r | grep 5.10 > /dev/null
			RET=$?
			if [ $RET -eq 0 ]
			then
				CLASS="solaris_sparc_v10"

				if [ $FALLBACK_CLASSIFY -ne 1 ]
				then
					isainfo -v | grep 64-bit > /dev/null
					RET=$?
					if [ $RET -eq 0 ]
					then
						CLASS="solaris_sparc64_v10"
					fi
				fi
			fi

# 			this is the fallback default if it was not possible to detect the correct solaris version
			if [ -z "$CLASS" ]
			then
				CLASS="solaris_sparc_v8"

				if [ $FALLBACK_CLASSIFY -ne 1 ]
				then
					isainfo -v | grep 64-bit > /dev/null
					RET=$?
					if [ $RET -eq 0 ]
					then
						CLASS="solaris_sparc64_v8"
					fi
				fi
			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_v39"
			fi
			;;
		netbsd)
			CLASS="netbsd"
			;;
		darwin)
			CLASS="macosx_x86"

			uname -a | grep " i386" > /dev/null
			if [ $? -eq 0 ]
			then
				CLASS="macosx_x86_32"
			fi

			if [ $FALLBACK_CLASSIFY -ne 1 ]
			then
				uname -a  | grep x86_64 > /dev/null
				RET=$?
				if [ $RET -eq 0 ]
				then
					CLASS="macosx_x86_64"
				fi
			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

	if [ -n "$VERSION" ]
	then
		VERSION="-$VERSION"
	fi

	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
# AVCS4F3A4200C37O030000000203000002A4010000000000000000000000000303000000650300000200P000000000000000020633D7E9378132CBB2472824146A94091AF320EE5F99D35308145E62C49583F679C7BF1EFA0A4BE2018BFA884AFFCEDF0357FCD787BF1FA33AC023238462901F8C5587B588422E63473666B62BD02BC45576C2261AD36A2D2D3F9BB2AA4A1C3A5AE07529700D1CBEB680351FD3E7B2B0EAF70D735DF04B431474E6562A30FF8207F9EDAC30A406A80E10836F85AF82A8F6F1629FF4F1F3EEC4B5F008A695A463918926A3EEBEF54C31A7C5F1A6C713B6E97F753D825F96DE9734671AE03B6AEBF437D036639C9C3A411409A331F28223165A694FC473399C98B90770280F31799CE5CF224A481154AE9E90E266D71AE5D7D5BACAA9149A96F4702979884216D00000000000000000000000000000292030000000003T62F3AB0132FAVCSE