#some more functions called from boot scripts in Puppy4...
#v411 moved choosemousefunc() out of /usr/sbin/xorgwizard, want call from rc.sysinit.
#w468 not using set_mixers() anymore, it's done in /etc/init.d/alsa.
#w468 choosemousefunc() simplified.
#101120 new firmware_tarball_func, as /sbin/pup_event_backend_modprobe rewritten.

#101119 called from rc.sysinit...
#global $MODULE is name of module, global $KERNVER.
firmware_tarball_func() {
 MODULEx="`echo -n "$MODULE" | tr '_' '-'`"
 iPATTERN='^'"${MODULE}"'$|^'"${MODULEx}"'$'
 if [ "`grep "$iPATTERN" /etc/modules/firmware.dep.inst.${KERNVER}`" = "" ];then
  fPATTERN='[:,]'"${MODULE}"'\.ko|[:,]'"${MODULEx}"'\.ko'
  FIRMPKG="`cat /etc/modules/firmware.dep.${KERNVER}  | grep -v '^#' | grep ':' | grep -E "$fPATTERN" | cut -f 1 -d ':' | head -n 1`"
  if [ "$FIRMPKG" != "" ];then
   if [ -f /lib/modules/all-firmware/${FIRMPKG}.tar.gz ];then
    tar -z -x --strip=1 --directory=/ -f /lib/modules/all-firmware/${FIRMPKG}.tar.gz > /dev/null 2>&1
    #execute any post-install script...
    if [ -f /pinstall.${FIRMPKG}.sh ];then
     BRKCNT=0; SLEEPU=$((${$} * 10))
     while [ 1 ];do #serialise execution of pinstall scripts...
      PINSTALLCNT=`find / -maxdepth 1 -type f -name 'pinstall.*.sh' | wc -l`
      [ $PINSTALLCNT -eq 1 ] && break
      usleep $SLEEPU
      BRKCNT=$(($BRKCNT + 1))
      [ $BRKCNT -gt 5 ] && break
     done
     echo "$MODULE" >> /etc/modules/firmware.dep.inst.${KERNVER}
     cd /
     /pinstall.${FIRMPKG}.sh >/dev/null 2>&1
     rm -f /pinstall.${FIRMPKG}.sh >/dev/null 2>&1
    fi
   fi
  fi
 fi
}


#v403 change desktop icon to unmounted status, called from pup_event_frontend_d, pmount...
icon_unmounted_func() {
 #passed params: drivename category  ex: sda usbdrv
 DRVICON="drive48.png" #default.
 [ "$2" = "usbdrv" ] && DRVICON="usbdrv48.png"
 [ "$2" = "card" ] && DRVICON="card48.png"
 [ "$2" = "floppy" ] && DRVICON="floppy48.png"
 [ "$2" = "optical" ] && DRVICON="optical48.png"
 #find out if already done...
 if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
  GLOBLINES="`cat /root/.config/rox.sourceforge.net/ROX-Filer/globicons | tr '\n' '|' | sed -e 's%</rule>|%\n%g'`"
  diPATTERN='/root/.pup_event/drive_'"${1}"'"'
  [ "`echo "$GLOBLINES" | grep "$diPATTERN" | grep "$DRVICON"`" != "" ] && return
 fi
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <SetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
   <Icon>/usr/local/lib/X11/pixmaps/${DRVICON}</Icon>
  </SetIcon>
 </env:Body>
</env:Envelope>"  | rox -R
}

icon_mounted_func() {
 #passed params: drivename category  ex: sda usbdrv
 DRVICON="drive_mntd48.png" #default.
 [ "$2" = "usbdrv" ] && DRVICON="usbdrv_mntd48.png"
 [ "$2" = "card" ] && DRVICON="card_mntd48.png"
 [ "$2" = "floppy" ] && DRVICON="floppy_mntd48.png"
 [ "$2" = "optical" ] && DRVICON="optical_mntd48.png"
 imPATTERN='^/dev/'"${1}" #important, no space on end.
# if [ "`mount | cut -f 1,3 -d ' ' | grep "$imPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
 if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$imPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
  #only partitions mntd on /initrd/* then must be mntd at boot. cannot unmount.
  #also, full hd install has partition mntd on '/'.
  DRVICON="drive_mntd_boot48.png" #default.
  [ "$2" = "usbdrv" ] && DRVICON="usbdrv_mntd_boot48.png"
  [ "$2" = "card" ] && DRVICON="card_mntd_boot48.png"
  [ "$2" = "floppy" ] && DRVICON="floppy_mntd_boot48.png"
  [ "$2" = "optical" ] && DRVICON="optical_mntd_boot48.png"
 fi
 #find out if already done...
 if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
  GLOBLINES="`cat /root/.config/rox.sourceforge.net/ROX-Filer/globicons | tr '\n' '|' | sed -e 's%</rule>|%\n%g'`"
  diPATTERN='/root/.pup_event/drive_'"${1}"'"'
  [ "`echo "$GLOBLINES" | grep "$diPATTERN" | grep "$DRVICON"`" != "" ] && return
 fi
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <SetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
   <Icon>/usr/local/lib/X11/pixmaps/${DRVICON}</Icon>
  </SetIcon>
 </env:Body>
</env:Envelope>"  | rox -R
}


icon_remove_func() {
 #passed param is drive icon to remove from desktop. ex: sda
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <UnsetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
  </UnsetIcon>
 </env:Body>
</env:Envelope>"  | rox -R
}

##v403 got this out of /usr/sbin/alsaconf, needed to get sound on my laptop...
##thi func called from rc.local0.
#set_mixers() {
# amixer -s -q <<EOF
#set Master 75% unmute
#set Master -12dB
#set 'Master Mono' 75% unmute
#set 'Master Mono' -12dB
#set Front 75% unmute
#set Front -12dB
#set PCM 90% unmute
#set PCM 0dB
#mixer Synth 90% unmute
#mixer Synth 0dB
#mixer CD 90% unmute
#mixer CD 0dB
## mute mic
#set Mic 0% mute
## ESS 1969 chipset has 2 PCM channels
#set PCM,1 90% unmute
#set PCM,1 0dB
## Trident/YMFPCI/emu10k1
#set Wave 100% unmute
#set Music 100% unmute
#set AC97 100% unmute
## CS4237B chipset:
#set 'Master Digital' 75% unmute
## Envy24 chips with analog outs
#set DAC 90% unmute
#set DAC -12dB
#set DAC,0 90% unmute
#set DAC,0 -12dB
#set DAC,1 90% unmute
#set DAC,1 -12dB
## some notebooks use headphone instead of master
#set Headphone 75% unmute
#set Headphone -12dB
#set Playback 100% unmute
## turn off digital switches
#set "SB Live Analog/Digital Output Jack" off
#set "Audigy Analog/Digital Output Jack" off
#EOF
#}

#w468 simplify...
##v411 moved from /usr/sbin/xorgwizard...
#choosemousefunc() { #called from xorgwizard and rc.sysinit.
# DETECTEDMOUSEDEV="`cat /etc/mousedevice`"
# case $DETECTEDMOUSEDEV in
#  input/mice)
#   MOUSETYPE="usb"
#   MOUSEMENU1a='usb'; MOUSEMENU1b='(little 11x4mm flat plug)'
#   MOUSEMENU2a='ps/2'; MOUSEMENU2b='(little 8mm round green plug, or laptop builtin touchpad)'
#   MOUSEMENU3a='serial'; MOUSEMENU3b='(big 15x9mm DB9 or DB25 plug)'
#  ;;
#  psaux)
#   MOUSETYPE="ps/2"
#   MOUSEMENU1a='ps/2'; MOUSEMENU1b='(little 8mm round green plug, or laptop builtin touchpad)'
#   MOUSEMENU2a='usb'; MOUSEMENU2b='(little 11x4mm flat plug)'
#   MOUSEMENU3a='serial'; MOUSEMENU3b='(big 15x9mm DB9 or DB25 plug)'
#  ;;
#  *) #ttyS0-4
#   MOUSETYPE="serial"
#   MOUSEMENU1a='serial'; MOUSEMENU1b='(big 15x9mm DB9 or DB25 plug)'
#   MOUSEMENU2a='ps/2'; MOUSEMENU2b='(little 8mm round green plug, or laptop builtin touchpad)'
#   MOUSEMENU3a='usb'; MOUSEMENU3b='(little 11x4mm flat plug)'
#  ;;
# esac
# EXTRAMOUSEMSG="HOWEVER, if you disagree then please choose the correct type, but do note that your alternative choice here will override autodetection on subsequent startups -- in future, if you want to remove this 'sticky' behaviour, please delete file /etc/mousedevice_sticky."
# if [ -f /etc/mousedevice_sticky ];then
#  EXTRAMOUSEMSG="HOWEVER, if you disagree then please choose the correct type. Please note that autodetection of mouse at bootup is currently turned off, determined by existence of file /etc/mousedevice_sticky. If you want to restore autodetection, please delete that file."
# fi
# dialog --no-cancel --menu "Puppy thinks that you have a $MOUSETYPE mouse.\n
#If you agree with that, just press ENTER key only (OK button).\n\n
#${EXTRAMOUSEMSG}\n\n
#Please choose mouse type...\n(You can identify type by plug shape)\nUse UP/DOWN ARROW keys, ENTER key to finish" 0 0 0 "$MOUSEMENU1a" "$MOUSEMENU1b" "$MOUSEMENU2a" "$MOUSEMENU2b" "$MOUSEMENU3a" "$MOUSEMENU3b" 2> /tmp/testmsx
# if [ $? -eq 0 ];then
#  CHOSENMOUSETYPE="`cat /tmp/testmsx`"
# else #precaution.
#  CHOSENMOUSETYPE="$MOUSETYPE"
# fi
# MOUSEDEV="psaux"
# if [ "$CHOSENMOUSETYPE" = "usb" ];then
#  MOUSEDEV="input/mice"
#  [ "`lsmod | grep '^usbhid'`" = "" ] && modprobe usbhid #v2.17
# fi
# if [ "$CHOSENMOUSETYPE" = "serial" ];then
#  dialog --no-cancel --menu "Serial port mouse plugged into...\nUse UP/DOWN ARROW keys, ENTER key to finish" 0 0 0 ttyS0 "COM1 (most likely)" ttyS1 COM2 ttyS2 COM3 ttyS3 COM4 2> /tmp/testmsx
#  MOUSEDEV="`cat /tmp/testmsx`"
#  modprobe sermouse
# fi
# if [ "$CHOSENMOUSETYPE" != "$MOUSETYPE" ];then
#  echo -n "yes" > /etc/mousedevice_sticky #content doesn't matter.
#  echo -n "$DETECTEDMOUSEDEV" > /etc/oldmousedevice
# fi
# MOUSETYPE="$CHOSENMOUSETYPE"
# ln -snf $MOUSEDEV /dev/mouse
# echo -n "$MOUSEDEV" > /etc/mousedevice #note, rc.sysinit will read this on next boot.
# #fix mouse protocol for Xorg...
# if [ -f /etc/X11/xorg.conf ];then
#    case "$MOUSEDEV" in
#     inpu*) #"input/mice", usb. v411 fix...
#      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\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\+\)\|\(\w\+\)\)\W\+#mouse0protocol/	Option	    "Protocol" "Microsoft" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
#      ;;
#     *) #v411 fix...
#      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\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
#} #choosemousefunc()

choosemousefunc() { #w468 called from xorgwizard.
 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). Press the UP and DOWN arrow keys to choose your mouse type, then press the ENTER key:')"
 DETECTEDMOUSEDEV="`cat /etc/mousedevice`"
 case $DETECTEDMOUSEDEV in
  ttyS*)
   MOUSEMENU1a='serial'; MOUSEMENU1b=$(gettext '(mouse in very old computers)')
   MOUSEMENU2a='usb/ps-2'; MOUSEMENU2b=$(gettext '(mouse or touchpad in all modern computers)')
  ;;
  *)
   MOUSEMENU1a='usb/ps-2'; MOUSEMENU1b=$(gettext '(mouse or touchpad in all modern computers)')
   MOUSEMENU2a='serial'; MOUSEMENU2b=$(gettext '(mouse in very old computers)')
  ;;
 esac
 dialog --no-cancel --menu "${MOUSEINTRO}\n\n
Please choose mouse type..." 0 0 0 "$MOUSEMENU1a" "$MOUSEMENU1b" "$MOUSEMENU2a" "$MOUSEMENU2b" 2> /tmp/testmsx
 [ $? -ne 0 ] && return 1
 CHOSENMOUSETYPE="`cat /tmp/testmsx`"
 case $CHOSENMOUSETYPE in
 serial)
  dialog --no-cancel --menu "$(gettext 'Serial port mouse plugged into...\nUse UP/DOWN ARROW keys, ENTER key to finish')" 0 0 0 ttyS0 "$(gettext 'COM1 (most likely)')" ttyS1 COM2 ttyS2 COM3 ttyS3 COM4 2> /tmp/testmsx
  [ $? -ne 0 ] && return 1
  MOUSEDEV="`cat /tmp/testmsx`"
  modprobe sermouse
  ln -snf $MOUSEDEV /dev/mouse
  echo -n "$MOUSEDEV" > /etc/mousedevice
  cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\w\+\)\|\(\w\+\)\)\W\+#mouse0protocol/	Option	    "Protocol" "Microsoft" #mouse0protocol/' > /tmp/xorg.conf.tmp2
 ;;
 *)
  echo -n "input/mice" > /etc/mousedevice
  ln -snf input/mice /dev/mouse
  cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\(\(\w\+\/\w\+\)\|\(\w\+\)\)\W\+#mouse0protocol/	Option	    "Protocol" "IMPS\/2" #mouse0protocol/' > /tmp/xorg.conf.tmp2
 ;;
 esac
 sync
 cp -f /tmp/xorg.conf.tmp2 /etc/X11/xorg.conf
 rm -f /tmp/xorg.conf.tmp2
} #choosemousefunc()


###END###
