#!/bin/sh
#
# Report System Information
#
# Copyright (c) 2010 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=""

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