#!/bin/sh
#(c) Barry Kauler 2005/2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#keyboard/mouse wizard for Puppy Linux
#based on code from /usr/X11R7/bin/xwin
#updated for 2.17

if [ "`which gtkdialog2`" = "" ];then
 gxmessage -center -title "エラー" "Puppyのこのディストリビューションは、アプリケーション
 「gtkdialog」が足りません。
 同様に、gtk2ライブラリーパッケージが必要です。
 このウィザードを実行するには「Gtkdialog」が必要です。

終了するには「OK」ボタンを押して下さい..."
 exit
fi


export MouseKeyboardWizard="
 <vbox>
  <hbox>
   <text>
    <label>国別のキーボードレイアウトの選択...</label>
   </text>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/mini-keyboard.xpm</input>
    <action type=\"exit\">EXIT=KEY1BUTTON</action>
   </button>
  </hbox>
  <hbox>
   <text>
    <label>古い2文字方式のキーボードレイアウトの選択</label>
   </text>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/mini-keyboard.xpm</input>
    <action type=\"exit\">EXIT=KEY2BUTTON</action>
   </button>
  </hbox>
  <hbox>
   <text>
    <label>高度なXorgキーボードの構成...</label>
   </text>
   <button>
    <input file>/usr/local/apps/XkbConfigurationManager/images/layout16.png</input>
    <action type=\"exit\">EXIT=KEY4BUTTON</action>
   </button>
  </hbox>

  <hbox>
   <text>
    <label>キーボードインターフェースの選択...</label>
   </text>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/mini-keyboard.xpm</input>
    <action type=\"exit\">EXIT=KEY3BUTTON</action>
   </button>
  </hbox>
  <hbox>
   <text>
    <label>マウスタイプの選択...</label>
   </text>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/Mouse.xpm</input>
    <action type=\"exit\">EXIT=MOUSE1BUTTON</action>
   </button>
  </hbox>

  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
"

CHECK1="false"


askoldkbdlayout ()
{
 KEYBOARD=""
 CURRKMAP="us"
 [ -f /etc/keymap ] && CURRKMAP="`cat /etc/keymap | sed -e 's/\..*//'`"
  Xdialog --title "キーボードウィザード" --cancel-label "キャンセル" --no-tags --default-item "$CURRKMAP" --menubox "キーボードレイアウトを選択して下さい:" 0 0 0  be "be (Belgium)" br "br (Brazil)" cf "cf (Central African Republic)" de "de (Germany)" dk "dk (Denmark)" es "es (Spain)" fi "fi (Finland)" fr "fr (France)" gr "gr (Greece)" hu "hu (Hungary)" it "it (Italy)" jp "jp (Japan)" no "no (Norway)" pl "pl (Poland)" ru "ru (Russia)" se "se (Sweden)" uk "uk (United Kingdom)" us "us (USA)"  2> /tmp/kbdextlayout.txt
  [ $? != 0 ] && return 1
  KEYBOARD="`cat /tmp/kbdextlayout.txt`"
 return 0
}


#thanks to Stefan Talpalaru (blueflops distro) for ideas for this func...
askkbdlayout ()
{
 KEYBOARD=""
 FONTMAP=""
 CODEPAGE=""
 CURRKMAP="us"
 [ -f /etc/keymap ] && CURRKMAP="`cat /etc/keymap | sed -e 's/\..*//'`"
 #while :; do
  Xdialog --title "キーボードウィザード" --cancel-label "キャンセル" --default-item "$CURRKMAP" --menubox  "キーボードレイアウトを選んで下さい:" 0 0 0\
	$( for F in `ls /usr/share/kbd/keymaps/i386/[^i]*/*`; do echo $F | cut -d '/' -f 8 | sed 's/.map//g'; echo " "$F | cut -d '/' -f 7 ; done )\
 2> /tmp/kbdextlayout.txt
  [ $? != 0 ] && return 1
  KEYBOARD="`cat /tmp/kbdextlayout.txt`"
  case $KEYBOARD in 
   cz|hu101|pl|ro_win|sk-qwerty|croat|cz-us-qwertz|hu|slovene)
    FONTMAP=/usr/share/kbd/consolefonts/lat2-12.psfu
    CODEPAGE=ISO-8859-2
    #break
    ;;
   *)
    FONTMAP=/usr/share/kbd/consolefonts/lat1-12.psfu
    CODEPAGE=ISO-8859-1
    #break
    ;;
  esac
 #done
 KEYBOARD=$KEYBOARD.map  #.gz
 return 0
}

#big while loop...
while :; do
 KMAP=""
 unset MouseWizard
 unset KeyboardFeaturesWizard

 #put some code in here to ask keyboard layout, keyboard ps2/usb, mouse serial/ps2/usb setup?
 #also offer the old 2-letter layout...
 eval `gtkdialog2 --program=MouseKeyboardWizard | grep "EXIT"`

 case $EXIT in

 KEY1BUTTON)
  askkbdlayout #this is extended menu, func above.
  #...this sets KEYBOARD, FONTMAP, CODEPAGE variables.
  if [ $? -eq 0 ];then
   KMAP="$KEYBOARD"
  fi
  ;;

 KEY2BUTTON)
  askoldkbdlayout
  if [ $? -eq 0 ];then
   KMAP="$KEYBOARD"
  fi
  ;;
  
 KEY4BUTTON)
  exec /usr/local/bin/xkbconfigmanager
  ;;

 KEY3BUTTON)
  KEYBRDTYPE="`cat /etc/keyboardtype`"
  CHECKKEY1="false"
  if [ "$KEYBRDTYPE" = "usb" ];then
   CHECKKEY1="true"
  fi
  export KeyboardFeaturesWizard="
 <vbox>
  <frame キーボードインターフェース>
   <vbox>
    <text>
     <label>この変更はPuppyの再起動後に有効となります！</label>
    </text>
    <checkbox>
     <label>USBインターフェース付きキーボード</label>
     <default>$CHECKKEY1</default>
     <variable>CHECKBOX002</variable>
    </checkbox>
   </vbox>
  </frame>
  <hbox>
   <button cancel></button>
   <button ok></button>
  </hbox>
 </vbox>
"
  RETPARAMS=`gtkdialog2 --program=KeyboardFeaturesWizard`
  echo "$RETPARAMS"
  if [ ! "`echo "$RETPARAMS" | grep "EXIT" | grep "OK"`" = "" ];then
   if [ "`echo "$RETPARAMS" | grep "CHECKBOX002" | grep "true"`" != "" ];then
    echo -n "usb" > /etc/keyboardtype
    modprobe usbhid 2> /dev/null #maybe then just need to restart X.
   else
    echo -n "ps/2" > /etc/keyboardtype
   fi
  fi
  ;;

 MOUSE1BUTTON)
  MOUSEDEV="`cat /etc/mousedevice`"
  #RADIOBUTTONS="$RADIO2$RADIO1$RADIO3" #serial
  DEF1="false"
  DEF2="true"
  DEF3="false"
  if [ "$MOUSEDEV" = "psaux" ];then
   #RADIOBUTTONS="$RADIO1$RADIO2$RADIO3"
   DEF1="true"
   DEF2="false"
   DEF3="false"
  fi
  if [ "$MOUSEDEV" = "input/mice" ];then
   #RADIOBUTTONS="$RADIO3$RADIO2$RADIO1"
   DEF1="false"
   DEF2="false"
   DEF3="true"
  fi

  RADIO1="     <radiobutton>
      <label>PS/2マウス（ほとんどのマウス）</label>
      <variable>RADIO001</variable>
      <default>$DEF1</default>
      <action>if true disable:COMPORT</action>
     </radiobutton>
"
  RADIO2="     <radiobutton>
      <label>シリアルマウス</label>
      <variable>RADIO002</variable>
      <default>$DEF2</default>
      <action>if true enable:COMPORT</action>
     </radiobutton>
"
  RADIO3="     <radiobutton>
      <label>USBマウス</label>
      <variable>RADIO003</variable>
      <default>$DEF3</default>
      <action>if true disable:COMPORT</action>
     </radiobutton>
"
  RADIOBUTTONS="$RADIO1$RADIO2$RADIO3"


  MOUSEBUTTONS="`cat /etc/mousebuttons`"
  CHECK1="false"
  if [ "$MOUSEBUTTONS" = "5" ];then
   CHECK1="true"
  fi

  #v1.0.7
  CHECKM="false"
  if [ ! "`cat /etc/xextraoptions | grep "\\-2button"`" = "" ];then
   CHECKM="true"
  fi
  #v2.01
  CHECKH="false"
  IDLETIME="0"
  if [ -f /etc/mousehide ];then
   IDLETIME="`cat /etc/mousehide | cut -f 1 -d ','`"
   [ ! "$IDLETIME" = "0" ] && CHECKH="true"
  fi

  #v2.14 modified by Raul Suarez (rarsa at yahoo.com)
  # if the button 1 is mapped to something different than 1 then I assume
  # it is a lefhanded mouse
  CHECKL="false"
  LEFT_H_MOUSE=` xmodmap -pp | grep -E " *[0-9] *[0-9]" | sed -e 's/ *\([0-9]*\) *\([0-9]*\)/\2/' | head -n 1`
  if [ ${LEFT_H_MOUSE} -ne 1 ] ; then
    CHECKL="true"
  fi
  #v1.0.7 modified for gtkdialog2...
  export MouseWizard="
 <wtitle>マウスウィザード</wtitle>
 <vbox>
  <frame マウスインターフェース>
   <hbox>
    <vbox>
     $RADIOBUTTONS
    </vbox>
    <vbox>
       <text><label>シリアルポート:</label></text>
       <combobox>
         <item>ttyS0</item>
         <item>ttyS1</item>
         <item>ttyS2</item>
         <item>ttyS3</item>
         <variable>COMPORT</variable>
         <visible>disabled</visible>
       </combobox>
    </vbox>
   </hbox>
  </frame>
  <frame マウスの特徴>
   <vbox>
    <checkbox>
     <label>スクロールボタンがあるマウス</label>
     <variable>CHECKBOX009</variable>
     <default>$CHECK1</default>
    </checkbox>
    <checkbox>
     <label>2つボタンで3つボタンマウスをエミュレート</label>
     <variable>CHECKBOX010</variable>
     <default>$CHECKM</default>
    </checkbox>
    <checkbox>
     <label>静止時にマウスカーソルを自動的に隠す</label>
     <variable>CHECKBOX011</variable>
     <default>$CHECKH</default>
    </checkbox>
    <checkbox>
     <label>左利き用マウス</label>
     <variable>CHECKBOX012</variable>
     <default>$CHECKL</default>
    </checkbox>
   </vbox>
  </frame>
  <hbox>
   <button cancel></button>
   <button ok></button>
  </hbox>
 </vbox>
"

  RETPARAMS=`gtkdialog2 --program=MouseWizard`
  echo "$RETPARAMS"
  if [ ! "`echo "$RETPARAMS" | grep "EXIT" | grep "OK"`" = "" ];then
   if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
    modprobe -r psmouse 2> /dev/null #v1.0.2 k2.6
    modprobe -r sermouse 2> /dev/null #v1.0.2 k2.6
   else
    #modprobe -r usbmouse 2> /dev/null #v1.0.2
    modprobe -r mousedev #v2.0.0
   fi
   if [ ! "`echo "$RETPARAMS" | grep "RADIO001" | grep "true"`" = "" ];then
    MOUSEDEV="psaux" #ps/2 mouse.
    ln -sf /dev/psaux /dev/mouse
    #k2.4 has ps/2 mouse support inbuilt.
    if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
     modprobe psmouse 2> /dev/null #v1.0.2 k2.6
    fi
   fi
   if [ ! "`echo "$RETPARAMS" | grep "RADIO002" | grep "true"`" = "" ];then
    eval `echo "$RETPARAMS" | grep "COMPORT"`
    MOUSEDEV="$COMPORT" #ttyS0,1,2,3
    ln -sf /dev/$COMPORT /dev/mouse
    #2.4 kernel has serial mouse support inbuilt.
    if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
     modprobe 8250_pci 
     modprobe sermouse 
    fi
   fi
   if [ ! "`echo "$RETPARAMS" | grep "RADIO003" | grep "true"`" = "" ];then
    MOUSEDEV="input/mice" #usb mouse
    if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
     modprobe usbhid 2> /dev/null
    else
     #modprobe usbmouse
     modprobe hid #v2.0.0
     modprobe mousedev #v2.0.0
    fi
    ln -sf /dev/input/mice /dev/mouse
   fi
   echo -n "$MOUSEDEV" > /etc/mousedevice

  #v2.14 modified by Raul Suarez (rarsa at yahoo.com)
  # sets right or lefhanded mouse and saves it for next boot.
   if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX012" | grep "true"`" = "" ];then #v2.01
     XMODMAP_CMD="pointer = 3 2 1 4 5"
   else
     XMODMAP_CMD="pointer = 1 2 3 4 5"
   fi
   xmodmap -e "${XMODMAP_CMD}"
   grep -v "^pointer" /root/.Xmodmap > /tmp/Xmodmap.tmp
   cp /tmp/Xmodmap.tmp /root/.Xmodmap
   echo "${XMODMAP_CMD}" >> /root/.Xmodmap
  #

   if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX011" | grep "true"`" = "" ];then #v2.01
    [ "$IDLETIME" = "0" ] && IDLETIME="3"
    echo -n "${IDLETIME}," > /etc/mousehide
    [ "`pidof unclutter`" = "" ] && unclutter -idle $IDLETIME &
   else
    echo -n '0,' > /etc/mousehide
    [ ! "`pidof unclutter`" = "" ] && killall unclutter
   fi

   #v1.0.7 emulate middle button
   #v2.02 Dougal has suggested changes for 3-button emulation...
   MBUTOUT="`cat /etc/xextraoptions | sed -e 's/ \-[23]button//g'`"
   if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX010" | grep "true"`" = "" ];then
    #'-2button' tells Xvesa that want 3rd-button emulation...
    echo -n "$MBUTOUT -2button" > /etc/xextraoptions
    echo -n "2" > /etc/mousebuttons
    if [ -f /etc/X11/xorg.conf ];then
     #\W\+ means one or more whitespace chars...
     cat /etc/X11/xorg.conf | tr -s '#' | sed -e 's/#Option\W\+"Emulate3Buttons"/Option      "Emulate3Buttons"/g' > /tmp/xorg.conf.tmp2
     sync
     cat /tmp/xorg.conf.tmp2 | tr -s '#' | sed -e 's/#Option\W\+"Emulate3Timeout"/Option      "Emulate3Timeout"/g' > /tmp/xorg.conf.tmp
    fi
   else
    #'-3button' tells Xvesa that mouse has 3 buttons so do not want emulation...
    #echo -n "$MBUTOUT -3button" > /etc/xextraoptions
    #umm, no, leave it off, for case of 2-button mouse without emulation...
    #no need to have a explicit '-3button' entry...
    echo -n "$MBUTOUT" > /etc/xextraoptions
    echo -n "3" > /etc/mousebuttons
    if [ -f /etc/X11/xorg.conf ];then
     cat /etc/X11/xorg.conf | sed -e 's/Option\W\+"Emulate3Buttons"/#Option      "Emulate3Buttons"/g' | tr -s '#' > /tmp/xorg.conf.tmp2
     sync
     cat /tmp/xorg.conf.tmp2 | sed -e 's/Option\W\+"Emulate3Timeout"/#Option      "Emulate3Timeout"/g' | tr -s '#' > /tmp/xorg.conf.tmp
    fi
   fi
   sync
   if [ -f /tmp/xorg.conf.tmp ];then
    cp -f /tmp/xorg.conf.tmp /etc/X11/xorg.conf
   fi

   if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX009" | grep "true"`" = "" ];then
    echo -n "5" > /etc/mousebuttons #enable scrollwheel.
    if [ -f /etc/X11/xorg.conf ];then
     cat /etc/X11/xorg.conf | tr -s '#' | sed -e 's/#Option\W\+"ZAxisMapping"\W\+"4 5"/Option      "ZAxisMapping" "4 5"/g' > /tmp/xorg.conf.tmp
    fi
   else
   # echo -n "3" > /etc/mousebuttons ...v1.0.7 moved up.
    if [ -f /etc/X11/xorg.conf ];then
     cat /etc/X11/xorg.conf | sed -e 's/Option\W\+"ZAxisMapping"\W\+"4 5"/#Option      "ZAxisMapping" "4 5"/g' | tr -s '#' > /tmp/xorg.conf.tmp
    fi
   fi
   if [ -f /etc/X11/xorg.conf ];then
    cp -f /tmp/xorg.conf.tmp /etc/X11/xorg.conf
   fi
   sync

   #v1.0.7 fix mouse protocol for Xorg...
   if [ -f /etc/X11/xorg.conf ];then

#    if [ "`cat /etc/mousedevice`" = "input/mice" ];then
#     cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"auto"\W\+#mouse0protocol/	Option	    "Protocol" "IMPS\/2" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
#    else
#     cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"IMPS\/2"\W\+#mouse0protocol/	Option	    "Protocol" "auto" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
#    fi

    case `cat /etc/mousedevice | cut -b 1-4` in
     "inpu") #"input/mice", usb
      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\w\+\W\+#mouse0protocol/	Option	    "Protocol" "IMPS\/2" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
      ;;
     "ttyS") #ttyS0,1,2,3, serial
      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\w\+\W\+#mouse0protocol/	Option	    "Protocol" "Microsoft" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
      ;;
     *)
      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\w\+\W\+#mouse0protocol/	Option	    "Protocol" "auto" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
      ;;
    esac

    sync
    cp -f /tmp/xorg.conf.tmp2 /etc/X11/xorg.conf
    sync
    rm -f /tmp/xorg.conf.tmp2
   fi

  fi
  ;;

 *)
  break
  ;;
 esac



 if [ "$KMAP" ];then
  echo -n "$KMAP" > /etc/keymap
  if [ "$FONTMAP" ];then
   echo -n "$FONTMAP" > /etc/fontmap
  fi
  if [ "$CODEPAGE" ];then
   echo -n "$CODEPAGE" > /etc/codepage
  fi
  #in rc.local0, so this will happen every bootup...
  cat /etc/keymap | grep "\\.map" > /dev/null 2>&1 #looking for .map
  if [ $? -eq 0 ];then #=0 found.
   #key layout maps are in /usr/share/kbd, using "kbd" package...
   /usr/sbin/loadkeys $KMAP
   #xwin also set these...
   #echo -e "Loading fontmap \"${FONTMAP}\"..."
   #setfont $FONTMAP  #cat /etc/fontmap | loadfont
   #...this needs work. messes up font set required by dialog program. see also rc.local0.
   #um, also have /etc/codepage, don't know how to do anything with that.
  else
   #this is the technique up to Puppy v0.8.6. KMAP is just a 2-letter code...
   /bin/gzip -dcf /lib/keymaps/$KMAP.gz | /sbin/loadkmap
  fi
  KMAPMSG="キーボードマップ「 \"$KMAP\" 」を選択しました。
注意。この情報は /etc/keymap ファイルに保存されました。
Puppyはすぐにこのレイアウトをロードして使います。

ウィザードを続けるには「OK」ボタンを押して下さい..." 
  Xdialog --title "キーボードウィザード" --msgbox "$KMAPMSG" 0 0
 fi


done #end while loop.
unset MouseKeyboardWizard
unset MouseWizard
unset KeyboardFeaturesWizard

