#!/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()
{
  skript=$1

  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"
  if test "$skript" != ""; then
    echo Skript: "$skript"
  fi
  echo "*** DEBUG END ***"
  echo
}

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

if test "$1" != "--num-3dboards"; then
  echo "3Ddiag version 0.496"
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   | \
      --mesaglide  | \
      --mesasoft   | \
      --nvidia_glx | \
      --omit-config | \
      --num-3dboards | \
      --runtime) 
        option="$1"
        ;;
      *)
        option="--help"
        ;;
    esac
  fi
fi

case "$option" in
  --devel)
    grep -q SuSE-Linux-Professional \
      /var/adm/current_package_descr/suse/setup/descr/info && \
      3Ddiag.devel
    exit 0
    ;;
  --dri)
    echo "Verifying DRI/XFree86 4.x configuration"
    INFO=3Ddiag.dri
    ;;
  -h|--help)
    echo "Usage: 3Ddiag [OPTION]"
    echo
    grep -q SuSE-Linux-Professional \
      /var/adm/current_package_descr/suse/setup/descr/info && \
    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 "  --mesaglide   verify 'Mesa/Glide' configuration"
    echo "  --mesasoft    verify 'Mesa Software Rendering' configuration"
    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
    ;;
  --mesaglide)
    echo "Verifying Mesa/Glide configuration"
    INFO=3Ddiag.mesaglide
    ;;
  --mesasoft)
    echo "Verifying 'Mesa Software Rendering' configuration:"
    echo "Using 3Ddiag.mesasoft"
    3Ddiag.mesasoft
    grep "7.[01]" /etc/SuSE-release > /dev/null 2>&1
    if test $? -ne 0; then
      3Ddiag.runtime
    fi
    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:::xf86_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`
  # SaX2 needs this
  if test "$option" != "--omit-config"; then
    server=`echo "$line"|cut -d ":" -f 3`
    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 "," " "`

  grep "7.0" /etc/SuSE-release > /dev/null 2>&1
  if test $? -ne 0; then
    skript=`echo "$line"|cut -d ":" -f 9`
  fi

  #debug $skript

  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
  for pkg in $packages; do
    if test $pkg == "nv_glx"; then
      echo "Tests for packages \"nv_glx\", \"NVIDIA_GLX\", \"NVIDIA_kernel\":"
    else
      echo "Tests for package \"$pkg\":"
    fi
    echo -n "  package ... "
    rpm -q $pkg &> /dev/null
    if test $? -eq 0; then
      echo  "done."
      # SuSE 7.2 comes with NVIDIA_GLX/NVIDIA_kernel dummy packages
      if test $pkg == "NVIDIA_GLX" -o $pkg == "NVIDIA_kernel"; then
         rpm -ql $pkg|grep -i dummy &> /dev/null
         # Do not verify during installation (confuses customer, Bug #18988)
         if test $? -eq 0 -a ! -f /var/lib/YaST2/run_suseconfig; then
           echo "======================= !!! WARNING !!! ========================"
           echo "Due to license issues this package only contains a Dummy"
           echo "Nvidia 2D/3D driver with Software 3D/OpenGL rendering. Please"
           echo "update the package $pkg with YOU (YaST2 Online Update)"
           echo "or download the package directly from the nVidia webserver"
           echo "(http://www.nvidia.com), if you want to use 3D hardware"
           echo "accelerated 3D/OpenGL."
           echo "================================================================"
         fi
      fi 
      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
    elif test $pkg == "nv_glx"; then
      rpm -q NVIDIA_GLX NVIDIA_kernel &> /dev/null
      if test $? -eq 0; then
        echo  "done."
        echo  -n "  package files ... "
        skript="switch2nvidia_glx"        
        rpm -V NVIDIA_GLX NVIDIA_kernel &> /dev/null
        if test $? -ne 0; then
          echo "failed!"
          echo "================================================================"
          echo "Some file(s) of packages \"NVIDIA_GLX/NVIDIA_kernel\" are"
          echo "missing/modified:"
          rpm -V NVIDIA_GLX NVIDIA_kernel
          echo
          echo "Consider to reinstall the packages \"NVIDIA_GLX\" + \"NVIDIA_kernel\"!"
          echo "================================================================"
        else
          echo "done."
        fi
      else
        echo "failed!"
        echo "================================================================"
        echo "Package \"nv_glx\" or packages \"NVIDIA_GLX\"+\"NVIDIA_kernel\""
        echo "for SuSE Linux must be installed with YaST(2)!"
        echo "================================================================"
      fi
    else
      echo "failed!"
      echo "================================================================"
      echo "Package \"$pkg\" must be installed with YaST(2)!"
      echo "================================================================"
    fi
  done
  echo
  echo "Tests for correct OpenGL libraries/GLX extensions:"
  if test "$skript" != "" -a -x /usr/X11R6/bin/$skript; then
    echo -n "  Symbolic Links ... "
    /usr/X11R6/bin/$skript -test
    if test $? -eq 0; then
      echo "done."
    else
      echo "failed!"
      echo "================================================================"
      echo "3D Hardware acceleration is not used. To use 3D"
      echo "Hardware acceleration the skript"
      echo
      echo " \"/usr/X11R6/bin/$skript\""
      echo
      echo "must be executed."
      echo "================================================================"
    fi
  fi
  if test "$rc_config" != ""; then
    echo -n "  /etc/sysconfig/3ddiag (SCRIPT_3D=$skript) ... "
    grep "^SCRIPT_3D=\"$skript\"" /etc/sysconfig/3ddiag &> /dev/null
    if test $? -eq 0; then
       echo "done."
    else
       echo "failed!"
       echo "================================================================"
       echo "3D Hardware acceleration is not used. To use 3D"
       echo "Hardware acceleration the entry"
       echo
       echo "  SCRIPT_3D=\"$skript\""
       echo
       echo "is required in /etc/sysconfig/3ddiag. Execute /sbin/SuSEconfig"
       echo "after changing this."
       echo "================================================================"
       
    fi
  fi    
  echo
  echo -n "Test for correct XFree86 version ... "
  if test "$XF86Version" == "$xf86version"; then
    echo "done."
    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
  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! You should consider to use"
  echo "'Mesa Software Rendering'. You can verify this configuration"
  echo "with the command \"3Ddiag --mesasoft\"."
  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

echo "----------------------- NOTE -----------------------------------"
echo "If 3D hardware OpenGL configuration is not stable enough, you"
echo "should switch back to 'Mesa Software Rendering'. You can"
echo "verify this configuration with the command \"3Ddiag --mesasoft\"."
echo "----------------------- NOTE -----------------------------------"

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

rm -rf $TMPDIR

exit 0


