#!/bin/bash
#
# Original Author...: Anthony Awtrey
#
# Copyright (c) 2006  Hewlett-Packard Development Company, L.P. 
#
# Copyright 2005 I.D.E.A.L. Technology Corporation
#
# Purpose:
# To provide a control to activate and deactivate the hpmouse package.  Tasks 
# include dependacy checking, copying programs to correct locations, modifying i
# xorg.conf files, and in some cases acting as an init script.
#
# 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

REDHATRELEASE=`cat /etc/redhat-release 2>/dev/null| sed 's/^.\+release \(5\).*/\1/'`

# 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"
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=""
fi



TMPXCONF="/tmp/xconf"

HPBEGIN="#BEGIN HP ILO X MOUSE DRIVER"
HPEND="#END HP ILO X MOUSE DRIVER"
inputdevicetext()
{

if [ "$REDHATRELEASE" = "5" ]; then
   HCMT="#"
fi

echo "${HPBEGIN}
Section \"InputDevice\"
            Identifier   \"hpmouse\"
            Driver       \"hpmouse\"
            Option       \"Device\"         \"${DEVICE}\"
${HCMT}            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}" 
}

starthelper()
{
   if [ "`pidof ${HELPER}`" = "" ]; then
      $BASEDIR/$HELPER >/dev/null 2>&1
   fi
}

stophelper()
{
   if [ "`pidof ${HELPER}`" != "" ]; then
      killall ${HELPER}
   fi
}

restarthelper()
{
  stophelper
  starthelper
}


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 if it is equivalant to the original xorg.conf.  If
         #it 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
}

#
# 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 init script  and stop the helper if rhel5
      if [ "$REDHATRELEASE" = "5" ]; then
         if [ -f /etc/init.d/hpmouse ]; then
            chkconfig --del hpmouse
            rm -f /etc/init.d/hpmouse
         fi

         stophelper
      fi
 
      #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)

      #make sure we have the correct hardware
      ILO2=`grep -i -e 0e11b203 -e 0e11b204 -e 103c3300 /proc/bus/pci/devices 2> /dev/null | wc -l`
      if [ $ILO2 -ne 3 ]; then
         echo -e "The hpmouse package is meant for systems with HP's ILO2 management controllers only.  Please remove this package."
         exit 1
      fi

      #make sure we have the correct kernel
      KERNEL=`uname -r|cut -b1,2,3`

      if [ `expr $KERNEL \< 2.6` -eq 1 ]; then
         echo -e "The hpmouse package will only work on 2.6 or above kernels. Please remove this package."
         exit 1
      fi

      #make sure we have an X server installed
      if [ ! -f /etc/X11/xorg.conf -a ! -f /etc/X11/XF86Config ]; then
         echo -e "This package requires either the xorg-x11 or the XFree86 package neither of which seem to be available. hpmouse will not be activated."
         exit 1
      fi

      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

      #if rhel5 we need to install the init script and start the helper
      if [ "$REDHATRELEASE" = "5" ]; then
         install -m 755 $BASEDIR/hpmouse /etc/init.d
         chkconfig --add hpmouse
         starthelper
      fi
 
      showsuccess 

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

     ;;

   start|start-helper)
      echo -n "Starting hpmouse helper process"
      if [ -n $XCONF ]; then
         starthelper
      fi
      showsuccess
   ;;

   stop|stop-helper)
      echo -n "Stopping hpmouse helper process"
      if [ -n $XCONF ]; then
              stophelper
      fi
      showsuccess
   ;;

   restart|restart-helper)
      echo -n "Restarting hpmouse helper process"
      if [ -n $XCONF ]; then
         restarthelper
      fi
      showsuccess
   ;;

   #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
   ;;

   reload|force-reload|status)
   ;;

   test)
      inputdevicetext
   ;;

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

# End
