#!/usr/bin/env bash
#
# Copyright 2008 VMware, Inc.  All rights reserved.
#

# Must come before set -e.
. "`dirname $0`"/functions

set -e

[ $# -ne 1 ] && error "Incorrect number of arguments provided"

command=$1
PACKAGE=$(basename $(dirname $0))
MODULE=

xconfig="`find_first_exists /etc/X11/xorg.conf /etc/X11/XF86Config`"

# Subsitutes any Driver field that has the name $from with $to while
# preserving whitespace.
replace_driver()
{
   local from="$1"
   shift
   local to="$1"
   shift

   replace "s,Driver\(\s\+\)\"$from\",Driver\1\"$to\",g" "$xconfig"
}

configure_mouse()
{
   replace_driver mouse vmmouse
}

configure_display()
{
   replace_driver vesa vmware
}

deconfigure_mouse()
{
   replace_driver vmmouse mouse
}

deconfigure_display()
{
   replace_driver vmware vesa
}

if is_rhel && [ `get_rhel_major_version` -eq 5 ]; then
   configure_mouse()
   {
      python $LIBDIR/install/$PACKAGE/x-conf-rhel.py install vmmouse
   }

   configure_display()
   {
      python $LIBDIR/install/$PACKAGE/x-conf-rhel.py install vmware
   }

   deconfigure_mouse()
   {
      python $LIBDIR/install/$PACKAGE/x-conf-rhel.py uninstall vmmouse
   }

   deconfigure_display()
   {
      python $LIBDIR/install/$PACKAGE/x-conf-rhel.py uninstall vmware
   }
fi

case $PACKAGE in
   vmware-open-vm-tools-xorg-drv-display)
      MODULE=display
      ;;
   vmware-open-vm-tools-xorg-drv-mouse)
      MODULE=mouse
      ;;
   *)
      error "Unknown package $PACKAGE"
      ;;
esac

case "$command" in
   install)
      configure_$MODULE
      ;;
   upgrade)
      ;;
   uninstall)
      deconfigure_$MODULE
      ;;
   *)
      echo "Unknown option $1" >&2
      ;;
esac
