#!/bin/sh
#Barry Kauler Oct. 2011. GPL3 (/usr/share/doc/legal)
#called from delayedrun.
#111020 convert from Xdialog to pupdialog, internationalised.
#111021 lutz-ulrich: fix missing translation.
#111026 add 'nowait' param, when call from 'quicksetup'.

[ ! -f /etc/X11/xorg.conf ] && exit

export TEXTDOMAIN=mousecheck
export OUTPUT_CHARSET=UTF-8

PARAM1=""
[ $1 ] && PARAM1="$1"

TABCHAR="`echo -n -e '\t'`"
B_ok=$(gettext 'OK')
B_cancel=$(gettext 'Cancel')

choosemousefunc() { #based on code in /etc/rc.d/functions4puppy4
 ALERTMSG1=$(gettext 'Your mouse pointer has not moved, so perhaps it is not working? This dialog window may be operated by the keyboard. \ZbUP/DOWN\ZB arrows then \ZbENTER\ZB key to choose mouse-type, then \ZbTAB\ZB key to select the \ZbOK\ZB button, then ENTER -- X will immediately restart. If your mouse is working, click the \ZbCancel\ZB button.')
 ALERTMSG2=$(gettext "NOTICE: If you want, CTRL-ALT-BACKSPACE will terminate X and you can type 'xorgwizard' to run the full Xorg Wizard, to resolve any other problems (such as screen resolution) as well as fix the mouse.")
 MOUSEINTRO=$(gettext "Only very old PCs may have a serial mouse. These are recognised by the fairly large rectangular plug, technically known as a DB9 or the even larger DB25 (where the 9 or 25 refers to the number of pins in the plug).")
 DETECTEDMOUSEDEV="`cat /etc/mousedevice`"
 case $DETECTEDMOUSEDEV in
  ttyS*)
   MOUSEMENU1a=$(gettext 'serial'); MOUSEMENU1b=$(gettext '(mouse in very old computers)')
   MOUSEMENU2a=$(gettext 'usb/ps-2'); MOUSEMENU2b=$(gettext '(mouse or touchpad in all modern computers)')
  ;;
  *)
   MOUSEMENU1a=$(gettext 'usb/ps-2'); MOUSEMENU1b=$(gettext '(mouse or touchpad in all modern computers)')
   MOUSEMENU2a=$(gettext 'serial'); MOUSEMENU2b=$(gettext '(mouse in very old computers)')
  ;;
 esac
 H_mc=$(gettext 'MouseCheck: choose interface')
 H_mt=$(gettext 'Please choose mouse type...') #111021
 pupdialog --background '#FFFF00' --ok-label "${B_ok}" --cancel-label "${B_cancel}" --colors --backtitle "${H_mc}" --menu "${ALERTMSG1}\n\n${ALERTMSG2}\n\n${MOUSEINTRO}\n\n${H_mt}" 0 100 3 "$MOUSEMENU1a" "${TABCHAR}${MOUSEMENU1b}" "$MOUSEMENU2a" "${TABCHAR}${MOUSEMENU2b}" 2> /tmp/testmsx
 [ $? -ne 0 ] && return 1
 [ ! -s /tmp/testmsx ] && return 1
 CHOSENMOUSETYPE="`cat /tmp/testmsx`"
 case $CHOSENMOUSETYPE in
 serial)
  H_s=$(gettext 'MouseCheck: choose serial port')
  M_s=$(gettext 'Choose serial port mouse plugged into. \ZbUP/DOWN\ZB arrows then \ZbENTER\ZB key to choose interface, then \ZbTAB\ZB key to select the \ZbOK\ZB button, then ENTER')
  S_1=$(gettext '(most likely)')
  pupdialog --background '#FFFF00' --ok-label "${B_ok}" --cancel-label "${B_cancel}" --colors --backtitle "${H_s}" --menu "${M_s}" 0 100 5 ttyS0 "COM1 ${S_1}" ttyS1 COM2 ttyS2 COM3 ttyS3 COM4 2> /tmp/testmsx
  [ $? -ne 0 ] && return 1
  [ ! -s /tmp/testmsx ] && return 1
  MOUSEDEV="`cat /tmp/testmsx`"
  modprobe sermouse
  ln -snf $MOUSEDEV /dev/mouse
  echo -n "$MOUSEDEV" > /etc/mousedevice
  sed -i -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\w\+\)\|\(\w\+\)\)\W\+#mouse0protocol/	Option	    "Protocol" "Microsoft" #mouse0protocol/' /etc/X11/xorg.conf
 ;;
 *)
  echo -n "input/mice" > /etc/mousedevice
  ln -snf input/mice /dev/mouse
  sed -i -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\w\+\)\|\(\w\+\)\)\W\+#mouse0protocol/	Option	    "Protocol" "IMPS\/2" #mouse0protocol/' /etc/X11/xorg.conf
 ;;
 esac
 return 0
} #choosemousefunc()

if [ "$PARAM1" != "nowait" ];then #111026

 CURPOS1="`getcurpos`"
 sleep 5
 CURPOS2="`getcurpos`"
 [ "$CURPOS1" != "$CURPOS2" ] && exit 0
 sleep 6
 CURPOS2="`getcurpos`"
 [ "$CURPOS1" != "$CURPOS2" ] && exit 0

 S_1=$(gettext 'Your mouse has not moved, does it work? Please move it now. If your mouse does not work, just wait, in 10 seconds a window (usable by the keyboard) will pop up and enable you to fix the mouse.')
 MOUSEICON='/usr/local/lib/X11/mini-icons/mini-mouse.xpm'
 [ -f /usr/share/hardinfo/pixmaps/mouse.png ] && MOUSEICON='/usr/share/hardinfo/pixmaps/mouse.png'
 yaf-splash -icon $MOUSEICON -bg pink -fg black -placement center -close never -fontsize x-large -text "${S_1}" &
 YPID=$!

 mCNT=0
 while [ $mCNT -lt 11 ];do
  sleep 2
  CURPOS2="`getcurpos`"
  if [ "$CURPOS1" != "$CURPOS2" ];then
   kill $YPID
   exit 0
  fi
  mCNT=`expr $mCNT + 2`
 done
 kill $YPID

 #...failure
fi

choosemousefunc
if [ $? -eq 0 ];then
 echo -n '' > /tmp/delayedrun_quicksetup_count #111026 hack for 'delayedrun', so quicksetup will re-run.
 rm -rf /tmp/.X0-lock
 sync
 exec restartwm
fi

###END###
