#!/bin/sh -f
#
# Copyright (c) 2000 by Sun Microsystems, Inc.
# All Rights Reserved
#
# @(#)ogl_install_check.sh 1.9 00/03/07 SMI
#
# The 64-bit Solaris Application Development paper advises using a copy
# of /usr/lib/isaexec as the wrapper which precisely preserves the
# argument list. Since currently ogl_install_check does not use an
# argument list, isaexec does not exist before Solaris 7, and Sun OpenGL
# for Solaris must run on Solaris eariler than Solaris 7 this shell script 
# is used.
#
# This wrapper emits a warning if microcode installed is not a minimum
# microcode version.
#

CMD=`basename $0`
DIR=`dirname $0`
ISALIST=/usr/bin/isalist
UNAMEP=`/bin/uname -p`
EXEC=
EGREP=/bin/egrep
prtconf=`/usr/sbin/prtconf 2>&1 | ${EGREP} "(SUNW,afb|SUNW,Expert3D)"`

#
# Utility checking minimum Elite3D microcode installed version.
# Version 1.0
#
verify_Elite3D_microcode_version() {
    min_major=$1  # Minimum Elite3D microcode version
    min_minor=$2  # containing polygon offset optimization
    min_patch=$3

    STRINGS=/bin/strings
    AWK=/bin/awk
    AFB_UCODE=/usr/lib/afb.ucode
    CUT=/bin/cut

    isE3D=`echo "${prtconf}" | ${EGREP} SUNW,afb`
    if [ "${isE3D}" != "" -a -f ${AFB_UCODE} ]; then
	cur_ver=`${STRINGS} ${AFB_UCODE} | ${AWK} '{print $7}'`

	# Is the current version same as or newer than the minimum?
	cur_major=`echo ${cur_ver} | ${CUT} -d. -f1`
	cur_minor=`echo ${cur_ver} | ${CUT} -d. -f2`
	if [ ${cur_major} -lt ${min_major} -o \
	         ${cur_major} -le ${min_major} -a \
	         ${cur_minor} -lt ${min_minor} ]; then

		s251_min_patchid="105791-16"
		s26_min_patchid="105363-17"
		s7_min_patchid="106148-03 and 106144-05"
		UNAMER=`/bin/uname -r`
		UNAMEN=`/bin/uname -n`

		echo "WARNING: Sun OpenGL Elite3D requires a minimum patch \c"
		echo "for operating systems:"
		echo "  Solaris 2.5.1 : patchID  ${s251_min_patchid}"
		echo "  Solaris 2.6   : patchID  ${s26_min_patchid}"
		echo "  Solaris 7     : patchIDs ${s7_min_patchid}"
		echo "Using Sun OpenGL Elite3D without one of these patches"
		echo "will cause the system to hang requiring a power cycle."

		case ${UNAMER} in
		  5.5.1)
		    min_patchid=${s251_min_patchid} ;;
		  5.6)
		    min_patchid=${s26_min_patchid} ;;
		  5.7)
		    min_patchid=${s7_min_patchid} ;;
		   *) ;;
		esac
		echo "WARNING: Sun OpenGL Elite3D on ${UNAMEN} \c"
		echo "minimum required patch: ${min_patchid}\n"

		echo "Continue with ${CMD} (default=y, y/n)? \c"
		read readin
		if [ "${readin}" != "y" -a "${readin}" != "" ]; then
		    echo "${CMD} aborted."
		    exit 1
		fi
	fi
    fi
}

verify_Elite3D_microcode_version 1 3 0

#
# Utility checking first Expert3D microcode installed version.
# First microcode version (802) does not work.
# Version 1.0
#
verify_Expert3D_microcode_version() {

    IFB_UCODE=/usr/lib/ifb.ucode
    SUM=/bin/sum

    isEx3D=`echo "${prtconf}" | ${EGREP} SUNW,Expert3D`
    if [ "${isEx3D}" != "" -a -f ${IFB_UCODE} ]; then
	cur_ver=`${SUM} ${IFB_UCODE}`
        ver_sum="58769 225 /usr/lib/ifb.ucode"  # ver 802
        #ver_sum="43221 273 /usr/lib/ifb.ucode"  # ver 902

	# Does the current version need patching?
	if [ "${cur_ver}" = "${ver_sum}" ]; then

		s26_min_patchid="108788-01"
		s7_min_patchid="108787-01"
		s8_min_patchid="108576-02"
		UNAMER=`/bin/uname -r`
		UNAMEN=`/bin/uname -n`

		echo "WARNING: Sun OpenGL Expert3D requires a minimum patch \c"
		echo "for operating system:"
		echo "  Solaris 8     : patchID ${s8_min_patchid}"
		echo "Using Sun OpenGL Expert3D without this patch"
		echo "will cause the system to hang requiring a power cycle."
		case ${UNAMER} in
		  5.6)
		    min_patchid=${s26_min_patchid} ;;
		  5.7)
		    min_patchid=${s7_min_patchid} ;;
		  5.8)
		    min_patchid=${s8_min_patchid} ;;
		   *) ;;
		esac
		echo "WARNING: Sun OpenGL Expert3D on ${UNAMEN} \c"
		echo "minimum required patch: ${min_patchid}\n"

		echo "Continue with ${CMD} (default=y, y/n)? \c"
		read readin
		if [ "${readin}" != "y" -a "${readin}" != "" ]; then
		    echo "${CMD} aborted."
		    exit 1
		fi
	fi
    fi
}

verify_Expert3D_microcode_version

if [ -x ${ISALIST} ]; then
    for isa in `${ISALIST}`; do
	if [ -x ${DIR}/${isa}/${CMD} ]; then
	    EXEC=${DIR}/${isa}/${CMD}
	    break;
	fi
    done

else
    #
    # 32-bit default
    #
    case ${UNAMEP} in
      sparc)
        if [ -x ${DIR}/sparcv7/${CMD} ]; then
            EXEC=${DIR}/sparcv7/${CMD}
	fi
	;;
       *) ;;
    esac
fi

if [ -z "${EXEC}" ]; then
    echo 1>&2 "$0: no executable for this architecture"
    exit 1
fi

exec ${EXEC} "${@}"
