#!/bin/sh
#Barry Kauler april 2008, for Puppy Linux, www.puppylinux.com. GPL
# hacked for nls support and slight improvements, Nathan Fisher
# Fatdog64 hack, add option for left-right mouse button reversal.

#########################################
# Get nls translations
#
MYPATH="`which $0`"
MYBINDIR="`dirname $MYPATH`"
PREFIX="`dirname $MYBINDIR`"
export SHAREDIR="$PREFIX/share/pupx"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $SHAREDIR/locale/english.po #always run to fill gaps in translation
[ -f "$SHAREDIR/locale/${LANGUAGE}.po" ] && . $SHAREDIR/locale/${LANGUAGE}.po 2> /dev/null
#
# end language section
#######################################################################

XSETQ="`xset q`"

MOUSEACCEL10=`echo "$XSETQ" | grep '  acceleration:' | grep ' threshold:' | tr -s ' ' | cut -f 3 -d ' ' | cut -f 1 -d '/'`
MOUSEACCELDIV=`echo "$XSETQ" | grep '  acceleration:' | grep ' threshold:' | tr -s ' ' | cut -f 3 -d ' ' | cut -f 2 -d '/'`
MOUSEACCEL=`busybox dc ${MOUSEACCEL10} ${MOUSEACCELDIV} div p`
MOUSETHRESHOLD=`echo "$XSETQ" | grep '  acceleration:' | grep ' threshold:' | tr -s ' ' | cut -f 5 -d ' '`
KEYBOARDVOL="`echo "$XSETQ" | grep 'key click percent: ' | tr -s ' ' | cut -f 8 -d ' '`"
KEYBOARDAUTO="`echo "$XSETQ" | grep ' auto repeat: ' | tr -s ' ' | cut -f 4 -d ' '`"
if [ "$KEYBOARDAUTO" = "on" ];then
 KEYBOARDAUTO="yes"
 KBVISIBLE="enabled"
fi
if [ "$KEYBOARDAUTO" = "off" ];then
 KEYBOARDAUTO="no"
 KBVISIBLE="disabled"
fi
KEYBOARDDELAY="`echo "$XSETQ" | grep ' auto repeat delay: ' | tr -s ' ' | cut -f 5 -d ' '`"
KEYBOARDRATE="`echo "$XSETQ" | grep ' auto repeat delay: ' | grep 'repeat rate:' | tr -s ' ' | cut -f 8 -d ' '`"
SCREENSAVERDELAY="`echo "$XSETQ" | grep ' timeout: ' | grep ' cycle: ' | tr -s ' ' | cut -f 3 -d ' '`"
SCREENSAVERFLAG='yes'
SCREENVISIBLE="enabled"
if [ "$SCREENSAVERDELAY" = "0" ];then
 SCREENSAVERFLAG='no'
 SCREENVISIBLE="disabled"
fi
SCREENSAVERCYCLE="`echo "$XSETQ" | grep ' timeout: ' | grep ' cycle: ' | tr -s ' ' | cut -f 5 -d ' '`"
SCREENSAVERBLANKING="`echo "$XSETQ" | grep ' prefer blanking: ' | tr -s ' ' | cut -f 4 -d ' '`"
SCREENSAVEREXPOSURES="`echo "$XSETQ" | grep ' allow exposures: ' | tr -s ' ' | cut -f 7 -d ' '`"
if [ "$(cat $HOME/.xset.sh | grep 'xmodmap -e')" != "" ] ;then 
REVERSEBUTTONS='true'
fi

#precautions...
[ "$MOUSEACCEL" = "" ] && MOUSEACCEL='2.0'
[ "$MOUSETHRESHOLD" = "" ] && MOUSETHRESHOLD='4'
[ "$KEYBOARDVOL" = "" ] && KEYBOARDVOL='0'
[ "$KEYBOARDAUTO" = "" ] && KEYBOARDAUTO='yes'
[ "$KEYBOARDDELAY" = "" ] && KEYBOARDDELAY='500'
[ "$KEYBOARDRATE" = "" ] && KEYBOARDRATE='6'
[ "$SCREENSAVERFLAG" = "" ] && SCREENSAVERFLAG='yes'
[ "$SCREENSAVERDELAY" = "" ] && SCREENSAVERDELAY='600'
[ "$SCREENSAVERCYCLE" = "" ] && SCREENSAVERCYCLE='600'
[ "$SCREENSAVERBLANKING" = "" ] && SCREENSAVERBLANKING='yes'
[ "$SCREENSAVEREXPOSURES" = "" ] && SCREENSAVEREXPOSURES='yes'
[ "$REVERSEBUTTONS" = "" ] && REVERSEBUTTONS='false'


#apply-now script for setting mouse params...
echo '#!/bin/sh
MOUSEACCX10=`busybox dc ${1} 10 mul p`
xset m ${MOUSEACCX10}/10 ${2}
echo -n "m ${MOUSEACCX10}/10 ${2}" >> /tmp/pupx_finalparams
[ "$3" = false ] && xmodmap -e "pointer = 1 2 3" || xmodmap -e "pointer = 3 2 1"
[ $4 ] && exit
Xdialog --title "Mouse settings" --msgbox "Mouse acceleration set to ${1}\nMouse threshold set to ${2}\n(for this session only)" 0 0' > /tmp/pupx_mouseaccel.sh
chmod 777 /tmp/pupx_mouseaccel.sh

#apply-now script for setting keyboard...
echo '#!/bin/sh
KEYBOARDVOL=$1
KEYBOARDAUTO=$2
KEYBOARDDELAY=$3
KEYBOARDRATE=$4
KEYBOARDDELAY=`echo -n $KEYBOARDDELAY | cut -f 1 -d '.'`
KEYBOARDRATE=`echo -n $KEYBOARDRATE | cut -f 1 -d '.'`
[ $KEYBOARDVOL -eq 0 ] && KEYBOARDVOL=off
if [ "$KEYBOARDAUTO" = "false" ];then
 KEYBOARDPARAMS="off"
else
 KEYBOARDPARAMS="rate $KEYBOARDDELAY $KEYBOARDRATE"
fi
xset c $KEYBOARDVOL r $KEYBOARDPARAMS
echo -n " c $KEYBOARDVOL r $KEYBOARDPARAMS"  >> /tmp/pupx_finalparams
[ $5 ] && exit
Xdialog --title "Keyboard settings" --msgbox "Keyclick volume set to ${KEYBOARDVOL}\nAuto-repeat set to ${KEYBOARDPARAMS}\n(for this session only)" 0 0' > /tmp/pupx_keyboard.sh
chmod 777 /tmp/pupx_keyboard.sh

#/tmp/pupx_screen.sh \$SCREENSAVERFLAG \$SCREENSAVERDELAY \$SCREENSAVERCYCLE \$SCREENSAVERBLANKING \$SCREENSAVEREXPOSURES
echo '#!/bin/sh
SCREENSAVERFLAG=$1
SCREENSAVERDELAY=$2
SCREENSAVERCYCLE=$3
SCREENSAVERBLANKING=$4
SCREENSAVEREXPOSURES=$5
SCREENSAVERDELAY=`echo -n $SCREENSAVERDELAY | cut -f 1 -d '.'`
SCREENSAVERCYCLE=`echo -n $SCREENSAVERCYCLE | cut -f 1 -d '.'`
if [ "$SCREENSAVERFLAG" = "false" ];then
 SCREENSAVERPARAMS="s noblank s off -dpms"
else
 [ "$SCREENSAVERBLANKING" = "false" ] && SCREENSAVERBLANKING=noblank
 [ "$SCREENSAVERBLANKING" = "true" ] && SCREENSAVERBLANKING=blank
 [ "$SCREENSAVEREXPOSURES" = "false" ] && SCREENSAVEREXPOSURES=noexpose
 [ "$SCREENSAVEREXPOSURES" = "true" ] && SCREENSAVEREXPOSURES=expose
 SCREENSAVERPARAMS="s $SCREENSAVERDELAY $SCREENSAVERCYCLE s $SCREENSAVERBLANKING s $SCREENSAVEREXPOSURES"
fi
xset $SCREENSAVERPARAMS
echo -n " $SCREENSAVERPARAMS"  >> /tmp/pupx_finalparams
[ $6 ] && exit
Xdialog --title "Screensaver settings" --msgbox "Screensaver parameters set to:\n${SCREENSAVERDELAY} ${SCREENSAVERCYCLE} ${SCREENSAVERBLANKING} ${SCREENSAVEREXPOSURES}\n(for this session only)" 0 0
' > /tmp/pupx_screen.sh
chmod 777 /tmp/pupx_screen.sh


export MAIN_DIALOG="<window title=\"$LOC_MAIN\" icon-name=\"video-display\">
<vbox>
  <notebook labels=\"$LOC_TABS\">
  
    <vbox>
     <hbox>
      <text>
       <label>$LOC_ACCEL</label>
      </text>
      <combobox>
       <variable>MOUSEACCEL</variable>
       <item>${MOUSEACCEL}</item>
       <item>7.0</item>
       <item>6.5</item>
       <item>6.0</item>
       <item>5.5</item>
       <item>5.0</item>
       <item>4.5</item>
       <item>4.0</item>
       <item>3.5</item>
       <item>3.0</item>
       <item>2.5</item>
       <item>2.0</item>
       <item>1.8</item>
       <item>1.6</item>
       <item>1.4</item>
       <item>1.2</item>
       <item>1.0</item>
      </combobox>
     </hbox>
     <text><label>$LOC_ACCEL_TEXT</label></text>
     <hbox>
      <text>
       <label>$LOC_THRESHOLD</label>
      </text>
      <combobox>
       <variable>MOUSETHRESHOLD</variable>
       <item>${MOUSETHRESHOLD}</item>
       <item>16</item>
       <item>15</item>
       <item>14</item>
       <item>13</item>
       <item>12</item>
       <item>11</item>
       <item>10</item>
       <item>9</item>
       <item>8</item>
       <item>7</item>
       <item>6</item>
       <item>5</item>
       <item>4</item>
       <item>3</item>
       <item>2</item>
       <item>1</item>
      </combobox>
     </hbox>
     <text><label>$LOC_THRESHOLD_TEXT</label></text>
     <checkbox>
       <label>$LOC_REV_BUTTONS</label>
       <variable>REVERSEBUTTONS</variable>
       <default>${REVERSEBUTTONS}</default>
       <action>if true enable: KEYBOARDDELAY</action>
       <action>if true enable: KEYBOARDRATE</action>
       <action>if false disable: KEYBOARDDELAY</action>
       <action>if false disable: KEYBOARDRATE</action>
      </checkbox>
    </vbox>

    <vbox>
     <hbox>
      <text><label>$LOC_VOLUME</label></text>
      <combobox>
       <variable>KEYBOARDVOL</variable>
       <item>${KEYBOARDVOL}</item>
       <item>100</item>
       <item>90</item>
       <item>80</item>
       <item>70</item>
       <item>60</item>
       <item>50</item>
       <item>40</item>
       <item>30</item>
       <item>20</item>
       <item>10</item>
       <item>0</item>
      </combobox>
     </hbox>
     <text><label>$LOC_KEYCLICK</label></text>
     <frame $LOC_AUTOREPEAT>
      <checkbox>
       <label>$LOC_ENABLE_AR</label>
       <variable>KEYBOARDAUTO</variable>
       <default>${KEYBOARDAUTO}</default>
       <action>if true enable: KEYBOARDDELAY</action>
       <action>if true enable: KEYBOARDRATE</action>
       <action>if false disable: KEYBOARDDELAY</action>
       <action>if false disable: KEYBOARDRATE</action>
      </checkbox>
      <hbox>
       <text><label>\"       \"</label></text>
       <text><label>$LOC_DELAY</label></text>
       <entry max_length=\"4\">
        <visible>${KBVISIBLE}</visible>
        <default>${KEYBOARDDELAY}</default>
        <variable>KEYBOARDDELAY</variable>
       </entry>
       <text><label>\"$LOC_MSEC\"</label></text>
      </hbox>
      <text><label>$LOC_DELAY_HINT</label></text>
      <hbox>
       <text><label>\"        \"</label></text>
       <text><label>$LOC_RATE</label></text>
       <entry max_length=\"2\">
        <visible>${KBVISIBLE}</visible>
        <default>${KEYBOARDRATE}</default>
        <variable>KEYBOARDRATE</variable>
       </entry>
       <text><label>\"$LOC_REPEATS\"</label></text>
      </hbox>
      <text><label>$LOC_REPEAT_HINT</label></text>
     </frame>
    </vbox>

    <vbox>
      <checkbox>
       <label>$LOC_ENABLE_SS</label>
       <variable>SCREENSAVERFLAG</variable>
       <default>${SCREENSAVERFLAG}</default>
       <action>if true enable: SCREENSAVERDELAY</action>
       <action>if true enable: SCREENSAVERCYCLE</action>
       <action>if false disable: SCREENSAVERDELAY</action>
       <action>if false disable: SCREENSAVERCYCLE</action>
      </checkbox>
      <hbox>
       <text><label>\"       \"</label></text>
       <text><label>$LOC_DELAY</label></text>
       <entry max_length=\"4\">
        <visible>${SCREENVISIBLE}</visible>
        <default>${SCREENSAVERDELAY}</default>
        <variable>SCREENSAVERDELAY</variable>
       </entry>
       <text><label>\"$LOC_SECONDS\"</label></text>
      </hbox>
      <text><label>$LOC_SECONDS_HINT</label></text>
      <hbox>
       <text><label>\"        \"</label></text>
       <text><label>$LOC_CYCLE</label></text>
       <entry max_length=\"4\">
        <visible>${SCREENVISIBLE}</visible>
        <default>${SCREENSAVERCYCLE}</default>
        <variable>SCREENSAVERCYCLE</variable>
       </entry>
       <text><label>\"$LOC_SECONDS\"</label></text>
      </hbox>
      <text><label>$LOC_INTERVAL</label></text>
      <hbox>
       <text><label>\"   \"</label></text>
       <vbox>
        <checkbox>
         <label>$LOC_BLANK</label>
         <variable>SCREENSAVERBLANKING</variable>
         <default>${SCREENSAVERBLANKING}</default>
        </checkbox>
        <checkbox>
         <label>$LOC_EXPOSE</label>
         <variable>SCREENSAVEREXPOSURES</variable>
         <default>${SCREENSAVEREXPOSURES}</default>
        </checkbox>
       </vbox>
      </hbox>
    </vbox>
    
  </notebook>
  
  <hbox>
   <button cancel>
    <action type=\"exit\">no save</action>
   </button>
   <button>
    <input file stock=\"gtk-apply\"></input>
    <label>$LOC_APPLY</label>
    <action>/tmp/pupx_mouseaccel.sh \$MOUSEACCEL \$MOUSETHRESHOLD \$REVERSEBUTTONS &</action>
    <action>/tmp/pupx_keyboard.sh \$KEYBOARDVOL \$KEYBOARDAUTO \$KEYBOARDDELAY \$KEYBOARDRATE &</action>
    <action>/tmp/pupx_screen.sh \$SCREENSAVERFLAG \$SCREENSAVERDELAY \$SCREENSAVERCYCLE \$SCREENSAVERBLANKING \$SCREENSAVEREXPOSURES &</action>
   </button>
   <button ok>
    <action type=\"exit\">save settings</action>
   </button>
  </hbox>
</vbox>
</window>
"

RETPARAMS="`gtkdialog3 --program=MAIN_DIALOG`"

eval "$RETPARAMS"

echo "$RETPARAMS"

[ "$EXIT" != "save settings" ] && exit

echo -n "" > /tmp/pupx_finalparams
/tmp/pupx_mouseaccel.sh $MOUSEACCEL $MOUSETHRESHOLD $REVERSEBUTTONS quiet
/tmp/pupx_keyboard.sh $KEYBOARDVOL $KEYBOARDAUTO $KEYBOARDDELAY $KEYBOARDRATE quiet
/tmp/pupx_screen.sh $SCREENSAVERFLAG $SCREENSAVERDELAY $SCREENSAVERCYCLE $SCREENSAVERBLANKING $SCREENSAVEREXPOSURES quiet
sync
echo -n "xset " > $HOME/.xset.sh
cat /tmp/pupx_finalparams >> $HOME/.xset.sh
echo "" >> $HOME/.xset.sh
[ "$REVERSEBUTTONS" = false ] || echo "xmodmap -e \"pointer = 3 2 1\"" >> $HOME/.xset.sh
chmod +x $HOME/.xset.sh
#...this is evaluated at bootup by $HOME/.xinitrc

# Cleanup
rm -f /tmp/pupx_mouseaccel.sh
rm -f /tmp/pupx_keyboard.sh
rm -f /tmp/pupx_screen.sh
rm -f /tmp/pupx_finalparams

###END###

