#!/bin/sh
# Copyright (c) 2001 SuSE GmbH Nuernberg, Germany.  All rights reserved.
# 
# Author: Stefan Dirsch, 2001
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 675 Mass Ave, Cambridge MA 02139, USA.

function debug()
{
  echo
  echo "*** DEBUG BEGIN ***"
  echo "$line"
  echo Model: "$model"
  echo XFree86 Version: "$xf86version"
  echo Server: "$server"
  echo Color Depths: "$colordepths"
  echo Extensions: "$extensions"
  echo Options: "$options"
  echo Raws: "$raws"
  echo Packages: "$packages"
  echo "*** DEBUG END ***"
  echo
}

export LANG=POSIX
#export PATH=.:$PATH
INFO=""

if test "$1" != "--num-3dboards"; then
  echo "3Ddiag version 0.703"
fi 

# create directory for tmp file
TMPDIR=$(/bin/mktemp -d /tmp/3Ddiag.XXXXXX) || { echo "3Ddiag: can not create temporary directory" >& 2; exit 1; }

if [ $# -ge 2 ]; then
  option="--help"
else
  if [ $# -eq 1 ]; then
    case $1 in 
      --devel      | \
      --dri        | \
      --help | -h  | \
      --ignoredb   | \
      --nvidia_glx | \
      --omit-config | \
      --num-3dboards | \
      --runtime) 
        option="$1"
        ;;
      *)
        option="--help"
        ;;
    esac
  fi
fi

case "$option" in
  --devel)
    3Ddiag.devel
    exit 0
    ;;
  --dri)
    echo "Verifying DRI/XFree86 4.x configuration"
    INFO=3Ddiag.dri
    ;;
  -h|--help)
    echo "Usage: 3Ddiag [OPTION]"
    echo
    echo "  --devel       verify full OpenGL Development configuration"
    echo "  --dri         verify 'DRI/XFree86 4.x' configuration"
    echo "  --help, -h    display this help and exit"
    echo "  --ignoredb    search for possible 3D configurations of might be" 
    echo "                untested/unsuccessful tested hardware"
    echo "  --nvidia_glx  verify 'nVidia GLX/XFree86 4.x' configuration"
    echo "  --omit-config omit XF86Config check"
    echo "  --num-3dboards count number of 3D capable devices"
    echo "  --runtime     verify GLU/glut Runtime configuration"
    exit 0
    ;; 
  --ignoredb)
    echo "Searching for configurations of untested/unsuccesfull tested hardware"
    echo "Using 3Ddiag.ignoredb"
    3Ddiag.ignoredb
    exit 0
    ;;
  --nvidia_glx)
    echo "Verifying nVidia-GLX/XFree86 4.x configuration"
    INFO=3Ddiag.nvidia_glx
    ;;
  --num-3dboards)
    # Number of 3D capable devices"
    num=`/usr/bin/3dinfo | wc -l`
    exit $num
    ;;
  --runtime)
    3Ddiag.runtime
    exit 0
    ;;
  *)
    ;;
esac 

/usr/X11R6/bin/X -version 2>&1|grep "XFree86 Version"|awk '{print $3}'|grep ^4 &> /dev/null
if test $? -eq 0; then
  XF86Version="4"
else
  XF86Version="3"
fi

grep "7.[01]" /etc/SuSE-release > /dev/null 2>&1
if test $? -ne 0; then
  rc_config="true"
fi

if [ "$INFO" == "" ]; then
  echo "Verifying 3D configuration:"
  if [ -x ./3dinfo -o -x /usr/bin/3dinfo ]; then
    INFO="3dinfo"
  else
    echo "3dinfo program not found!"
    exit 0
  fi
fi

echo "Using $INFO"

rm -f $TMPDIR/3Dboard

#echo "Voodoo-3:4:tdfx:16:glx,dri:::XFree86-GLX:switch2xf86_glx"| \
$INFO | \
while read line; do
  touch $TMPDIR/3Dboard
  model=`echo "$line"|cut -d ":" -f 1`
  xf86version=`echo "$line"|cut -d ":" -f 2`
  server=`echo "$line"|cut -d ":" -f 3`
  # SaX2 needs this
  if test "$option" != "--omit-config"; then
    colordepths=`echo "$line"|cut -d ":" -f 4|tr "," " "`
    extensions=`echo "$line"|cut -d ":" -f 5|tr "," " "`
    options=`echo "$line"|cut -d ":" -f 6|tr "," " "`
    raws=`echo "$line"|cut -d ":" -f 7`
  fi
  packages=`echo "$line"|cut -d ":" -f 8|tr "," " "`

  #debug

  if test "$xf86version" == "3"; then
    xf86config="/etc/XF86Config"
    sax="\"SaX\""
    defaultdepth="DefaultColorDepth"
  else
    xf86config="/etc/X11/XF86Config"
    sax="\"SaX2\""
    defaultdepth="DefaultDepth"
  fi    

  echo "************************************************************"
  echo
  echo "Verifying 3D configuration based on XFree86 $xf86version for 3D board" 
  echo "\"$model\":"
  echo
  if test "$packages" != ""; then
   for pkg in $packages; do
    echo "Tests for package \"$pkg\":"
    echo -n "  package ... "
    rpm -q $pkg &> /dev/null
    if test $? -eq 0; then
      echo  "done."
      echo  -n "  package files ... "
      rpm -V $pkg &> /dev/null
      if test $? -ne 0; then
        echo "failed!"
        echo "================================================================"
        echo "Some file(s) of package $pkg are missing/modified:"
        rpm -V $pkg
        echo
        echo "Consider to reinstall the package $pkg!"
        echo "================================================================"
      else
        echo "done."
      fi
    else
      echo "failed!"
      echo "================================================================"
      echo "Package \"$pkg\" must be installed with YaST(2)!"
      echo "================================================================"
    fi
   done
  fi
  if test "$server" == "nvidia"; then
    echo
    echo "Verifying driver installation:"
    echo -n "  $server ... "
    /usr/X11R6/bin/xsload -vendor | grep -q "nvidia:NVIDIA Corporation"
    if test $? -eq 0; then
      echo "done."
    else
        echo "failed!"
        echo "======================= !!! WARNING !!! ========================"
        echo "Due to license issues only a Dummy Nvidia 2D/3D driver with"
        echo "Software 3D/OpenGL rendering is provided. Please download"
        echo "the official 2D/3D Nvidia driver from the nVidia webserver"
        echo "(http://www.nvidia.com), if you want to use 3D hardware"
        echo "accelerated 3D/OpenGL."
        echo "================================================================"
    fi
  fi
  echo
  echo -n "Test for correct XFree86 version ... "
  if test "$XF86Version" == "$xf86version"; then
    echo "done."
    if test "$option" != "--omit-config"; then
    echo
    echo "Tests for XFree86 configuration:"
    echo -n "  Config File $xf86config ... "
    if test -f $xf86config; then
      echo "done."
      if test "$XF86Version" == "4" -a "$server" != ""; then
        echo -n "  Driver ... "
        cat $xf86config | grep -v ^# | tr "\t" " " | \
	    grep -i -E "^[\ ]*Driver[\ ]+\"$server\"" > /dev/null 2>&1
        if test $? -eq 0; then
          echo "done."
        else
          echo "failed!"
          echo "================================================================"
          echo "3D Hardware acceleration is not used. To use 3D" 
          echo "Hardware acceleration please specify the entry" 
          echo
          echo  "  Driver \"$server\""
          echo
          echo "in the Section \"Device\" of your $xf86config."
          echo "================================================================"
        fi
      fi
      correct_color=false
      for depth in $colordepths; do 
        cat $xf86config | grep -v ^# | tr "\t" " " | \
	    grep -i -E "^[\ ]*$defaultdepth[\ ]+$depth" > /dev/null 2>&1
        if test $? -eq 0; then
          correct_color=true
        fi
      done
      echo -n "  Color Depth ... "
      if test "$correct_color" == "true" -o -z "$colordepths"; then
        echo "done."
      else
        echo "failed!"
        echo "================================================================"
        echo "3D Hardware acceleration is not used. To use 3D" 
        echo "Hardware acceleration please specify one of these entries" 
        echo
        for depth in $colordepths; do
          echo $defaultdepth $depth
        done
        echo
        echo "in the Section \"Screen\" of your $xf86config."
        echo "================================================================"
      fi
      echo -n "  Extensions ... "
      all_extensions=true
      for ext in $extensions; do
        cat $xf86config | grep -v ^# | tr "\t" " " | \
	        grep -E "^[\ ]*Load[\ ]+\"$ext\"" > /dev/null 2>&1
      	if test $? -ne 0; then
          all_extensions=false
        fi
      done
      if test "$all_extensions" == "true" -o -z "$extensions"; then
        echo "done."
      else
        echo "failed!"
        echo "================================================================"
        echo "3D Hardware acceleration is not used. To use 3D" 
        echo "Hardware acceleration please specify these entries" 
        echo
        for ext in $extensions; do
          echo "Load \"$ext\""
        done
        echo
        echo "in the Section \"Module\" of your $xf86config."
        echo "================================================================"
      fi
      echo -n "  Options ... "
      all_options=true
      for opt in $options; do
        cat $xf86config | grep -v ^# | tr "\t" " " | \
	    grep -i -E "^[\ ]*Option[\ ]+\"$opt\"" > /dev/null 2>&1
      	if test $? -ne 0; then
          all_options=false
        fi
      done
      if test "$all_options" == "true" -o -z "$options"; then
        echo "done."
      else
        echo "failed!"
        echo "================================================================"
        echo "3D Hardware acceleration is not used. To use 3D" 
        echo "Hardware acceleration please specify these entries" 
        echo
        for opt in $options; do
          echo "Option \"$opt\""
        done
        echo
        echo "in the Section \"Device\" of your $xf86config."
        echo "================================================================"
      fi
      # DOES NOT WORK FOR NOW
      if false; then
      echo -n "  Raw entries ... "
      rm -f $TMPDIR/$USER.raw_false
      echo $raws|awk -F "," '{for (i=1; i <= split($0,a); i++) print $i}'| \
      while read raws_line; do
        raws_line=`echo $raws_line|sed 's/\"/\\\"/g'`
        echo $raws_line
        cat $xf86config | grep -v ^# | tr "\t" " " | \
	    grep -i -E "^[\ ]*"$raws_line"[\ ]*" > /dev/null 2>&1
      	if test $? -ne 0; then
          touch $TMPDIR/$USER.raw_false
        fi
      done
      if test ! -f $TMPDIR/$USER.raw_false -o -z "$raws"; then
        echo "done."
      else
        echo "failed!"
        echo "=================================================="
        echo "3D Hardware acceleration is not used. To use 3D" 
        echo "Hardware acceleration please specify these entries" 
        echo
        echo $raws|awk -F "," '{for (i=1; i <= split($0,a); i++) print $i}'| \
        while read raws_line; do
          echo $raws_line
        done
        echo
        echo "in the Section \"Device\" of your $xf86config."
        echo "=================================================="
      fi
      rm -f $TMPDIR/$USER.raw_false
      fi # DOES NOT WORK FOR NOW STUFF
   else
      echo "failed!"
      echo "================================================================"
      echo -n "XFree86 $xf86version is not configured yet. Use $sax for "
      echo "configuration!"
      echo "================================================================"
    fi
    fi
  else
    echo "failed!"
    echo "================================================================"
    echo -n "You are using XFree86 $XF86Version, but XFree86 $xf86version " 
    echo "is required."
    echo "Run $sax to configure XFree86 $xf86version."
    echo "================================================================"
  fi
  echo
done

if test ! -f $TMPDIR/3Dboard; then
  echo "************************************************************"
  echo
  echo "================================================================"
  echo "No 3D capable graphic chipset found!"
  echo "================================================================"
  grep "7.[01]" /etc/SuSE-release > /dev/null 2>&1
  if test $? -ne 0; then
    3Ddiag.runtime
  fi
  exit 0
fi

/sbin/lspci -n|grep "8086:2530" &> /dev/null
if [ $? -eq 0 ]; then
  echo "--------------------- WARNING ----------------------------------"
  echo "i850 chipset detected! There are known problems with this" 
  echo "chipset, which could disable OpenGL 3D hardware support on" 
  echo "these systems!" 
  echo "--------------------- WARNING ----------------------------------"
  echo
fi

grep "7.[01]" /etc/SuSE-release > /dev/null 2>&1
if test $? -ne 0; then
    3Ddiag.runtime
fi

rm -rf $TMPDIR

exit 0


