#!/bin/sh

XCONF_FILE="XF86Config"
GLLIB_PATH="libGL/libGL.so*"
GLX_PATH="servGL/glx.so"
GLX_HEADERS="include/GL/gl*.h"


# warning, we're not done yet!
echo "This is an alpha version, and nowhere near complete yet!!!"
echo ""

# first introduce ourselves to the user, and explain what's going on
echo "First, we need to modify your XF86Config file."
echo "I will look in the standard places for it, so if you don't have"
echo "it in the usual place, please let me know where I can find the file."
echo ""
echo "If that doesn't make sense to you, just 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 "I found the file at ${XCONF_PATH}."
            echo "I don't appear to have read/write access!"
            echo "You probably need to be running this script as 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 "I found the file at ${XCONF_PATH}/${XCONF_FILE}."
               echo "I don't appear to have read/write access!"
               echo "You probably need to be running this script as root."
               echo ""
            fi
         else
            # nope, can't find it.
            echo "I wasn't able to find ${XCONF_FILE} at ${XCONF_PATH}"
            echo "Please try a different path, or press <ctrl-c> to abort"
            echo "and read the README file"
            echo ""
         fi
      fi
   else
      echo "I'll look in the regular places.."
      # 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 "I found the file at /etc/${XCONF_FILE}."
            echo "I don't appear to have read/write access!"
            echo "You probably need to be running this script as 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 "I found the file at /etc/X11/${XCONF_FILE}."
               echo "I don't appear to have read/write access!"
               echo "You probably need to be running this script as root."
               echo ""
            fi
         else
            echo "I was unable to find your ${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 "I've found the 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 "Your X Server appears to already be set up for GLX!"
   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. Your X Server is now configured to use GLX!"
   fi
else
   # didn't find it, we can just append a module section to the file
   echo "" >> ${XCONF_PATH}
   echo '# Added by glx_install script' >> ${XCONF_PATH}
   echo 'Section "Module"' >> ${XCONF_PATH}
   echo '   Load "glx.so"' >> ${XCONF_PATH}
   echo 'EndSection'       >> ${XCONF_PATH}
   echo "All set. Your X Server is now configured to use GLX!"
fi


# now we need to copy the appropriate files to the X Tree.

echo "Now I'm going to copy the GLX files to your X Server tree."
echo "If your X Server has been installed somewhere other than /usr/X11R6"
echo "you need to let me know where to find it."
echo ""
echo "If unsure, just press <enter>"

found=0
while test ${found} -eq 0
do
   echo -n "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}/lib"; then
         # it exists, and we have write access. Copy the files.
         # '-d' option preserves links
         echo Copying Client libraries
         cp -d ${GLLIB_PATH} ${XTREE_PATH}/lib >& /dev/null
         echo Creating Library links
         ln -s ${XTREE_PATH}/lib/libGL.so ${XTREE_PATH}/lib/libMesaGL.so.3 >& \
               /dev/null
         ln -s ${XTREE_PATH}/lib/libMesaGL.so.3 ${XTREE_PATH}/lib/libMesaGL.so \
               >& /dev/null
         echo Copying Server module
         if [ ! -d "${XTREE_PATH}/lib/modules" ]; then
            mkdir ${XTREE_PATH}/lib/modules
         fi
         cp ${GLX_PATH} ${XTREE_PATH}/lib/modules
         echo Copying include files
         if [ ! -d "${XTREE_PATH}/include/GL" ]; then
            mkdir ${XTREE_PATH}/include/GL
         fi
         cp -rd ${GLX_HEADERS} ${XTREE_PATH}/include/GL
         if [ -f "${XTREE_PATH}/include/GL/GLXproto.h" ]; then
            rm -rf ${XTREE_PATH}/include/GL/GLXproto.h
         fi
         if which ldconfig >& /dev/null; then
            if ! ldconfig ; then
               echo "It appears ldconfig did not run properly."
               echo "You may need to be root to run it."
            fi
         else
            echo "Couldn't find ldconfig!"
            echo "You'll have to run it manually. Applications may not work"
            echo "until you do."
         fi
         found=1
      else
         echo "Found directory, but I don't have write access to it"
         echo "Make sure you have the proper access to that directory"
         echo "do you need to be root?"
         echo ""
      fi
   else
      echo "I couldn't find the directory ${XTREE_PATH}"
      echo "Please verify the directory exists and try again: "
      echo ""
   fi
done

