#!/bin/sh
#
# chkconfig:	2345 85 15
#
# description:	GPM adds mouse support to text-based Linux applications such \
#		the Midnight Commander. Is also allows mouse-based console \
#		cut-and-paste operations, and includes support for pop-up \
#		menus on the console.
# description(es.UTF-8): GPM añade soporte al ratón para aplicaciones Linux \
#		consola como el Midnight Commander.  También permite \
#		operaciones con el ratón de cortar y pegar e incluye soporte \
#		para menús pop-up en la consola.
# description(pt_BR.UTF-8): O GPM adiciona suporte a mouse para aplicações Linux \
#		console como o Midnight Commander. Também permite \
#		operações com o mouse de cortar-e-colar e inclui suporte \
#		a menus pop-up no console.
#
# processname:	gpm
# pidfile:	/var/run/gpmpid
# config:	/etc/sysconfig/mouse

# Source function library
. /etc/rc.d/init.d/functions

# Get service config
[ -f /etc/sysconfig/mouse ] && . /etc/sysconfig/mouse

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/gpm ]; then
		[ -z "$DEVICE" ] && nls "Please set %s in /etc/sysconfig/mouse" "DEVICE"
		[ -z "$MOUSETYPE" ] && nls "Please set %s in /etc/sysconfig/mouse" "MOUSETYPE"

		msg_starting "Gpm"
		OPTIONS=""
		[ -n "$DEVICE" ] && OPTIONS="-m $DEVICE"
		[ -n "$MOUSETYPE" ] && OPTIONS="$OPTIONS -t $MOUSETYPE"
		[ -n "$BAUD_RATE" ] && OPTIONS="$OPTIONS -b $BAUD_RATE"
		[ -n "$CLEAR_LINES" ] && OPTIONS="$OPTIONS -o $CLEAR_LINES"
		[ -n "$BUTTON_SEQ" ] && OPTIONS="$OPTIONS -B $BUTTON_SEQ"
		[ -n "$TAP_BUTTON" ] && OPTIONS="$OPTIONS -g $TAP_BUTTON"
		[ -n "$ACCEL" ] && OPTIONS="$OPTIONS -a $ACCEL"
		[ -n "$DELTA" ] && OPTIONS="$OPTIONS -d $DELTA"
		[ -n "$INTERVAL" ] && OPTIONS="$OPTIONS -i $INTERVAL"
		[ -n "$RESP" ] && OPTIONS="$OPTIONS -r $RESP"
		[ -n "$SAMPLE_RATE" ] && OPTIONS="$OPTIONS -s $SAMPLE_RATE"
		[ -n "$CHARSET" ] && OPTIONS="$OPTIONS -l \"$CHARSET\""
		[ "$BUTTON_COUNT" = "2" ] && OPTIONS="$OPTIONS -2"
		[ "$BUTTON_COUNT" = "3" ] && OPTIONS="$OPTIONS -3"
		if [ -n "$POINTER_VIS" ] && [ "$POINTER_VIS" != "no" ]; then
			OPTIONS="$OPTIONS -p"
		fi
		if [ -n "$REPEATER" ] && [ "$REPEATER" != "no" ]; then
			OPTIONS="$OPTIONS -R"
		fi
		daemon /usr/sbin/gpm $OPTIONS $GPM_OPTIONS < /dev/null
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/gpm
	else
		msg_already_running "Gpm"
	fi
}

stop() {
	if [ -f /var/lock/subsys/gpm ]; then
		msg_stopping "Gpm"
		killproc gpm
		rm -f /var/lock/subsys/gpm >/dev/null 2>&1
	else
		msg_not_running "Gpm"
	fi
}

condrestart() {
	if [ -f /var/lock/subsys/gpm ]; then
		stop
		start
	else
		msg_not_running "Gpm"
		RETVAL=$1
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	status gpm
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
