#!/bin/sh
#BK (c) 2003,2004,2005 www.goosee.com/puppy
echo "This script will run X windows for you..."

#0.9.9 enables to start a specific w.m. from commandline...
if [ $1 ];then
 echo -n "$1" > /etc/windowmanager
 #note, /root/.xinitrc uses this file.
fi

#0.9.9 support for SVGA, Xfbdev X servers...
HAVEKDRIVE="`readlink /usr/X11R6/bin/X`"
if [ "`echo "$HAVEKDRIVE" | grep --extended-regexp "Xvesa|Xfbdev"`" = "" ];then
 #have a conventional X server, that uses /etc/X11/XF86Config.
 HAVEKDRIVE="no"
fi

#thanks to Stefan Talpalaru (blueflops distro) for ideas for this func...
askkbdlayout ()
{
 KEYBOARD=""
 FONTMAP=""
 CODEPAGE=""
 while :; do
  dialog --cancel-label "BACK" --menu "Select the keyboard map:" 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
}

if [ "$HAVEKDRIVE" = "Xvesa" ];then #0.9.9
 #want to start 1st time with a safe video mode...
 if [ ! -e /etc/videomode ];then
  if [ ! -e /tmp/videomode ];then
   echo -n "0x0111 640x480x16" > /tmp/videomode
   rm -f /tmp/Xflag #precaution.
  fi
 fi
 #...if /tmp/videomode exists, video-wizard is started in ~/.xinitrc
 #...note, if /etc/videomode exists instead, normal X startup.
fi

#ask for mouse type below, but we will also ask for keyboard type here...
if [ ! -e /etc/mousedevice ];then
 if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6
  echo
  #echo "Puppy needs to know if you have a USB keyboard..."
  #modprobe usbhid 2> /dev/null
  #sleep 1
  #cat /proc/bus/usb/devices | grep -i "keyboard"
  #if [ $? -eq 0 ];then #=0 found.
  # echo "Okay, it seems that you do have a USB keyboard!"
  # echo -n "usb" > /etc/keyboardtype
  #else
  # echo "It seems not. Removing the USB keyboard driver."
  # modprobe -r usbhid
  # echo -n "ps/2" > /etc/keyboardtype
  #fi
  cat /proc/bus/usb/devices | grep -i "keyboard"
  if [ $? -eq 0 ];then #=0 found.
   echo "It seems that you have a USB keyboard!"
   modprobe usbhid 2> /dev/null
   echo -n "usb" > /etc/keyboardtype
  else
   echo "It seems that you have a ps/2 keyboard"
   echo -n "ps/2" > /etc/keyboardtype
  fi
 else #k2.4
  if [ "`lsmod | grep "usb\-storage"`" = "" ];then
   echo " "
   echo "Puppy needs to know if you have a USB keyboard..."
   modprobe usbkbd
   modprobe usbmouse #this loads hid and everything else in correct order.
   #(...note, k2.6, this is HIDBoot protocol, only dependency is usbcore.ko)
   sleep 1
   cat /proc/bus/usb/devices | grep -i "keyboard"
   if [ $? -eq 0 ];then #=0 found.
    echo "Okay, it seems that you do have a USB keyboard!"
    echo -n "usb" > /etc/keyboardtype
   else
    echo "It seems not. Removing the USB keyboard driver."
    modprobe -r usbmouse
    modprobe -r usbkbd
    echo -n "ps/2" > /etc/keyboardtype
   fi
  else
   echo -n "ps/2" > /etc/keyboardtype
   echo
   echo "There seems to be a conflict if both booting from usb and have usb keyboard."
   echo "For this case, the current version of Puppy will require you to have a ps/2 keyboard."
  fi
 fi
fi

if [ ! -e /etc/keymap ];then
 while :; do
#  echo " "
#  echo "When Puppy booted for the very first time, the keyboard layout"
#  echo "was set to \"us\" (USA). You now have the opportunity to change"
#  echo "to a different country, however note that THE CHANGE WILL ONLY"
#  echo "TAKE EFFECT WHEN YOU REBOOT PUPPY."
#  echo
#  echo "These are your choices:"
#  echo "be br cf de dk es fi fr gr hu it jp no pl ru se uk us"
#  echo
#  echo "The choices shown here are very limited. If you need something else,"
#  echo "such as Dvorak layout, or you are unable to type the required letters,"
#  echo "just hit the ENTER key only. This will bring up an extended menu that"
#  echo "you can navigate with the arrow keys..."
#  echo -n "Enter 2-digit code for your country (or just ENTER): "
#  read KMAP
#  if [ "$KMAP" = "" ];then
   askkbdlayout #this is extended menu, func above.
   #...this sets KEYBOARD, FONTMAP, CODEPAGE variables.
   if [ $? -eq 0 ];then
    KMAP="$KEYBOARD"
    break
   else #cancel, error exit.
    KMAP="us"
   fi
#  else
#   break
#  fi
 done #end while loop.
 echo -n "$KMAP" > /etc/keymap
 echo "You have chosen \"$KMAP\" keyboard map."
 echo "Note, this information is saved in file /etc/keymap" 
 if [ "$FONTMAP" ];then
  echo -n "$FONTMAP" > /etc/fontmap
 fi
 if [ "$CODEPAGE" ];then
  echo -n "$CODEPAGE" > /etc/codepage
 fi
 #0.9.6 have copied this code from 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...
  echo -e "Loading \"$KMAP.gz\" keyboard map... "
  KMAP="`echo -n "$KMAP" | cut -f 1 -d '.'`" #v1.0.5 get rid of .map
  /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...
  echo -e "Loading \"$KMAP\" keyboard map... "
  /bin/gzip -dcf /lib/keymaps/$KMAP.gz | /sbin/loadkmap
 fi
fi

rm -f /dev/mouse
if [ ! -e /etc/mousedevice ];then
 echo " "
 echo -n "Enter mouse type serial, ps/2, usb [ps/2]: "
 #echo -n "Enter mouse type serial, ps/2 [ps/2]: "
 read n1
 if [ "$n1" = "serial" ];then
  echo -n "Enter mouse port, ttyS0 ttyS1, ttyS2, ttyS3: "
  read n1
  ln -sf /dev/$n1 /dev/mouse
  MOUSEDEV="$n1"
  #2.4 kernel has serial mouse support inbuilt, hence the /dev/null's here...
  modprobe 8250_pci 2> /dev/null #v1.0.2
  modprobe sermouse 2> /dev/null #v1.0.2
 else
  if [ "$n1" = "usb" ];then
    ln -sf /dev/input/mice /dev/mouse
    MOUSEDEV="input/mice"
   if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
    modprobe usbhid 2> /dev/null #may already be loaded if usb keyboard.
   else #2.4...
    #modprobe -v input    #k2.4
    #modprobe -v hid      #k2.4
    #modprobe -v mousedev #k2.4
    modprobe usbmouse #uses HIDBP which is simpler than HID,
    #...usbmouse.o needs CONFIG_USB_MOUSE=m in kernel config,
    #...this is an alternative to the above.
    #...note, may already be loaded above, for usb keyboard test.
   fi
  else
    ln -sf /dev/psaux /dev/mouse
    MOUSEDEV="psaux"
   #k2.4 has ps/2 mouse support inbuilt, hence the /dev/null here...
    modprobe psmouse #v1.0.2
  fi
 fi
 echo -n "$MOUSEDEV" > /etc/mousedevice
 echo "You have selected $MOUSEDEV as your mouse device."
 echo "This information is saved in file /etc/mousedevice."

 echo " "
 echo -n "Do you have a wheel/scroll type of mouse? y/n [n]: "
 read n1
 if [ "$n1" = "y" ];then
  echo -n "5" > /etc/mousebuttons
  echo -e "The file /etc/mousebuttons has been created, with content \"5\"."
 else
  echo -n "3" > /etc/mousebuttons
  echo -e "...you chose \"no\"."
  echo -e "The file /etc/mousebuttons has been created, with content \"3\"."
 fi

else
 #modules already loaded by /etc/rc.d/rc.local0.
 MOUSEDEV="`cat /etc/mousedevice`"
 ln -sf /dev/$MOUSEDEV /dev/mouse
fi

if [ "$HAVEKDRIVE" = "Xvesa" ];then #0.9.9
 #/tmp/videomode exists if starting X with new video test mode...
 if [ -e /tmp/videomode ];then
  cat /tmp/videomode | grep "0x0111"
  if [ $? -eq 0 ];then #=0 if found.
   if [ -e /tmp/Xflag ];then
    #we have just attempted 640x480x16 and aborted with ctrl-alt-backspace
    #so have failed.
    echo "An attempt has just been made to run X at 640x480x16 but it has"
    echo "not worked (or it did but you didn not press the OK button in the"
    echo "Video-wizard). It is not feasible to run Puppy at any lower resolution"
    echo -e "or color. If you want to try again, type \"xwin\" at the prompt."
    echo "It may be helpful to look in /tmp/xerrs.txt for error messages"
    rm -f /tmp/videomode
    rm -f /tmp/Xflag
    echo -n "0x0111 640x480x16" > /etc/videomode
    exit 0
   else
    #flag to prevent endless loop X restarting...
    touch /tmp/Xflag
    #...pressing a button in video-wizard removes this file.
    #...however, aborting X will leave it in existence.
   fi
  fi
 fi
 #/tmp/videomode exists if starting X with new video test mode...
 if [ -e /tmp/videomode ];then
  #stored in format "0x0111 640x480x16"
  VIDMODE="`cat /tmp/videomode | cut -f 1 -d ' '`"
  VIDSCRN="`cat /tmp/videomode | tr -s ' ' | cut -f 2 -d ' '`"
 else
  #stored in format "0x0111 640x480x16"
  VIDMODE="`cat /etc/videomode | cut -f 1 -d ' '`"
  VIDSCRN="`cat /etc/videomode | tr -s ' ' | cut -f 2 -d ' '`"
 fi
fi

MOUSEBUTTONS=3
if [ -e /etc/mousebuttons ];then
 MOUSEBUTTONS=`cat /etc/mousebuttons`
fi

#0.9.8
XEXTRAOPTIONS=""
if [ -e /etc/xextraoptions ];then
 XEXTRAOPTIONS="`cat /etc/xextraoptions`"
fi

case $HAVEKDRIVE in #v0.9.9
 Xvesa)
  echo "Starting X with video mode $VIDMODE and mouse $MOUSEDEV..."
  #startx $VIDMODE $MOUSEDEV > /tmp/xerrs.txt 2>&1
  #Puppy 0.8.4 no need for startx script...
  VIDFREQ="`echo -n "$VIDSCRN" | cut -f 4 -d "x"`"
  if [ ! "$VIDFREQ" ];then
   echo "$VIDMODE" > /tmp/currentvideomode
   /usr/X11R6/bin/xinit /root/.xinitrc -- -mode $VIDMODE $XEXTRAOPTIONS -mouse /dev/mouse,$MOUSEBUTTONS > /tmp/xerrs.txt 2>&1
  else
   #also, want to have adjustable frequency, VIDSCRN=widthxheightxdepthxfreq...
   echo "$VIDSCRN" > /tmp/currentvideomode
   /usr/X11R6/bin/xinit /root/.xinitrc -- -screen $VIDSCRN $XEXTRAOPTIONS -mouse /dev/mouse,$MOUSEBUTTONS > /tmp/xerrs.txt 2>&1
  fi
  ;;
 Xfbdev)
  echo "Starting X with Xfbdev Kdrive X server, mouse $MOUSEDEV..."
  /usr/X11R6/bin/xinit /root/.xinitrc -- -mouse /dev/mouse,$MOUSEBUTTONS > /tmp/xerrs.txt 2>&1
  ;;
 *)
  echo "Starting X, specs in /etc/X11/XF86Config, startup apps /root/.xinitrc..."
  /usr/X11R6/bin/xinit /root/.xinitrc -- -bpp 16 > /tmp/xerrs.txt 2>&1
 ;;
esac

echo 'Exited from X. Type "xwin [fvwm95|jwm]" to restart X ([ ] mean optional).'
echo '(To change mouse and keyboard, type "rm /etc/mousedevice", then run xwin)'
if [ -f /usr/X11R6/bin/Xfbdev ];then
 echo '(To run the Xfbdev Framebuffer Wizard, type "framebufferwizard")'
fi
if [ -e /tmp/videomode ];then
 exec xwin #restart X.
fi

#v0.9.8
#/root/.fvwm95rc calls /usr/X11R6/bin/fvwmreboot or fvwmpoweroff at exit,
#which create /tmp/fvwmexitmode.txt then kill all X processes.
#0.9.9 ditto for w.m. restart calls /usr/X11R6/bin/restartwm.
if [ -f /tmp/fvwmexitmode.txt ];then
 FVWMEXITMODE="`cat /tmp/fvwmexitmode.txt`"
 #rm -f /tmp/fvwmexitmode.txt ...no, removed in rc.reboot.
 if [ "$FVWMEXITMODE" = "poweroff" ];then
  exec /sbin/poweroff
 fi
 if [ "$FVWMEXITMODE" = "reboot" ];then
  exec /sbin/reboot
 fi
 #0.9.9 restart window manager...
 #make a gross assumption, if fvwmexitmode.txt exists, haven't already exited this script, then want
 #to restart maybe with different window manager. /etc/windowmanager already has desired w.m.
 rm -f /tmp/fvwmexitmode.txt #definitely delete it now.
 exec xwin
fi

#the end#
