#!/bin/sh
#-------------------------------#
# Script for testing X-Servers  # 
# using a lex/yacc based parser # 
# Start this script to find out #
# the maximum RamDac            #
#-------------------------------#

#
# security setting
#
#set -o noclobber           

SERVER=$1
COLORS=$2
if [ -z "$SERVER" ];then
 exit 1
fi
if [ -z "$COLORS" ];then
 exit 1
fi

#------------------#
# init scan values #
#------------------#
DACSPEED=""

#---------------#
# prove on root #
#---------------#
if [ ! $UID = "0" ];then
 echo "TestServer: only root can run server test" 
 exit 1
fi

#-----------------#
# Getting Display #
#-----------------#
if [ -z "$DISPLAY" ]; then
 export DISPLAY=:0
 DISP=:0
else 
 DISP=`echo $DISPLAY | cut -f2 -d: | cut -c1`
 DISP=`expr $DISP + 1` 
fi
 
#--------------#
# path names   #
#--------------#
CONF=/var/X11R6/sax/config/TestConfig
PARS=/var/X11R6/sax/bin/ParseXerr
XBIN=/usr/X11R6/bin/

#--------------#
# run the test #
#--------------#
FILE="Info_"$SERVER
rm -f $INFO$FILE
DACSPEED=`$XBIN$SERVER -probeonly -xf86config $CONF -bpp $COLORS 2>&1 :$DISP | grep ^.[-*] | cut -c 0-80 | $PARS 2>&1 | grep dacspeed | cut -f2 -d:`

CLOCKS=`echo $DACSPEED | sed --silent "s/clocks:.//p"`
DACSPEED=`echo $DACSPEED | cut -f1 -d.`

#------------------------------#
# test if value is numeric     # 
# if yes decrease to 10%       #
# if no... something was wrong #
# set value to 60 MHz          #
#------------------------------#
expr $DACSPEED + 1 2>/dev/null >/dev/null
if [ $? = 0 ];then
 DACSPEED=`expr $DACSPEED \* 90`
 DACSPEED=`expr $DACSPEED / 100`
else
 DACSPEED="0"
fi

#------------------------------#
# test if fixed clocks are     #
# present, if set value to the #
# highest. If an error has     #
# occured set value to 60 MHz  #
#------------------------------#
maxclock=`for i in $CLOCKS; do echo $i; done | sort -n - | tail -1`
if [ -n "$maxclock" ]; then
 dacspeed=`echo $maxclock | cut -f1 -d.`;
 if [ $dacspeed -lt $DACSPEED ]; then
    DACSPEED=$dacspeed;
 fi
fi
if [ $DACSPEED -eq 0 ]; then
    DACSPEED="60"
fi


echo "$DACSPEED"
exit 0
