#!/bin/bash
# check script for the ATI Radeon drivers for Linux/x86(IA32 and x86_64) with XFree 4.x

# projects name
MODULE=fglrx

# strings
VENDOR_TEXT="ATI Technologies"
MODULE_SPACED="ATI Radeon 8500, FireGL 8700/8800"

# file names
DRM_LIB_PREFIX=lib${MODULE}drm
IP_LIB_PREFIX=lib${MODULE}_ip

# script insertion markers
MODULE_UPPER=FGLRX

# i686 or x86_64
PlatformName=`uname -m`
# remap all old x86 platforms to the i686 string
if [ "$PlatformName" = "i386" ]; then
    PlatformName=i686
fi
if [ "$PlatformName" = "i486" ]; then
    PlatformName=i686
fi
if [ "$PlatformName" = "i586" ]; then
    PlatformName=i686
fi

# general locations
HERE=`pwd`
XXX="$HERE"/XXX

i=0
i=$(($i+`echo "$HERE" | grep -c \ `))
i=$(($i+`echo "$HERE" | grep -c \*`))
i=$(($i+`echo "$HERE" | grep -c \\\\\\\\`))
i=$(($i+`echo "$HERE" | grep -c \"`))
i=$(($i+`echo "$HERE" | grep -c \'`))
i=$(($i+`echo "$HERE" | grep -c \\\``))
if [ $i -ne 0 ];
then
  echo "Path to current working directory contains critical characters"
  echo "like an asterisk, backslash, quotes or spaces. Since some system"
  echo "tools will not cope with such characters this script will abort now."
  echo "Unable to determine XFree86 Version. Stopping now."  
  echo ""
  exit 1
fi

if [ "$PlatformName" = "x86_64" ]; then
    LIB=lib64
else
	LIB=lib
fi



# xfree locations
XF_ROOT=/usr/X11R6
XF_BIN=$XF_ROOT/bin
XF_LIB=$XF_ROOT/${LIB}
XF_INC=$XF_ROOT/include
XF_MOD=$XF_LIB/modules
XF_DRV=$XF_MOD/drivers
XF_DRI=$XF_MOD/dri
XF_LIN=$XF_MOD/linux
XF_EXT=$XF_MOD/extensions

# system locations
OS_MOD=/lib/modules
USR_LIB=/usr/${LIB}

# list of dirs that are used for verifying xfree 4.x presence
XFREEDIRS="$XF_BIN \
           $XF_LIN \
           $XF_DRV \
           $XF_EXT \
	  "

# list of dirs that are used for verifying xfree 4.x presence
XFREEFILES="$XF_BIN/XFree86 \
           "

# vendor options - tightly related to supplied package contents - do not change!
INSTALL_LIBGL=0
INSTALL_LIBGL_V2=1
INSTALL_MODULE_SUBDIRS=0
INSTALL_INIT_SERVICE=0
INSTALL_MODULE_MAKE_INSTALL=1
INSTALL_LIBPATH=0

# shell script defaults
OPTION_FORCE=0
OPTION_QUERY=1
report_tgz=0

# shell script subroutines
GetOsInfo()
{
	echo "Checking which OS you're running..."

	OS_ame="`uname`"
	OsVersion="`uname -r`"
	case "$OS_ame" in
	SunOS) # Assumes SunOS 5.x
		OsArch="`uname -p`"
		;;
	*)
		OsArch="`uname -m`"
		;;
	esac
	# Some SVR4.0 versions have a buggy uname that reports the node name
	# for the OS name.  Try to catch that here.  Need to check what is
	# reported for non-buggy versions.
	if [ "$OS_ame" = "`uname -n`" -a -f /stand/unix ]; then
		OS_ame=UNIX_SV
	fi
	echo "uname reports '$OS_ame' version '$OsVersion', architecture '$OsArch'."

	# Find the object type, where needed

	case "$OS_ame" in
	Linux|FreeBSD|NetBSD)
		if file -L /bin/sh | grep ELF > /dev/null 2>&1; then
			OsObjFormat=ELF
		else
			OsObjFormat=a.out
		fi
		;;
	esac

	if [ X"$OsObjFormat" != X ]; then
        echo "Object format is '$OsObjFormat'."
		needNL=YES
	fi

	# Sets test flags for dealing with symlinks
	#
	# For OSes that don't support symlinks, choose a type that is guaranteed to
	# return false for regular files and directories.

	case "$OS_ame" in
	FreeBSD)
		case "$OsVersion" in
		2.*)
			L="-h"
			;;
		*)
			L="-L"
		esac
		;;
	OS-with-no-symlinks)	# Need to set this correctly
		L="-b"
		NoSymlinks=YES
		;;
	*)
		L="-L"
		;;
	esac

	# Find the libc version, where needed
	case "$OS_ame" in
	Linux)
		tmp="`ldd /bin/sh | grep libc.so 2> /dev/null`"
		LibcPath=`expr "$tmp" : '[^/]*\(/[^ ]*\)'`
		tmp="`strings $LibcPath | grep -i 'c library'`"
		OsLibcMajor=`expr "$tmp" : '.* \([0-9][0-9]*\)'`
		OsLibcMinor=`expr "$tmp" : '.* [0-9][0-9]*\.\([0-9][0-9]*\)'`
		case "$OsLibcMajor" in
		2)
			# 2 is the glibc version
			OsLibcMajor=6
			;;
		esac
		;;
	esac

	if [ X"$OsLibcMajor" != X ]; then
		if [ X"$OsLibcMinor" != X ]; then
			echo "libc version is '$OsLibcMajor.$OsLibcMinor'."
		else
			echo "libc version is '$OsLibcMajor'."
		fi
		needNL=YES
	fi
	if [ X"$needNL" = XYES ]; then
		echo ""
	fi
#   echo ""
}

GetXfreeInfo()
{
    if [ $chk_env -eq 0 ]
    then
      FILE=$XF_BIN/XFree86
    else
      FILE=../../xc/programs/Xserver/XFree86
    fi

    x_ver_line=`$FILE -version 2>&1 | grep "XFree86 Version"`
    x_ver=`echo "$x_ver_line" | cut -d' ' -f3`

    if [ "$x_ver" = "" ]
    then
      echo "You are either not running this script from the console"
      echo "or simply do not have console ownership.  Requirement failed."
      echo "Unable to determine XFree86 Version. Stopping now."
      echo ""
      exit 1
    fi

    if [ "$PlatformName" = "x86_64" ]; then
        Is64BitOS=" (64-bit)"
    else
        Is64BitOS=""
    fi

    if [ $chk_env -eq 0 ]
    then
      echo "XFree86 Version $x_ver$Is64BitOS is installed."
    else
      echo "XFree86 Version $x_ver$Is64BitOS was found in your current source environment."
    fi
    echo ""

    # some post processing of our main result
    X_4_0=0
    X_4_1=0
    X_4_2=0
    X_4_3=0
    X_good=0
    # X4.0
    if [ `echo "$x_ver" | grep -c '^4\.0\.[23]$'` -gt 0 ]
    then
      X_4_0=1
      X_good=1
    fi
    # X4.1
    if [ `echo "$x_ver" | grep -c '^4\.0\.99'` -gt 0 ]
    then
      X_4_1=1
      X_good=1
    fi
    if [ `echo "$x_ver" | grep -c '^4\.1\.0'` -gt 0 ]
    then
      X_4_1=1
      X_good=1
    fi
    # X4.2
    if [ `echo "$x_ver" | grep -c '^4\.2\.[01]'` -gt 0 ]
    then
      X_4_2=1
      X_good=1
    fi
    # X4.3
    if [ `echo "$x_ver" | grep -c '^4\.2\.99'` -gt 0 ]
    then
      X_4_3=1
      X_good=1
    fi
    if [ `echo "$x_ver" | grep -c '^4\.3\.0'` -gt 0 ]
    then
      X_4_3=1
      X_good=1
    fi
}

FindPackageName()
{
	case "$OS_ame" in
	DGUX)	# Check this string
		case "$OsArch" in
		i*86)
			Message="DGUX binaries are not available"
			;;
		*)
			Message="DGUX binaries are not available"
			;;
		esac
		;;
	FreeBSD)
		case "$OsArch" in
		i386)
			case "$OsVersion" in
			2.2*)
			        Message="FreeBSD-2.2.x binaries are not available"
				;;
			3.*)
				case "$OsObjFormat" in
				ELF)
			                Message="FreeBSD-3.x binaries are not available"
					;;
				*)
			                Message="FreeBSD-3.x binaries are not available"
					;;
				esac
				;;
			4.*)
			        Message="FreeBSD-4.x binaries are not available"
				;;
			*)
				Message="FreeBSD/i386 binaries are not available"
				;;
			esac
			;;
		alpha)
			case "$OsVersion" in
			3.*)
			        Message="FreeBSD-alpha-3.x binaries are not available"
				;;
			4.*)
			        Message="FreeBSD-alpha-4.x binaries are not available"
				;;
			*)
				Message="FreeBSD/alpha binaries are not available"
				;;
			esac
			;;
		*)
			Message="FreeBSD binaries are not available"
			;;
		esac
		;;
	Linux)
        x_package_suffix=""
        if [ $X_4_0 -gt 0 ] 
        then
            x_package_suffix="-X40"
        fi
        if [ $X_4_1 -gt 0 ] 
        then
            x_package_suffix="-X41"
        fi
        if [ $X_4_2 -gt 0 ] 
        then
            x_package_suffix="-X42"
        fi
        if [ $X_4_3 -gt 0 ] 
        then
            x_package_suffix="-X43"
        fi

		case "$OsArch" in
		x86_64)
			case "$OsLibcMajor" in
			5)
				PackageName="flibc5.tgz"
				;;
			6)
				case "$OsLibcMinor" in
				0)
                    echo "No package available for glibc 2.$OsLibcMinor.  Try ${MODULE}-glibc21${x_package_suffix}.tgz"
                    PackageName="${MODULE}-glibc21${x_package_suffix}.tgz"
                    ;;
                1)
                    PackageName="${MODULE}-glibc21${x_package_suffix}.tgz"
                    ;;
                2)
                    PackageName="${MODULE}-glibc22${x_package_suffix}.tgz"
                    ;;
                *)
                    echo "No package available for glibc 2.$OsLibcMinor.  Try ${MODULE}-glibc22${x_package_suffix}.tgz"
                    PackageName="${MODULE}-glibc21${x_package_suffix}.tgz"
					;;
				esac
				;;
			*)
				case "$OsObjFormat" in
				a.out)
					Message="Linux a.out is no longer supported"
					;;
				*)
					Message="No Linux/ix86 binaries for this libc version"
					;;
				esac
				;;
			esac
			;;
		i*86)
			case "$OsLibcMajor" in
			5)
				PackageName="flibc5.tgz"
				;;
			6)
				case "$OsLibcMinor" in
				0)
                    echo "No package available for glibc 2.$OsLibcMinor.  Try ${MODULE}-glibc21${x_package_suffix}.tgz"
                    PackageName="${MODULE}-glibc21${x_package_suffix}.tgz"
                    ;;
                1)
                    PackageName="${MODULE}-glibc21${x_package_suffix}.tgz"
                    ;;
                2)
                    PackageName="${MODULE}-glibc22${x_package_suffix}.tgz"
                    ;;
                *)
                    echo "No package available for glibc 2.$OsLibcMinor.  Try ${MODULE}-glibc22${x_package_suffix}.tgz"
                    PackageName="${MODULE}-glibc21${x_package_suffix}.tgz"
					;;
				esac
				;;
			*)
				case "$OsObjFormat" in
				a.out)
					Message="Linux a.out is no longer supported"
					;;
				*)
					Message="No Linux/ix86 binaries for this libc version"
					;;
				esac
				;;
			esac
			;;
		alpha)
			case "$OsLibcMajor.$OsLibcMinor" in
			6.1)
				Message="Linux/alpha binaries are not available"
				;;
			6.*)
				Message="Linux/alpha binaries are not available"
				;;
			*)
				Message="Linux/alpha binaries are not available"
				;;
			esac
			;;
		*)
			Message="No Linux binaries available for this architecture"
			;;
		esac
		;;
	LynxOS)	# Check this
		Message="LynxOS binaries are not available"
		;;
	NetBSD)
		case "$OsArch" in
		i386)
			case "$OsVersion" in
			1.[3-9]*)	# Check this
				case "$OsObjFormat" in
				a.out)
					Message="NetBSD binaries are not available"
					;;
				*)
					Message="NetBSD binaries are not available"
					;;
				esac
				;;
			*)
				Message="NetBSD binaries are not available"
				;;
			esac
			;;
		*)
			Message="NetBSD binaries are not available"
			;;
		esac
		;;
	OpenBSD)
		case "$OsArch" in
		i386)
			case "$OsVersion" in
			2.[6-9]*)	# Check this
				Message="OpenBSD binaries are not available"
				;;
			*)
				Message="OpenBSD binaries are not available"
				;;
			esac
			;;
		*)
			Message="OpenBSD binaries are not available"
			;;
		esac
		;;
	SunOS)
		case "$OsArch" in
		i386)
			case "$OsVersion" in
			5.[67]*)
				Message="SunOS binaries are not available"
				;;
			5.8*)
				Message="SunOS binaries are not available"
				;;
			*)
				Message="SunOS binaries are not available"
				;;
			esac
			;;
		*)
			Message="SunOS binaries are not available"
			;;
		esac
		;;
	UNIX_SV)
		case "$OsArch" in
		i386)
			case "$OsVersion" in
			4.0*)
				Message="SYSV binaries are not available"
				;;
			*)
				# More detailed version check??
				Message="SYSV binaries are not available"
				;;
			esac
			;;
		*)
			Message="SYSV binaries are not available"
			;;
		esac
		;;
	*)
		Message="No binaries available for this OS"
		;;
	esac

	if [ X"$PackageName" != X ]; then
      if [ $report_tgz -gt 0 ]; then
		echo "Binary package name is '$PackageName'"
      fi
	else
		if [ X"$Message" = X ]; then
			echo "Unable to determine which package you should use."
		else
			echo "$Message"
		fi
	fi
    echo ""
}

#begin of main script
echo -e "====================================================================="   
echo -e "\033[31m $VENDOR_TEXT \033[0m"                               
echo -e "====================================================================="   

# test for commandline arguments
chk_env=0
if [ "$1" = "-check-environment" ]; then
  chk_env=1
fi

if [ "$1" = "--force" ]; then
  echo "Option 'force' detected."
  OPTION_FORCE=1
  # implies -check-environment
  chk_env=1
fi


# perform a basic system analysis
#GetOsInfo
GetXfreeInfo
#FindPackageName

### EOF ###
