#!/bin/sh
# @vasm : MOUSE
# @level: root
# @description: setup mouse (/dev/mouse)
#
# Based on Slackware 10 /var/log/setup/setup.mouse
# Heavily modified for VLocity Linux
# - autodetect
# - manual detect
# - select WHELL/5/3/2
# - set GPM and symlink /dev/mouse 
# - add entries to /etc/sysconfig/vlocity (to be used by vxconf)
#
# This is the first attempt to combine auto/manual configurator
# All hardware configurator should be written like this
# 
# (c) Eko M. Budi, 2004
# (c) VLocity Linux, 2004
#
# Released under GNU GPL

vdir=`dirname $0`
. $vdir/vasm-functions

VLocity_CONFIG=/etc/sysconfig/vlocity
GPM=/usr/sbin/gpm

###################################################
#
# Get variables that should have been detected by vlsetup
get_mouse()
{
    # local variables
    MOUSE_TYPE=""
    MOUSE0_DEVICE=""
    MOUSE0_FULLNAME="generic mouse"
    MOUSETYPE0="ps2"
    XMOUSETYPE0="auto"
    XMOUSE_ZAXISMAPPING=""
    XMOUSE_BUTTONS=""
    XMOUSE_SENDCOREEVENTS=""
    XMOUSE_E3BUTTONS="false"

    if [ -f /etc/sysconfig/vlocity ]; then
	. /etc/sysconfig/vlocity
    fi

}

# replace a config with new value
sed_config()
{
    if grep -qe "^ *$1=" $3; then
	sed -i "s!^ *$1=.*!$1=\"$2\"!" $3 
    else
	echo "$1=\"$2\"" >> $3
    fi
}

#    Option     "Protocol"      "auto" ## MOUSE_PROTOCOL
#    Option "Emulate3Buttons"      ## MOUSE_E3BUTTONS
#    Option "ZAxisMapping" "4 5"     ## MOUSE_ZAXISMAPPING
#    Option "Buttons"      "5"       ## MOUSE_BUTTONS
#    Option "SendCoreEvents" "true"  ## MOUSE_SENDCOREEVENTS


set_xmouse() {
    config_file=$1
   
   ## MOUSE_PROTOCOL must be always set, default=auto
   sed -i \
   "s!.*## MOUSE_PROTOCOL!    Option  \"Protocol\"  \"$XMOUSETYPE0\"   ## MOUSE_PROTOCOL!" \
   $config_file

   if [ "$XMOUSE_ZAXISMAPPING" ] && [ "$XMOUSE_BUTTONS" ]; then
      sed -i \
      "s!.*## MOUSE_ZAXISMAPPING!    Option \"ZAxisMapping\" \"$XMOUSE_ZAXISMAPPING\"    ## MOUSE_ZAXISMAPPING!" \
      $config_file
      sed -i \
      "s!.*## MOUSE_BUTTONS!    Option \"Buttons\" \"$XMOUSE_BUTTONS\"    ## MOUSE_BUTTONS!" \
      $config_file
   else
      sed -i \
      "s!.*## MOUSE_ZAXISMAPPING!#    Option \"ZAxisMapping\" \"$XMOUSE_ZAXISMAPPING\"    ## MOUSE_ZAXISMAPPING!" \
      $config_file
      sed -i \
      "s!.*## MOUSE_BUTTONS!#    Option \"Buttons\" \"$XMOUSE_BUTTONS\"    ## MOUSE_BUTTONS!" \
      $config_file
   fi

   if [ "$XMOUSE_E3BUTTONS" = "true" ]; then
   sed -i \
   's!.*## MOUSE_E3BUTTONS!    Option "Emulate3Buttons"      ## MOUSE_E3BUTTONS!' \
   $config_file
   else
   sed -i \
   's!.*## MOUSE_E3BUTTONS!#    Option "Emulate3Buttons"      ## MOUSE_E3BUTTONS!' \
   $config_file
   fi

   if [ "$XMOUSE_SENDCOREEVENTS" = "true" ]; then
   sed -i \
   's!.*## MOUSE_SENDCOREEVENTS!    Option "SendCoreEvents"      ## MOUSE_SENDCOREEVENTS!' \
   $config_file
   else
   sed -i \
   's!.*## MOUSE_SENDCOREEVENTS!#    Option "SendCoreEvents"      ## MOUSE_SENDCOREEVENTS!' \
   $config_file
   fi
   
}

set_mouse()
{
    # Set the GPM
    if [ "$MOUSE_TYPE" ]; then
      sed_config "OPT" "-t $MOUSE_TYPE" /etc/rc.d/init.d/gpm
    fi
    
    # Set /etc/sysconfig/vlocity
    if [ "$MOUSETYPE0" ]; then
	sed_config "MOUSETYPE0" "$MOUSETYPE0" /etc/sysconfig/vlocity
    fi
    # note, XMOUSE_PROTOCOL is stored as XMOUSETYPE0 in vlocity config
    if [ "$XMOUSETYPE0" ]; then
	sed_config "XMOUSETYPE0" "$XMOUSETYPE0" /etc/sysconfig/vlocity
    fi

    sed_config "XMOUSE_SENDCOREEVENTS" "$XMOUSE_SENDCOREEVENTS" /etc/sysconfig/vlocity
    sed_config "XMOUSE_ZAXISMAPPING" "$XMOUSE_ZAXISMAPPING" /etc/sysconfig/vlocity
    sed_config "XMOUSE_BUTTONS" "$XMOUSE_BUTTONS" /etc/sysconfig/vlocity
    sed_config "XMOUSE_E3BUTTONS" "$XMOUSE_E3BUTTONS" /etc/sysconfig/vlocity 
    
    [ -f /etc/X11/xorg.conf-vesa ] && set_xmouse /etc/X11/xorg.conf-vesa
    [ -f /etc/X11/xorg.conf ] && set_xmouse /etc/X11/xorg.conf
}

###############################################
# Mouse detection
auto_mouse() {
    if [ "$MOUSE0_DEVICE" ]; then
	infobox "Setting up $MOUSE0_FULLNAME" "MOUSE"
	( cd /dev ; rm -f mouse ; ln -sf $MOUSE0_DEVICE mouse )
	if [ -d /lib/udev/devices ]; then
	  ( cd /lib/udev/devices ; rm -f mouse ; ln -sf $MOUSE0_DEVICE mouse )
        fi
	sleep 3
        return 0
    fi
    
    # If the mouse is an USB, we can autodetect it:
    if [ -r /proc/bus/usb/devices ]; then
	if cat /proc/bus/usb/devices | grep usb_mouse 1> /dev/null 2> /dev/null ; then
	    infobox "Setting up USB mouse" "MOUSE"
	    ( cd /dev ; rm -f mouse ; ln -sf input/mice mouse )
	    if [ -d /lib/udev/devices ]; then
	      ( cd /lib/udev/devices ; rm -f mouse ; ln -sf input/mice mouse )
	    fi
	    sleep 3
	    return 0
	fi
    fi

    infobox "Sorry, cannot detect the mouse" "MOUSE ERROR"
    sleep 3
    return 1
}


menuA1() {
    
  $DCMD --backtitle "$BACKTITLE" --title "SELECT SERIAL PORT" --menu "\n
Your mouse requires a serial port.\n
Which one would you like to use?" 12 50 4 \
  "/dev/ttyS0" "First   (COM1: under DOS)" \
  "/dev/ttyS1" "Second  (COM2: under DOS)" \
  "/dev/ttyS2" "Third (COM3: under DOS)" \
  "/dev/ttyS3" "Fourth  (COM4: under DOS)" 2> $freply || return $?
 
 MDEVICE="`cat $TMP/mport`"
  SHORT_MDEVICE=`basename $MDEVICE`
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf $SHORT_MDEVICE mouse )
  if [ -d $T_PX/lib/udev/devices ]; then
    ( cd $T_PX/lib/udev/devices ; rm -f mouse ; ln -sf $SHORT_MDEVICE mouse )
  fi
  
}

menuA() {

while [ 0 ]; do

$DCMD --title "MOUSE CONFIGURATION" --menu "\n
This configurator allows you to select a mouse device as the default mouse.
It will also use the information about the mouse to set the correct protocol
for X-Window and the gpm mouse daemon.\n
Please select a mouse type from the list below:" 20 76 8 \
 "AUTO" "Try to autodetect" \
 "usb" "USB connected mouse" \
 "imps2" "Improved PS/2 (usually with wheel)" \
 "ps2" "PS/2 port mouse (most desktops and laptops)" \
 "ms" "Microsoft compatible serial mouse" \
 "mman" "Logitech serial MouseMan and similar devices" \
 "msc" "MouseSystems serial (most 3 button serial mice)" \
 "pnp" "Plug and Play (serial mice that do not work with ms)" \
 "ms3" "Microsoft serial Intellimouse" \
 "netmouse" "Genius Netmouse on PS/2 port" \
 "logi" "Some serial Logitech devices" \
 "logim" "Make serial Logitech behave like msc" \
 "atibm" "ATI XL busmouse (mouse card)" \
 "inportbm" "Microsoft busmouse (mouse card)" \
 "logibm" "Logitech busmouse (mouse card)" \
 "ncr" "A pointing pen (NCR3125) on some laptops" \
 "twid" "Twiddler keyboard, by HandyKey Corp" \
 "genitizer" "Genitizer tablet (relative mode)" \
 "js" "Use a joystick as a mouse" \
 "wacom" "Wacom serial graphics tablet" 2> $freply || return $?
 
MOUSE_TYPE="`cat $freply`"
if [ "$MOUSE_TYPE" = "AUTO" ]; then
  auto_mouse && return 0
  continue
fi

MOUSETYPE0=$MOUSE_TYPE
if [ "$MOUSE_TYPE" = "bare" -o "$MOUSE_TYPE" = "ms" \
 -o "$MOUSE_TYPE" = "mman" -o "$MOUSE_TYPE" = "msc" \
 -o "$MOUSE_TYPE" = "genitizer" \
 -o "$MOUSE_TYPE" = "pnp" -o "$MOUSE_TYPE" = "ms3" \
 -o "$MOUSE_TYPE" = "logi" -o "$MOUSE_TYPE" = "logim" \
 -o "$MOUSE_TYPE" = "wacom" -o "$MOUSE_TYPE" = "twid" ]; then
    menuA1
   # For the serial mice, the protocol is the same as the mouse type:
  XMOUSETYPE0=$MOUSE_TYPE
elif [ "$MOUSE_TYPE" = "ps2" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf psaux mouse )
  XMOUSETYPE0="PS2"
elif [ "$MOUSE_TYPE" = "imps2" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf psaux mouse )
  XMOUSETYPE0="IMPS/2"
elif [ "$MOUSE_TYPE" = "usb" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf input/mice mouse )
  XMOUSETYPE0="IMPS/2"
elif [ "$MOUSE_TYPE" = "logibm" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf logibm mouse )
  XMOUSETYPE0="PS2"
elif [ "$MOUSE_TYPE" = "atibm" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf atibm mouse )
  XMOUSETYPE0="PS2"
elif [ "$MOUSE_TYPE" = "ncr" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf psaux mouse )
  XMOUSETYPE0="ncr"
elif [ "$MOUSE_TYPE" = "inportbm" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf inportbm mouse )
  XMOUSETYPE0="bm"
elif [ "$MOUSE_TYPE" = "js" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf js0 mouse )
  XMOUSETYPE0="js"
fi
return 0

done
 
}

##################################################
# Ask Mouse
function menuB() {

WHEEL=""
DIMENSION="12 60 4"
TITLE="MOUSE MODEL"
TEXT="\n
How many button does your mouse have ?"

$WCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"WHEEL" "Mouse with 2 buttons and a wheel" \
"5"     "Mouse with 5 buttons, including wheel/trackball" \
"3"     "Mouse with 3 buttons, no wheel" \
"2"     "Mouse with 2 buttons, no wheel" \
2> $freply || return $?

case $(cat $freply) in
   WHEEL)
    MOUSETYPE0="imps2"
    XMOUSETYPE0="IMPS/2"
    XMOUSE_ZAXISMAPPING="4 5"
    XMOUSE_BUTTONS="5"
    XMOUSE_SENDCOREEVENTS="true"
    XMOUSE_E3BUTTONS="false"
    ;;
   5)
    MOUSETYPE0="imps2"
    XMOUSETYPE0="IMPS/2"
    XMOUSE_ZAXISMAPPING="6 7"
    XMOUSE_BUTTONS="7"
    XMOUSE_SENDCOREEVENTS="true"
    XMOUSE_E3BUTTONS="false"
    ;;
   3)
    XMOUSE_ZAXISMAPPING=""
    XMOUSE_BUTTONS=""
    XMOUSE_SENDCOREEVENTS="false"
    XMOUSE_E3BUTTONS="false"
    ;;
   2)
    XMOUSE_ZAXISMAPPING=""
    XMOUSE_BUTTONS=""
    XMOUSE_SENDCOREEVENTS="false"
    XMOUSE_E3BUTTONS="true"
    ;;
esac

set_mouse

clean_exit 0 
}

######################################################
# Main program
get_mouse
case $1 in
    --auto)
	if auto_mouse; then
	    wizard menuB
	else
	    wizard menuA menuB
	fi
	;;
    *)
	wizard menuA menuB
	;;
esac

clean_exit 1
