#!/bin/ksh
#
# Make sure the NVidia GLX driver and include files are properly installed

driver_major=1
driver_minor=0
driver_libdir=/usr/lib
driver_basename=libGL.so
nvidia_incdir=/usr/share/doc/NVIDIA_GLX-1.0/include/GL
opengl_incdir=/usr/include/GL
obsolete_incdir=${opengl_incdir}/obsolete

driver_version="$1"

if [ ! "$driver_version" ]; then
  echo "Usage: $0 <driver-version>"
  exit
fi

driver_shlib=${driver_basename}.${driver_major}.${driver_minor}.${driver_version}

echo "Libs..."
if [ ! -f ${driver_libdir}/${driver_shlib} ]; then
  echo "NVidia GL library ${driver_libdir}/${driver_shlib} not found. Wrong version?"
  exit
fi

for i in $(find ${driver_libdir}/ -name "${driver_basename}*"); do
  if [ -L $i ]; then
    echo "Changing symbolic link $i"
    rm $i
    ln -s ${driver_shlib} $i
  fi
done

echo "Includes..."
if [ ! -d ${nvidia_incdir} ]; then
  echo "NVIDIA GLX include dir ${nvidia_incdir} not found"
  exit
fi

for i in $(find ${nvidia_incdir}/ -name "*.h"); do
  leafname=${i##*/}
  curfile=${opengl_incdir}/${leafname}
  if [ -L ${curfile} ]; then
    echo "Replacing symbolic link ${curfile}"
    rm ${curfile}
  elif [ -f ${curfile} ]; then
    newname=${obsolete_incdir}/${leafname}
    if [ -d ${obsolete_incdir} ]; then
      mkdir ${obsolete_incdir}
    fi
    echo "Replacing file ${curfile} (old version copied to ${newname})"
    mv ${curfile} ${newname}
  fi
  ln -s $i ${curfile}
done
