#!/bin/sh
#
# Configure Mouse event server

cfg=/etc/gpm.conf

get_cmdln()
{
  cmdln=
  if [ -n "$device" ]; then cmdln="$cmdln -m $device"; fi
  if [ -n "$type" ]; then cmdln="$cmdln -t $type"; fi
  if [ -n "$responsiveness" ]; then cmdln="$cmdln -r $responsiveness"; fi
  if [ -n "$append" ]; then cmdln="$cmdln $append"; fi
  echo $cmdln
}

type_help()
{
    echo " ms    - For Microsoft Mice (2 or 3 button).  Some old two-button
         devices send some spurious packets, which can be
         misunderstood as middle-button events. If this is happens to
         you, use the bare mouse type."
    echo " bare  - For Microsoft Mice (2 button).  Bare is the Microsoft
         protocol, without any extension; it only reports two
         buttons."
    echo " bm    - For some busmice, including those produced by Logitech."
    echo " logi  - For old serial Logitech mice"
    echo " logim - Used to turn Logitech mice into Mouse-Systems-Compatible.
         Obviously, it only works with some of the Logitech mice."
    echo " ligim - For Logitech mice behaving like MouseSystems."
    echo " mm    - for MMSeries mice."
    echo " msc   - for MouseSystems mice.  This is the standard protocol for
         three button serial mice"
    echo " mman  - The MouseMan protocol used by new Logitech mice."
    echo " ms3   - for the new serial IntelliMouse devices, the ones with three
         buttons and a protocol incompatible with older ones."
    echo " ncr   - for pointing pens found on some laptops."
    echo " pnp   - for the new mice produced by Microsoft, and maybe with the
         old ones as well."
    echo " ps2   - if the mouse's connector is round with 6 metal pins."
    echo " sun   - for Sparc mice."
    echo " wacom - for Wacom tablets."
ARCH=$(dpkg --print-installation-architecture)
    case "$ARCH" in
	m68k)
	    echo "Default for "$ARCH" is bm";;
	alpha)
	    echo "Default for "$ARCH" is ps2";;
	sparc)
	    echo "Default for "$ARCH" is sun";;
	i386|*)
	    echo "Default for "$ARCH" is ms";;
    esac
}

gpm_test()
{
  if [ "$restart" = "yes" ]; then
    gpmpid=`cat /var/run/gpmpid`
    gpmcmdln=`cat /proc/$gpmpid/cmdline|tr '\0' ' '|cut -d' ' -f2-`
    start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/gpmpid --exec /usr/sbin/gpm
  fi
  gpm `get_cmdln`

  echo "gpm `get_cmdln`"
  echo "Finish testing by typing Ctrl-D"
  cat > /dev/null
  start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/gpmpid --exec /usr/sbin/gpm
  if [ "$restart" = "yes" ]; then
    gpm $gpmcmdln
  fi
}

if [ -f $cfg ]; then
  . $cfg
fi

if [ $# -gt 0 -a "$1" = "--norestart" ]; then
  restart=no
else
  restart=yes
fi

echo "Configuring gpm (mouse event server):"

ok=no
while [ ! "$ok" = "yes" ]; do
  echo; echo "Current configuration: `get_cmdln`"

  echo -n "Do you want to change anything (Y/n)? "; read answer
  if [ "$answer" = "N" -o "$answer" = "n" ]; then
    ok=yes
  else
    # Optionally run mouse-test
    cat <<EOF

gpm has an experimental mouse test program which may help you determine
your mouse's type and which device it's attatched to.  

You *must* not run any other software which needs the mouse, e.g. X,
selection or gpm, while running this program.
EOF

    echo -n "Do you want to run gpm's mouse-test program (Y/n)? "; read answer
    if [ "$answer" = "Y" -o "$answer" = "y" ]; then
      # Catch C-c because the user might want to escape from mouse-test
      trap "echo Caught SIGINT from mouse-test\; ignoring it." 2
      echo 
      echo "**********************************************************"
      gpm-mouse-test
      echo "**********************************************************"
      trap 2
    fi

    echo "Where is your mouse [$device]? "; echo -n "> "; read answer
    if [ -n "$answer" ]; then device="$answer"; fi
    answer=
    while [ -z "$answer" ]; do
      echo "What type is your mouse (or help) [$type]? "; echo -n "> "; read answer
      if [ -n "$answer" ]; then
        case $answer in
        bare|bm|ligim|logi|mm|mman|ms|msc|ncr|ps2|sun|wacom)
	  type=$answer
	  ;;
        h|help)
	  type_help
	  answer=
	  ;;
        *)
	  echo "Sorry, "$answer" is not a recognised mouse type."
	  echo "Try help"
	  answer=
	  ;;
        esac
      else
	if [ -n "$type" ]; then
	  answer=$type
	fi
      fi
    done

    echo "Set the responsiveness (normally not needed) [$responsiveness]? ";
    echo -n "> "; read answer
    if [ -n "$answer" ]; then responsiveness="$answer"; fi

# FIXME, -l configuration

    echo "Do you want to add any additional arguments [$append]? "
    echo -n "> "; read answer
    if [ -n "$answer" ]; then 
      if [ "$answer" = "N" -o "$answer" = "n" ]; then
              append=""
      else
	      append="$answer"
      fi
    fi

    echo -n "Do you want to test this configuration (y/N)? "; read answer
    if [ "$answer" = "Y" -o "$answer" = "y" ]; then gpm_test; fi
  fi
  
done

if [ -n "$device" -a "$device" != "/dev/mouse" ]; then
  rm -f /dev/mouse
  (cd /dev; ln -sf `echo $device|cut -d/ -f3` mouse)
fi

# Any backslashes in the append string must be protected from sed.

append=$(echo $append | sed -e "s/\\\/\\\\\\\/g")

# The append string is enclosed in quotes to preserve spaces and other
# shell meta-characters; as a result we _must_ escape any quote marks
# in the append string.  Failure to do so will lead to shell meta
# characters being evaluated with undesirable consequences ('foo
# "blah" ack' -> '"foo "blah" ack"'; blah is not protected by quotes.
# However 'foo "blah" ack' -> '"foo \"blah\" ack"' is safe).  
#
# (Again, as protection against sed, the backslashes are doubled up.)

append=$(echo $append | sed -e "s/\"/\\\\\\\\\"/g")

cat $cfg \
  | sed \
    -e "s,^[ ]*device=.*,device=$device,g" \
    -e "s,^[ ]*type=.*,type=$type,g" \
    -e "s,^[ ]*responsiveness=.*,responsiveness=$responsiveness,g" \
    -e "s,^[ ]*append=.*,append=\"$append\",g" \
  > $cfg.new
mv -f $cfg.new $cfg

  if [ "$restart" = "yes" ]; then
    /etc/init.d/gpm start
  fi
