#!/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 Server: "$server"
  echo Extensions: "$extensions"
  echo Options: "$options"
  echo Packages: "$packages"
  echo "*** DEBUG END ***"
  echo
}

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

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

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/X.Org configuration"
    INFO=3Ddiag.dri
    ;;
  -h|--help)
    echo "Usage: 3Ddiag [OPTION]"
    echo
    echo "  --devel       verify full OpenGL Development configuration"
    echo "  --dri         verify 'DRI/X.Org' 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/X.Org' configuration"
    echo "  --omit-config omit xorg.conf 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/X.Org 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 

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"

tmpname=`basename $0`
tmpdir=`mktemp -d /tmp/$tmpname.XXXXXX`
if [ $? -ne 0 ]; then
  echo "$0: Can't create temp dir, exiting..."
  exit 1
fi

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

  #debug

  xf86config="/etc/X11/xorg.conf"
  sax="\"SaX2\""
  defaultdepth="DefaultDepth"

  echo "************************************************************"
  echo
  echo "Verifying 3D configuration for 3D board \"$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 ... "
    libdir="lib"
    test "$(arch)" == "x86_64" && libdir="lib64"
    if [ -r /usr/$libdir/xorg/modules/updates/drivers/nvidia_drv.so ]; then
      driver=/usr/$libdir/xorg/modules/updates/drivers/nvidia_drv.so
    else
      driver=/usr/$libdir/xorg/modules/drivers/nvidia_drv.so
    fi
    strings $driver | grep -q "NVIDIA dlloader X Driver"
    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
  if test "$option" != "--omit-config"; then
    echo
    echo "Tests for X.Org configuration:"
    echo -n "  Config File $xf86config ... "
    if test -f $xf86config; then
      echo "done."
      if test "$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
      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
    else
      echo "failed!"
      echo "================================================================"
      echo -n "X.Org is not configured yet. Use $sax for configuration!"
      echo "================================================================"
    fi
  fi
  echo
done

if test ! -f $tmpdir/3Dboard; then
  echo "************************************************************"
  echo
  echo "================================================================"
  echo "No 3D capable graphic chipset found!"
  echo "================================================================"
  3Ddiag.runtime
  exit 0
fi

3Ddiag.runtime

rm -rf $tmpdir

exit 0
