#!/bin/sh

XCONF_FILE="XF86Config"
GLLIB_FILES="libGL.so*"
GLLIB_FILE="libGL.so.1.0"
GLX_FILE="glx.so"
XSVGA_FILE="XF86_SVGA"

if test -f ${XSVGA_FILE}
then
   install_svga=0
   install_glx=0
   install_lib=0
   # now we need to copy the appropriate files to the X Tree.
   echo "Enter path to the X server tree."
   echo "If unsure, press <Enter>."
   found=0
   XTREE_PATH="/usr/X11R6"
   while test ${found} -eq 0
   do
      echo -n "Path [${XTREE_PATH}] :"
      read XTREE_PATH
      echo ""
   
      if [ ! ${XTREE_PATH} ]; then
	 XTREE_PATH="/usr/X11R6"
      fi
      if test -d ${XTREE_PATH}; then
	 if test -w "${XTREE_PATH}/bin"; then
	    # it exists, and we have write access. Copy the files here.
	    found=1
	 else
	    echo "Don't have write access to ${XTREE_PATH}."
	    echo "Make sure you have the proper access to that directory."
	    echo "(do you need to be root?)"
	    echo ""
	 fi
      else
	 echo "Couldn't find the directory ${XTREE_PATH}."
	 echo "Please verify the directory exists and try again: "
	 echo ""
      fi
   done
   # look for files to install
   install_svga=1
   echo "Installing X server: ${XSVGA_FILE}"
   if test -f ${GLX_FILE}
   then
      echo "Installing GLX module: ${GLX_FILE}"
      install_glx=1
   fi
   if test -f ${GLLIB_FILE}
   then
      echo "Installing GL client library: ${GLLIB_FILE}"
      install_lib=1
   fi
   echo ""
   # install GLX module.
   # need to set up the XF86Config Modules section - yuck.
   if test ${install_glx} -eq 1
   then
      # first introduce ourselves to the user, and explain what's going on
      echo "Modification to your XF86Config file is required to load the module."
      echo "If the configuration file is in a non-standard location, enter it here."
      echo "If unsure, press <Enter>."
      # we're going to look for the XF86Config file. First, get any user
      # input, then act on that data. Loop until we find the file, or the
      # user exits
      found=0
      while test ${found} -eq 0
      do
	 echo -n "Path: "
	 read XCONF_PATH
	 echo ""
	 if test ${XCONF_PATH}
	 then
	    # check if the user gave us the full path, including filename
	    if test -f "${XCONF_PATH}"
	    then
	       # there it is. Verify we have read/write access.
	       if test -w "${XCONF_PATH}"
	       then
		  found=1
	       else
		  echo "Don't have write access to ${XCONF_PATH}."
		  echo "(do you need to be root?)"
	       fi
	    else
	       # nope, see if we need to add the filename.
	       if test -f "${XCONF_PATH}/${XCONF_FILE}"
	       then
		  # there it is. 
		  if test -w "${XCONF_PATH}/${XCONF_FILE}"
		  then
		     found=1
		     XCONF_PATH="${XCONF_PATH}/${XCONF_FILE}"
		  else
		     echo "Don't have write access to ${XCONF_PATH}/${XCONF_FILE}."
		     echo "(do you need to be root?)"
		     echo ""
		  fi
	       else
		  # nope, can't find it.
		  echo "Unable to find ${XCONF_FILE} at ${XCONF_PATH}"
		  echo "Please try a different path, or press <ctrl-c> to abort"
		  echo "and read the FAQ file"
		  echo ""
	       fi
	    fi
	 else
	    # most standard place
	    if test -f "/etc/${XCONF_FILE}"
	    then
	       # found it, check permissions
	       if test -w "/etc/${XCONF_FILE}"
	       then
		  found=1
		  XCONF_PATH="/etc/${XCONF_FILE}"
		  echo "Found it in ${XCONF_PATH}"
	       else
		  echo "Don't have write access to /etc/${XCONF_FILE}."
		  echo "(do you need to be root?)"
		  echo ""
	       fi
	    else
	       # not there, let's check Red Hat's location
	       if test -f "/etc/X11/${XCONF_FILE}"
	       then
		  # found it, check permissions
		  if test -w "/etc/X11/${XCONF_FILE}"
		  then
		     found=1
		     XCONF_PATH="/etc/X11/${XCONF_FILE}"
		  else
		     echo "Don't have write access to /etc/X11/${XCONF_FILE}."
		     echo "(do you need to be root?)"
		     echo ""
		  fi
	       else
		  echo "Unable to find ${XCONF_FILE}! Are you sure you've"
		  echo "installed it, and haven't hand-compiled it?"
		  echo ""
	       fi
	    fi
	 fi
	 if test ${found} -eq 0; then
	    echo "Please try a different filename. You most likely need to be root"
	    echo "to successfully run this script. If you are having problems, you"
	    echo "can abort at any time by pressing <ctrl>-c"
	 else
	    echo "Found config file at ${XCONF_PATH}"
	    echo ""
	 fi
      done
      # verify that the needed change hasn't already been made!
      echo "Checking for existing GLX capabilities."
      # check for a Modules section, inside of which must be the glx string.
      if grep -i '^section "module"$' ${XCONF_PATH} >& /dev/null
      then
	 # found it. Let's see if glx is in that section
	 # use sed to extract exactly the Module section, and grep to look for glx
	 if cat ${XCONF_PATH} | \
	      sed -n '/[sS]ection "[mM]odule"/,/^[eE]nd[sS]ection/ p' | \
	      grep -i 'glx.so' >& /dev/null;
	 then
	    echo "The X Server appears to already be set up for the GLX module."
	    echo "If GLX isn't available when you start up the X server,"
	    echo "make sure the Section Module or Load glx.so isn't commented out."
	 else
	    cat ${XCONF_PATH} | \
	       sed '/[sS]ection "[mM]odule"/{x;s/^/   Load "glx.so"/;x;G;}' \
	       > "${XCONF_PATH}.tmp"
	    mv ${XCONF_PATH}.tmp ${XCONF_PATH}
	    echo "All set. The X Server is now configured to use the GLX module."
	 fi
      else
	 # didn't find it, we can just append a module section to the file
	 echo "" >> ${XCONF_PATH}
	 echo '# Added by riva_install script' >> ${XCONF_PATH}
	 echo 'Section "Module"' >> ${XCONF_PATH}
	 echo '   Load "glx.so"' >> ${XCONF_PATH}
	 echo 'EndSection'       >> ${XCONF_PATH}
      fi
      cp ${GLX_FILE} ${XTREE_PATH}/lib/modules
      echo ""
      echo 'Copied '${GLX_FILE}' to '${XTREE_PATH}'/lib/modules'
      # done installing the GLX module
   else
      echo ""
   fi
   # install GL libraries
   if test ${install_lib} -eq 1
   then
      cp -d ${GLLIB_FILES} ${XTREE_PATH}/lib
      echo 'Copied '${GLLIB_FILES}' to '${XTREE_PATH}'/lib'
      # done installing the GL libraries
   fi
   # install X server
   if test ${install_svga} -eq 1
   then
      cp ${XSVGA_FILE} ${XTREE_PATH}/bin
      echo 'Copied '${XSVGA_FILE}' to '${XTREE_PATH}'/bin'
      # done installing the GL libraries
   fi
fi
# done installing the X server.

