#!/bin/sh
#BLURB="Configure the console mouse support (GPM)."
T_PX=$1
TMP=/var/log/setup/tmp
if [ "$COLOR" = "on" -o -r $TMP/SeTcolor ]; then
 dialog --title "MOUSE CONFIGURATION" --menu "This part of the configuration \
 process will create a /dev/mouse link pointing to your default mouse device. \
 You can change the /dev/mouse link later if the mouse doesn't work, or if \
 you switch to a different type of pointing device.  We will also use the \
 information about the mouse to set the correct protocol for gpm, the Linux \
 mouse server.  Please select a mouse type \
 from the list below:" 20 72 7 \
 "ps2" "PS/2 port mouse (most desktops and laptops)" \
 "imps2" "Microsoft PS/2 Intellimouse" \
 "bare" "2 button Microsoft compatible serial mouse" \
 "ms" "3 button 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)" \
 "usb" "USB connected mouse" \
 "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> $TMP/mtype
 if [ ! $? = 0 ]; then
  rm -f $TMP/mtype
  exit
 fi
 if [ -f $TMP/mtype ]; then
  MOUSE_TYPE="`cat $TMP/mtype`"
 else
  unset MOUSE_TYPE
 fi
 rm -f $TMP/mtype
 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
  dialog --title "SELECT SERIAL PORT" --menu "Your mouse requires a \
  serial port.  Which one would you like to use?" 12 50 4 \
  "/dev/ttyS0" "(COM1: under DOS)" \
  "/dev/ttyS1" "(COM2: under DOS)" \
  "/dev/ttyS2" "(COM3: under DOS)" \
  "/dev/ttyS3" "(COM4: under DOS)" 2> $TMP/mport
  if [ ! $? = 0 ]; then
   rm -f $TMP/mport
   exit
  fi
  MDEVICE="`cat $TMP/mport`"
  SHORT_MDEVICE=`basename $MDEVICE`
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf $SHORT_MDEVICE mouse )
  # For the serial mice, the protocol is the same as the mouse type:
  MTYPE=$MOUSE_TYPE
  rm -f $TMP/mport
 elif [ "$MOUSE_TYPE" = "ps2" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf psaux mouse )
  MTYPE="ps2"
 elif [ "$MOUSE_TYPE" = "ncr" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf psaux mouse )
  MTYPE="ncr"
 elif [ "$MOUSE_TYPE" = "imps2" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf psaux mouse )
  MTYPE="imps2"
 elif [ "$MOUSE_TYPE" = "logibm" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf logibm mouse )
  MTYPE="ps2"
 elif [ "$MOUSE_TYPE" = "atibm" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf atibm mouse )
  MTYPE="ps2"
 elif [ "$MOUSE_TYPE" = "inportbm" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf inportbm mouse )
  MTYPE="bm"
 elif [ "$MOUSE_TYPE" = "js" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf js0 mouse )
  MTYPE="js"
 elif [ "$MOUSE_TYPE" = "usb" ]; then
  ( cd $T_PX/dev ; rm -f mouse ; ln -sf input/mouse0 mouse )
  MTYPE="imps2"
 fi
 # OK, we know enough now to create a sample rc.gpm:
  cat << EOF > $T_PX/etc/rc.d/rc.gpm-sample
#!/bin/sh
# Start/stop the GPM mouse server:

if [ "\$1" = "stop" ]; then
  echo "Stopping gpm..."
  gpm -k
else # assume \$1 = start:
  echo "Starting gpm..."
  gpm -m /dev/mouse -t $MTYPE
fi

# There is another way to run GPM, where it acts as a repeater outputting a
# virtual MouseSystems mouse on /dev/gpmdata.  This is useful for feeding
# gpm's data to X, especially if you've got a busmouse (in that situation X
# and gpm may not coexist without using a repeater).  To try running a GPM
# repeater for X, change the gpm command line to look like this:
# gpm -R msc -m /dev/mouse -t $MTYPE
# Then, make sure that the mouse configuration in your XF86Config file refers
# to the repeater device (/dev/gpmdata) and a MouseSystems mouse type.  If you
# edit the file directly, you'll want the lines to look like this (minus the
# comment marks '#' shown here, of course):
#Section "Pointer"
#    Protocol    "MouseSystems"
#    Device      "/dev/gpmdata"

EOF
chmod 755 $T_PX/etc/rc.d/rc.gpm-sample
# Now ask if this should be the new rc.gpm:
 dialog --title "GPM CONFIGURATION" --yesno \
"The gpm program allows you to cut and paste text on\n\
the virtual consoles using a mouse.  If you choose to\n\
run it at boot time, this line will be added to your\n\
/etc/rc.d/rc.gpm:\n\
\n\
    gpm -m /dev/mouse -t $MTYPE \n\
\n\
Running gpm with a bus mouse can cause problems with\n\
XFree86.  If XFree86 refuses to start and complains\n\
that it can not open the mouse, then comment the line\n\
out of /etc/rc.d/rc.gpm, or add '-R' to gpm and set\n\
up X to use /dev/gpmdata as your mouse device.\n\
\n\
  Shall we load the gpm program at boot time?" 18 58 
 if [ $? = 0 ]; then
   mv $T_PX/etc/rc.d/rc.gpm-sample $T_PX/etc/rc.d/rc.gpm
 fi
else # no color!
 cat << EOF
Configuration of 'gpm':

"gpm" is a program that allows you to do cut and paste on the
virtual consoles using a mouse. If you choose to have it run at boot
time, the line "gpm -m /dev/mouse -t $MTYPE " will be added to 
your /etc/rc.d/rc.gpm.

Running gpm with a bus mouse can cause problems with XFree86.
If XFree86 refuses to start and complains that it cannot open the
mouse then you might want to comment the line out of /etc/rc.d/rc.gpm.

Would you like to add "gpm -m /dev/mouse -t $MTYPE " to /etc/rc.d/rc.gpm
EOF
 echo -n "so that gpm will load at boot time ([y]es, [n]o)? "
 while [ 0 ]; do
  read SL;
  if [ "$SL" = "y" ]; then
   echo
   echo "Configuring gpm..."
   cat << EOF > $T_PX/etc/rc.d/rc.gpm
#!/bin/sh
# Start/stop/restart the GPM mouse server:

if [ "\$1" = "stop" ]; then
  echo "Stopping gpm..."
  gpm -k
elif [ "\$1" = "restart" ]; then
  echo "Stopping gpm..."
  gpm -k
  sleep 1
  echo "Starting gpm..."
  gpm -m /dev/mouse -t $MTYPE
else # assume \$1 = start:
  echo "Starting gpm..."
  gpm -m /dev/mouse -t $MTYPE
fi

# There is another way to run GPM, where it acts as a repeater outputting a
# virtual MouseSystems mouse on /dev/gpmdata.  This is useful for feeding
# gpm's data to X, especially if you've got a busmouse (in that situation X
# and gpm may not coexist without using a repeater).  To try running a GPM
# repeater for X, change the gpm command line to look like this:
# gpm -R msc -m /dev/mouse -t $MTYPE
# Then, make sure that the mouse configuration in your XF86Config file refers
# to the repeater device (/dev/gpmdata) and a MouseSystems mouse type.  If you
# edit the file directly, you'll want the lines to look like this (minus the
# comment marks '#' shown here, of course):
#Section "Pointer"
#    Protocol    "MouseSystems"
#    Device      "/dev/gpmdata"

EOF
   chmod 755 $T_PX/etc/rc.d/rc.gpm
   echo
   break;
  elif [ "$SL" = "n" ]; then
   echo
   break;
  fi
  echo
  echo "Unknown response. Answer y or n."
 done
fi
