#!/bin/bash
# 
# lmouse - written by Chuck Mead <csm@moongroup.com>
# License - GPL
#
# This utility aims to assist users and system administrators
# configure their mouse properly to work with gpm on Lunar Linux.
# http://lunar-linux.org
#

# exit if executor is not root!

if [ $LOGNAME != root ]; then
  echo "User must have root privileges to run this program"
  exit 1
fi

clear

cat << EOF

LMOUSE - Lunar Mouse Configuration Tool

**** WARNING **** 

This tool will overwrite your mouse config file:

/etc/config.d/mouse

EOF

echo -e "Are you sure you want to do this? [y] \c "

read JUNK
case $JUNK in
  [nN])
    echo "Exiting..."
    exit 1
    ;;
esac

while true; do  # start main question loop

  echo
  clear

  cat << EOF

You will now be asked what device your mouse is connected to.  Typically
you would use /dev/mouse and symlink the real port to /dev/mouse.
This is the default and is recommended.

  EOF

  echo -e " Enter device name [/dev/mouse]:\c "
  read DEVICE

  if [ -z "$DEVICE" ]; then
    DEVICE=/dev/mouse
  fi

  clear
  cat << EOF

You will now be asked what kind of mouse you have.  Usually this
question may be answered by flipping the mouse over and reading the
model information from the bottom of the mouse.  If you leave this one
blank the system will try to detect your mousetype.  Here is a list of
the more common possibilities:

  EOF

  echo " Press enter to continue"
  read JUNK
  clear
  cat << EOF
  name     description                                                 synonym

  mman     The "MouseMan" and similar devices (3/4 bytes per packet).  Synonyms: Mouseman
  ms       The original ms protocol, with a middle-button extension.   Synonyms: 
  bare     Unadorned ms protocol. Needed with some 2-buttons mice.     Synonyms: Microsoft
  bm       Micro$oft busmice and compatible devices.                   Synonyms: BusMouse
  exps2    IntelliMouse Explorer (ps2) - 3 buttons, wheel unused       Synonyms: ExplorerPS/2
  js       Joystick mouse emulation                                    Synonyms: Joystick
  gunze    Gunze touch-screens (only button-1 events, by now)          Synonyms: 
* imps2    Microsoft Intellimouse (ps2)-autodetect 2/3 buttons         Synonyms: 
  logi     Used in some Logitech devices (only serial).                Synonyms: Logitech
  logim    Turn logitech into Mouse-Systems-Compatible.                Synonyms: 
  mm       MM series. Probably an old protocol...                      Synonyms: MMSeries
  ms3      Microsoft Intellimouse (serial) - 3 buttons, wheel unused   Synonyms: 
  ms+      Like 'ms', but allows dragging with the middle button.      Synonyms: 
  ms+lr    'ms+', but you can reset m by pressing lr (see man page).   Synonyms: 
* msc      Mouse-Systems-Compatible (5bytes). Most 3-button mice.      Synonyms: MouseSystems
  mtouch   MicroTouch touch-screens (only button-1 events, by now)     Synonyms: 
  netmouse Genius NetMouse (ps2) - 2 buttons and 2 buttons 'up'/'down' Synonyms: 
  pnp      Plug and pray. New mice may not run with '-t ms'.           Synonyms: 
* ps2      Busmice of the ps/2 series. Most busmice, actually.         Synonyms: PS/2
  syn      The "Synaptics" serial TouchPad.                            Synonyms: synaptics
  synps2   The "Synaptics" PS/2 TouchPad                               Synonyms: synaptics_ps2

  EOF

  echo -e " What type of mouse do you have? [pnp]\c"
  read MOUSETYPE

  if [ -z "$MOUSETYPE" ]; then
    MOUSETYPE=pnp
  fi
  clear

  cat << EOF

It is possible to add additional options to your mouse configuration.

In most cases this is not necessary so unless you know you need it you
should leave this blank.

  EOF

  echo -e " What additional options do you require? []\c"
  read OPTIONS

  clear

  echo " Based upon the information you provided your gpm"
  echo " command string will look like this:"
  echo ""
  echo "            gpm $OPTIONS -t $MOUSETYPE -m $DEVICE"
  echo ""
  echo -e " Is this correct? [n] \c "

  read ANSWER

  case $ANSWER in
    [yY])
      if [ -e "/etc/config.d/mouse" ]; then
        mv /etc/config.d/mouse /etc/config.d/mouse.old
        echo " /etc/config.d/mouse has been saved as /etc/config.d/mouse.old"
      fi
      cat > /etc/config.d/mouse << EOF

 # /etc/config.d/mouse
 # this is the config file for the gpm mouse.
 # it is sourced by /etc/init.d/gpm initscript at startup.
 #
 # Copyright, Lunar-Penguin Team, 2002, License - GPL

 OPTIONS=$OPTIONS

 MOUSETYPE=$MOUSETYPE

 DEVICE=$DEVICE

      EOF
      /etc/init.d/gpm restart

      exit 0
      ;;
    *)
      echo " Restarting.... "
      ;;
  esac
done;
