#!/bin/bash
#
# $RCSfile: hpmouse,v $
# Original Author...: Anthony Awtrey
# Version...........: $Revision: 1.1.4.8 $
# Last Modified By..: $Author: ssherbert $
# Last Modified.....: $Date: 2007/01/05 21:47:08 $
#
# Copyright (c) 2006  Hewlett-Packard Development Company, L.P. 
#
# Copyright 2005 I.D.E.A.L. Technology Corporation
#
# Purpose:
# Find a specific device on an event stream and create a
# link to it
#
# chkconfig: 35 99 99
# description: hp High Performance X mouse driver
#             
#
#
# Following lines are in conformance with LSB 1.2 spec
### BEGIN INIT INFO
# Provides:            hpmouse
# Should-Start:        hpasm hprsm
# Required-Start:      hwscan
# Required-Stop:       
# Default-Start:       3 5
# Default-Stop:        0 1 2 4 6
# Description:         starts hpmouse (HP High Performance ILO2 X mouse driver)
### END INIT INFO


#set -e

# source function library
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/rc.config ]; then
. /etc/rc.config
elif [ -f /etc/rc.d/functions ]; then
. /etc/rc.d/functions
elif [ -f /etc/rc.status ]; then
. /etc/rc.status
fi

showsuccess()
{
if [ -f /etc/rc.d/init.d/functions ]; then
  echo_success
  echo ""
elif [ -f /etc/rc.status ]; then
  rc_reset
  rc_status -v
else
  echo -ne "\t\t[ SUCCESS ]\n"
fi

}
showfailure()
{
if [ -f /etc/rc.d/init.d/functions ]; then
   echo_failure
  echo ""
elif [ -f /etc/rc.status ]; then
   rc_failed
else
  echo -ne "\t\t[ FAILED ]\n"
fi
}



PATH=/sbin:/bin:/usr/sbin:/usr/bin
BASEDIR="/opt/hp/hpmouse"
EVSTATUS="/usr/bin/evstatus"
NAME="hpmouse"
DEVICE="/dev/${NAME}"
HELPER="hpmouse-helper"
SEARCH="HP Virtual Keyboard"

if [ -f /etc/X11/xorg.conf ]; then
   XCONF="/etc/X11/xorg.conf"
elif [ -f /etc/X11/XF86Config ]; then
   XCONF="/etc/X11/XF86Config"
else
   XCONF=""
   echofailure
   exit -1
fi



TMPXCONF="/tmp/xconf"

HPBEGIN="#BEGIN HP ILO X MOUSE DRIVER"
HPEND="#END HP ILO X MOUSE DRIVER"
inputdevicetext()
{
echo "${HPBEGIN}
Section \"InputDevice\"
            Identifier   \"hpmouse\"
            Driver       \"hpmouse\"
            Option       \"Device\"         \"${DEVICE}\"
            Option       \"Helper\"         \"${BASEDIR}/${HELPER}\"
            Option       \"SendCoreEvents\" \"true\"
            Option       \"MinX\"           \"0\"
            Option       \"MaxX\"           \"3000\"
            Option       \"MinY\"           \"0\"
            Option       \"MaxY\"           \"3000\"
EndSection
${HPEND}"
}

serverlayouttext()
{
echo "${HPBEGIN}
        InputDevice \"hpmouse\"    \"SendCoreEvents\"
${HPEND}" 
}



UnConfigHpIloMouse()
{
      #Is X configured
      if [ -z "`grep \"${HPBEGIN}\" ${XCONF}`" ]; then
         return 0
      fi

      #get a list of BEGINs and ENDs
      START=`grep -n "$HPBEGIN" $XCONF | cut -d ":" -f1 | tr '\n' ' '`
      END=`grep -n "$HPEND" $XCONF | cut -d ":" -f1 | tr '\n' ' '`
      SIZE=`cat $XCONF | wc -l`
      i=0 
      j=0 

      #load up the arrays
      for d in $START; 
      do
         STARTA[$i]=$d
         i=`expr $i + 1`
      done
      for d in $END; 
      do
         ENDA[$j]=$d
         j=`expr $j + 1`
      done

      #check to make sure we got something resonable
      if [ $i -ne $j -a $i -eq 2 ]; 
      then
#          echo "Unconfig FAILED"
          return -1 
      fi
#      echo "****${STARTA[0]}*****"
#      echo "****${STARTA[1]}*****"
#      echo "****${ENDA[0]}*****"
#      echo "****${ENDA[1]}*****"
#      echo "****$SIZE******"

      head -n`expr ${STARTA[0]} - 1` $XCONF >$TMPXCONF
      tail -n`expr ${SIZE} - ${ENDA[0]}` $XCONF | head -n`expr ${STARTA[1]} - ${ENDA[0]} - 1` >>$TMPXCONF
      tail -n`expr $SIZE - ${ENDA[1]}` $XCONF>>$TMPXCONF

      mv /tmp/xconf $XCONF

      #FIX redhat bug with rhgb
      if [ -f /etc/rhgb/xorg.conf ]; then
         #Only remove it is equivalant to the original xorg.conf.  If
         #if is different then we don't need to mess with it.
         if [ `diff /etc/rhgb/xorg.conf $XCONF | wc -l` -eq 0 ]; then
            rm /etc/rhgb/xorg.conf
         fi
      fi

}

ConfigHpIloMouse()
{
   #Is X already configured
   if [ -n "`grep \"${HPBEGIN}\" ${XCONF}`" ]; then
      return 0
   fi

   #figure out the beginning of the ServerLayout section
   START=`grep -n "Section \"ServerLayout\"" $XCONF | cut -d ":" -f1`
   SIZE=`cat $XCONF | wc -l`
   for i in `grep -n "EndSection" $XCONF | cut -d':' -f1`; do

      #find the first EndSection greater than START
      if [ ${i} -gt ${START} ]; then
         END=`expr ${i} - 1`
         break;
      fi
   done

   head -n$END $XCONF >$TMPXCONF 

   serverlayouttext >>$TMPXCONF
   tail -n`expr $SIZE - $END` $XCONF >>$TMPXCONF
   
   inputdevicetext >>$TMPXCONF

   #copy the original X configuration file to a safe place
   cp $XCONF /opt/hp/hpmouse/`basename $XCONF`.orig

   #FIX a bug in RedHat were rhgb doesn't work because of
   #the entries we just added.
   if [ -d /etc/rhgb ]; then
      cp $XCONF /etc/rhgb/
   fi
      
   #move the tmp X config file to the right place
   mv $TMPXCONF $XCONF

   return 0
}

findhpmouse()
{
    # Make certain we have evstatus
    if [ ! -x ${EVSTATUS} ]; then 
       echo "hpmouse: ERROR evstatus not found"
       return -1
    fi

    # Delete the link if it exists
    if [ -h ${DEVICE} ]; then
       rm ${DEVICE}
    fi
   
#    echo -n "Looking for '${SEARCH}' device: "
    for i in /dev/input/event*; do
       ${EVSTATUS} ${i} >/tmp/mousesearch 2>/dev/null
       TEST=`grep "$SEARCH" /tmp/mousesearch | wc -l`
       if [ ${TEST} -ne 0 ]; then
         TEST=`grep "Event code 272 (LEFT)" /tmp/mousesearch | wc -l`
         if [ ${TEST} -ne 0 ]; then
            ln -s ${i} ${DEVICE}
            break
         fi
       fi
    done

    if [ -h ${DEVICE} ]; then
#       echo -n "Found on `basename ${i}`"
       return 0
    else
#       echo -e "No device found\n"
       return -1
    fi

}

#
# Check to see if this is being run as root
#
USER=`/usr/bin/id -u`
if [ $USER != 0 ]
then
  echo You must be root to run this program
  exit 1
fi

#
# Go to work
#
case "$1" in
   
   deactivate|unconfigure)
      
      echo -n "Deactivating hpmouse"

      #remove the hpmouse device link
      if [ -h ${DEVICE} ]; then
        rm ${DEVICE}
      fi

      #remove the X driver and the status utility
      make -C ${BASEDIR} remove >/dev/null

      #remove entried made in the X conf file
      UnConfigHpIloMouse

      #notify ilo that hpmouse is not longer supported
      if [ -f /opt/compaq/server/etc/cmasm2d ]; then
         sh /opt/compaq/server/etc/cmasm2d restart
      fi
 
      if [ $? -eq 0 ]; then
         showsuccess 
      else
         showfailure
      fi
   ;;

   activate|configure)

      echo -n "Activating hpmouse"

      #fix wierdness for those that don't set the system time correctly
      make -C ${BASEDIR} touch >/dev/null
      
      #install the X driver and status utility
      make -C ${BASEDIR} install >/dev/null

      #configure the X config file
      ConfigHpIloMouse
 
      showsuccess 

      echo "hpmouse: You must restart X for changes to take effect"

   ;;

   start)
   ;;

   stop-helper)
      killall ${HELPER} >/dev/null 2>$1
   ;;

   #This will be called from the hpmouse-helper program
   enable-ilo-hpmouse)
      if [ -f /opt/compaq/server/etc/cmasm2d ]; then
         CMAPID=`pidof -s cmasm2d`
         #restart cmasm2d if it has not already 
         #been restarted with hpmouse option turned on
         cat /proc/$CMAPID/cmdline | grep ENABLE >/dev/null 2>&1
         if [ $? -ne 0 ]; then
            sh /opt/compaq/server/etc/cmasm2d restart
         fi
      fi
   ;;

   stop)
   ;;

   restart)
   ;;

   reload|force-reload|status)
   ;;

   test)
      inputdevicetext
   ;;

   *)
      echo "Usage: $0 {start|stop|activate|configure|deactivate|unconfigure}" >&2
      exit 1
   ;;
esac

# End
