#these were originally in the retired script /sbin/pup_event_frontend_d
#130610 bugfix remove desktop icon if label different from drv name.
#130613 npierce: free_coords() supports screen widths not multiple of 32.
#130807 L18L: set executable in Woof 'VCSMETEDATA' file. move gettext declaration from other scripts.

#130807 L18L
export TEXTDOMAIN=pup_event_frontend_d
export OUTPUT_CHARSET=UTF-8
. gettext.sh

#v424 experiment, automatic unmounting...
auto_unmount_func() {
 for ONEMNTPT in `mount | grep '^/dev/' | grep -vE '^/dev/loop|^/dev/ram' | cut -f 3 -d ' ' | grep -v '^/initrd/' | grep -v '^/$' | tr '\n' ' '`
 do
  if [ "`fuser -v -m $ONEMNTPT`" = "" ];then
   sync
   umount $ONEMNTPT ; umVAL=$?
   #code from pmount: fix zombie process prevents partition unmount, invisible to fuser...
   if [ $umVAL -ne 0 ];then
    #120129 karl godt: fix ZOMBIES, see http://murga-linux.com/puppy/viewtopic.php?t=71767&start=420 ...
    ZOMBIES="`ps -H -A | grep '<defunct>' | sed 's/^[[:blank:]]*//;s/  /|/g' | grep -v '|||' | cut -f 1 -d ' ' | tr '\n' ' '`"
    for ONEZOMBIE in $ZOMBIES
    do
     kill $ONEZOMBIE 2>/dev/null
    done
    umount $ONEMNTPT ; umVAL=$?
   fi
  fi
 done
}

format_size_func() {
 [ ! $SIZE ] && SIZE=0
 if [ $SIZE -gt 1048576 ];then #1024*1024
  SIZE="`LANG=$OLDLANG dc $SIZE 1048576 \/ p`" #100613 dc o/p '.' if LANG=C.
  SIZE="`LANG=$OLDLANG printf "%.1f" $SIZE`GB" #100613
 else
  if [ $SIZE -gt 99 ];then
   SIZE="`expr $SIZE \/ 1024`MB"
  else
   SIZE="`LANG=$OLDLANG dc $SIZE 1024 \/ p`" #100613
   SIZE="`LANG=$OLDLANG printf "%.1f" $SIZE`MB" #100613
  fi
 fi
 [ "$SIZE" = "0.0MB" ] && SIZE="0"
 [ "$SIZE" = "0,0MB" ] && SIZE="0" #100613
}

create_appinfo_func() { #needs INFO, DRV_CATEGORY, MOREITEMS, ONEDRVNAME
   echo "<?xml version=\"1.0\"?>
<AppInfo>
  <Summary>${INFO}</Summary>
  <About>
   <Purpose>`gettext \"Drive icons are part of Puppy Event Manager, see System menu\"`</Purpose>
   <Authors>Barry Kauler LGPL 2008</Authors>
  </About>
  <AppMenu>
   <Item option=\"pmount_${DRV_CATEGORY}\" icon=\"gtk-harddisk\"><Label>$(gettext 'Run Pmount Puppy Drive Mounter')</Label></Item><Item option=\"eventmanager\" icon=\"gtk-execute\"><Label>$(gettext 'Run Desktop Drive Icon Manager')</Label></Item>${MOREITEMS}
  </AppMenu>
</AppInfo>" > /root/.pup_event/drive_${ONEDRVNAME}/AppInfo.xml
}

#v431 shinobar: fix for overlapping icons at some screen resolutions...
#140304 deleted redundant free_coord function
#See http://murga-linux.com/puppy/viewtopic.php?p=762099#762099

free_coord() {
 #find a free place on desktop. v410 avoid exact overlapping icons...
 #091208 finetune placement, also any side of screen...
 #read the coords grid, code from shinobar...
 COORDSGRID="`grep -o ' x="[0-9]*" y="[0-9]*" ' /root/Choices/ROX-Filer/PuppyPin | sed 's/[0-9]"/"/g'`"
 PIN_GRID_STEP=`grep "pinboard_grid_step" $HOME/.config/rox.sourceforge.net/ROX-Filer/Options | sed -e "s/ *<[^>]*>//g"`
 [ ! $PIN_GRID_STEP ] && PIN_GRID_STEP=16 #16=medium.
 [ $PIN_GRID_STEP -eq 0 ] && PIN_GRID_STEP=16 #precaution.
 PIN_GRID_STEP2=`expr $PIN_GRID_STEP \/ 2`
 MAX_X=`expr $SCRN_X - 96`
 MAX_Y=`expr $SCRN_Y - 96`
 case $ICON_PLACE_ORIENTATION in #see /etc/eventmanager
  bottom)
   COORD_Y=`expr $SCRN_Y - $ICON_PLACE_EDGE_GAP` #default 64, see /etc/eventmanager
   COORD_Y=`expr \( $COORD_Y + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP`
   COORD_X=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_X=`expr \( $COORD_X + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP` #130613
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" |  sed 's/[0-9]"/"/g'`"
    [ "`echo "$COORDSGRID" | grep "$xgPATTERN"`" = "" ] && break
    COORD_X=`expr $COORD_X + $ICON_PLACE_SPACING`
    if [ $COORD_X -gt $MAX_X ];then #start next line up.
     xyCNT=`expr $xyCNT + 1`
     [ $xyCNT -gt 1 ] && break
     COORD_Y=`expr $COORD_Y - $ICON_PLACE_SPACING`
     COORD_X=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   done
  ;;
  top)
   COORD_Y=$ICON_PLACE_EDGE_GAP
   COORD_Y=`expr \( $COORD_Y + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP`
   COORD_X=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_X=`expr \( $COORD_X + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP` #130613
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" |  sed 's/[0-9]"/"/g'`"
    [ "`echo "$COORDSGRID" | grep "$xgPATTERN"`" = "" ] && break
    COORD_X=`expr $COORD_X + $ICON_PLACE_SPACING`
    if [ $COORD_X -gt $MAX_X ];then #start next line down.
     xyCNT=`expr $xyCNT + 1`
     [ $xyCNT -gt 1 ] && break
     COORD_Y=`expr $COORD_Y + $ICON_PLACE_SPACING`
     COORD_X=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   done
  ;;
  left)
   COORD_X=$ICON_PLACE_EDGE_GAP #default 64, see /etc/eventmanager
   COORD_X=`expr \( $COORD_X + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP` #130613
   COORD_Y=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_Y=`expr \( $COORD_Y + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP`
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" |  sed 's/[0-9]"/"/g'`"
    [ "`echo "$COORDSGRID" | grep "$xgPATTERN"`" = "" ] && break
    COORD_Y=`expr $COORD_Y + $ICON_PLACE_SPACING`
    if [ $COORD_Y -gt $MAX_Y ];then #start next row.
     xyCNT=`expr $xyCNT + 1`
     [ $xyCNT -gt 1 ] && break
     COORD_X=`expr $COORD_X + $ICON_PLACE_SPACING`
     COORD_Y=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   done
  ;;
  right)
   COORD_X=`expr $SCRN_X - $ICON_PLACE_EDGE_GAP` #default 64, see /etc/eventmanager
   COORD_X=`expr \( $COORD_X + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP` #130613
   COORD_Y=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_Y=`expr \( $COORD_Y + $PIN_GRID_STEP2 \) / $PIN_GRID_STEP '*'  $PIN_GRID_STEP`
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" |  sed 's/[0-9]"/"/g'`"
    [ "`echo "$COORDSGRID" | grep "$xgPATTERN"`" = "" ] && break
    COORD_Y=`expr $COORD_Y + $ICON_PLACE_SPACING`
    if [ $COORD_Y -gt $MAX_Y ];then #start next row back.
     xyCNT=`expr $xyCNT + 1`
     [ $xyCNT -gt 1 ] && break
     COORD_X=`expr $COORD_X - $ICON_PLACE_SPACING`
     COORD_Y=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" "
   done
  ;;
 esac
}

add_pinboard_func() { #needs ONEDRVNAME, DRV_CATEGORY, FSTYPE
 #ppPATTERN=' label="'"$ONEDRVNAME"'"'
 #v411 rerwin: avoid icon getting duplicated if it is renamed...
 ppPATTERN="/root/.pup_event/drive_${ONEDRVNAME}<"
 [ "`grep "$ppPATTERN" /root/Choices/ROX-Filer/PuppyPin`" != "" ] && return #precaution.
 free_coord
 #120503 the label "mmcblk0p1" (SD memory cards via mmc interface) is too long...
 cutONEDRVNAME="`echo -n "${ONEDRVNAME}" | sed -e 's%cblk%%'`"
 #140101 SFR: partitions labels
 if [ "$ICONPARTITIONS" = "true" ] && [ "$LABELPARTITIONS" = "true" ]; then
   realLABEL=`blkid "/dev/${cutONEDRVNAME}" | grep -o "LABEL=.*" | cut -f2 -d '"'`
   [ "$realLABEL" = "" ] && realLABEL=" "
   [ "${#realLABEL}" -gt 8 ] && realLABEL="${realLABEL:0:7}..."
   cutONEDRVNAME="${cutONEDRVNAME}`echo -e "\n$realLABEL"`"
 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\">
  <PinboardAdd>
   <Path>/root/.pup_event/drive_${ONEDRVNAME}</Path>
   <X>${COORD_X}</X>
   <Y>${COORD_Y}</Y>
   <Label>${cutONEDRVNAME}</Label>
   <Args>${DRV_CATEGORY} ${FSTYPE}</Args>
  </PinboardAdd>
 </env:Body>
</env:Envelope>"  | rox -R
}

create_icon_func() {
 #create desktop icon, and directory with handler script, icon and help...
 #requires global variables DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION.
 #if a passed param $1, then PROBEPART already known, inefficient to rerun here each time.
 DRVINFO="$DRV_NAME"'|0|0'
 if [ "$ICONPARTITIONS" = "true" ];then
  #display an icon for each partition... w476 add ext4... 130216 add f2fs...
  [ ! $1 ] && PROBEPART="`probepart -k | grep -E '\|f2fs\||\|ext4\||\|ntfs\||\|msdos\||\|vfat\||\|ext2\||\|ext3\||\|iso9660\||\|udf\||\|audiocd\||\|xfs\||\|reiser\||\|none'`"
  drvPATTERN='^/dev/'"${DRV_NAME}" #important, no space on end!
  DRVINFO="`echo "$PROBEPART" | grep "$drvPATTERN" | cut -f 1,2,3 -d '|' | tr '\n' ' '`"

  #w482 k2.6.25.16 /proc/partitions (read by probepart) can be slow to update...
  diCNT=0
  if [ "`echo -n "$DRV_NAME" | grep -E '^sd|^mmc'`" != "" ];then
   while [ "$DRVINFO" = "" ];do
    diCNT=`expr $diCNT + 1`
    [ $diCNT -gt 3 ] && break
    sleep 1
    PROBEPART="`probepart -k | grep -E '\|f2fs\||\|ext4\||\|ntfs\||\|msdos\||\|vfat\||\|ext2\||\|ext3\||\|iso9660\||\|udf\||\|audiocd\||\|xfs\||\|reiser'`" #130216 added f2fs.
    DRVINFO="`echo "$PROBEPART" | grep "$drvPATTERN" | cut -f 1,2,3 -d '|' | tr '\n' ' '`"
   done
  fi
  
 fi
 LANG=$OLDLANG #see restored to C below.
 for ONEDRVINFO in $DRVINFO
 do
  ONEDRVNAME="`echo -n "$ONEDRVINFO" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
  FSTYPE="`echo -n "$ONEDRVINFO" | cut -f 2 -d '|'`"
  SIZE=`echo -n "$ONEDRVINFO" | cut -f 3 -d '|'`
  drvPATTERN='^/dev/'"${ONEDRVNAME}"' ' #important, a space on end!
  if [ ! -d /root/.pup_event/drive_${ONEDRVNAME} ];then
   mkdir /root/.pup_event/drive_${ONEDRVNAME}
   cp -af /usr/local/bin/drive_all /root/.pup_event/drive_${ONEDRVNAME}/AppRun
   if [ -e /sys/block/${ONEDRVNAME} ];then
    #device is an entire drive.
    SIZE=`cat /sys/block/${ONEDRVNAME}/size`
    if [ "`echo ${ONEDRVNAME}|grep 'sr'`" != "" ];then
      cddetect -q -d/dev/${ONEDRVNAME} #slow but necessary
      case "$?" in 
       255)SIZE=$SIZE ;; #blank misreported
       *)SIZE=$(($SIZE/2)) ;; #get KB.
      esac
    else
      SIZE=$(($SIZE/2)) #get KB.
    fi
    format_size_func #formats SIZE for display.
    INFO="$(gettext 'Description'): ${DRV_DESCRIPTION} $(gettext 'Size'): ${SIZE}" #*WARNING* parsed in clean_desk_icons !!!
   else
    if [ "$FSTYPE" = "0" -a "$SIZE" = "0" ];then
     INFO="$(gettext 'Puppy drive manager')"
    else
     format_size_func #formats SIZE for display.
     INFO="$(gettext 'Filesystem'): ${FSTYPE} $(gettext 'Size'): ${SIZE}" #*WARNING* parsed in clean_desk_icons !!!
    fi
   fi
   #v424 add item to unmount all partitions...
   MOREITEMS="<Item option=\"unmount\" icon=\"gtk-harddisk\"><Label>$(eval_gettext 'Unmount ${ONEDRVNAME} (if currently mounted)')</Label></Item><Item option=\"unmountall\" icon=\"gtk-harddisk\"><Label>$(gettext 'Unmount ALL mounted partitions')</Label></Item>"
   create_appinfo_func #needs INFO, DRV_CATEGORY, MOREITEMS, ONEDRVNAME
  fi
  MNTSTATUS="`df | grep "$drvPATTERN"`"
  if [ "$MNTSTATUS" = "" ];then
   icon_unmounted_func $ONEDRVNAME $DRV_CATEGORY #see functions4puppy4.
  else
   icon_mounted_func $ONEDRVNAME $DRV_CATEGORY #see functions4puppy4.
  fi
  add_pinboard_func #needs ONEDRVNAME, DRV_CATEGORY, FSTYPE.
 done
 #always create a handler for entire drive, even if no icon displayed...
 if [ ! -d /root/.pup_event/drive_${DRV_NAME} ];then
  if [ -e /sys/block/${DRV_NAME} ];then
   mkdir /root/.pup_event/drive_${DRV_NAME}
   cp -af /usr/local/bin/drive_all /root/.pup_event/drive_${DRV_NAME}/AppRun
   SIZE=`cat /sys/block/${DRV_NAME}/size`
   SIZE=$(($SIZE/2)) #get KB.
   format_size_func #formats SIZE for display.
   INFO="$(gettext 'Description'): ${DRV_DESCRIPTION} $(gettext 'Size'): ${SIZE}"
   #v424 add item to unmount all mounted partitions...
   MOREITEMS="</Item><Item option=\"unmountall\" icon=\"gtk-harddisk\"><Label>$(gettext 'Unmount ALL mounted partitions')</Label></Item>"
   ONEDRVNAME="$DRV_NAME"
   create_appinfo_func #needs INFO, DRV_CATEGORY, MOREITEMS, ONEDRVNAME
  fi
 fi
 LANG=C
}

remove_pinboard_func() { #needs DRV_NAME (name of entire drive)
 rpPTN="drive_${DRV_NAME}[^<]*" #130610 label may differ from drv name.
 [ "`grep "$rpPTN" /root/Choices/ROX-Filer/PuppyPin`" = "" ] && return
 ALLNAMES="`grep -o "$rpPTN" /root/Choices/ROX-Filer/PuppyPin | cut -f 2 -d '_' | cut -f 1 -d '<' | tr '\n' ' '`"
 for ONEDRVNAME in $ALLNAMES
 do
  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\">
  <PinboardRemove>
   <Path>/root/.pup_event/drive_${ONEDRVNAME}</Path>
  </PinboardRemove>
 </env:Body>
</env:Envelope>"  | rox -R
 done
}

#note that init script in initrd takes care of restoring modules if enough space.
delete_func() { #called from free_func() and free_flash_func(). delete modules to create more free space.
 #passed param: /pup_rw=delete tmpfs top layer only.
 DEL_LAYER=$1
 #find out what modules are loaded, keep those...
 for ONEKEEP_MOD in `lsmod | cut -f 1 -d ' ' | grep -v 'Module'`
 do
  ONEKEEP_SPEC="`modinfo -F filename ${ONEKEEP_MOD}`"
  ONEKEEP_PATH="`dirname $ONEKEEP_SPEC`"
  mkdir -p /tmp${ONEKEEP_PATH}
  cp -af ${ONEKEEP_SPEC} /tmp${ONEKEEP_PATH}/
 done
 if [ "$DEL_LAYER" != "" ];then
  rm -rf ${DEL_LAYER}/lib/modules
 else
  if [ $PUPMODE -eq 3 -o $PUPMODE -eq 7 -o $PUPMODE -eq 13 ];then
   rm -rf ${SAVE_LAYER}/lib/modules
  fi
  rm -rf /lib/modules
 fi
 cp -af /tmp/lib/modules /lib/modules
 depmod -a
}

free_initrd_func() { #UniPup, runs entirely in initramfs.
 #110405 fix for later versions busybox...
 memFREEK=`free | grep -o 'Mem: .*' | tr -s ' ' | cut -f 4 -d ' '`
 swapFREEK=`free | grep -o 'Swap: .*' | tr -s ' ' | cut -f 4 -d ' '`
 SIZEFREEK=`expr $memFREEK + $swapFREEK`
 SIZEFREEM=`expr $SIZEFREEK \/ 1024`
 [ -s /tmp/pup_event_sizefreem ] && PREVSIZEFREEM=`cat /tmp/pup_event_sizefreem`
 [ "$PREVSIZEFREEM" = "$SIZEFREEM" ] && return
 #save to a file, freememapplet can read this...
 echo "$SIZEFREEM" > /tmp/pup_event_sizefreem
}

free_func() { #called every 4 seconds.
 case $PUPMODE in
  6|12)
   if [ -L /initrd/pup_rw ]; then
    SIZEFREEM=`df -m | grep ' /initrd/mnt/dev_save$' | tr -s ' ' | cut -f 4 -d ' '`
   else
    SIZEFREEM=`df -m | grep ' /initrd/pup_rw$' | tr -s ' ' | cut -f 4 -d ' '`
   fi
  ;;
  *)
   SIZEFREEM=`df -m | grep ' /$' | head -n 1 | tr -s ' ' | cut -f 4 -d ' '` #110509 rerwin: insert head -n 1
  ;;
 esac
 WARNMSG=""
 [ -s /tmp/pup_event_sizefreem ] && PREVSIZEFREEM=`cat /tmp/pup_event_sizefreem`
 [ $PREVSIZEFREEM -eq $SIZEFREEM ] && return
 if [ $SIZEFREEM -lt 10 ];then
  if [ -d /initrd/pup_rw/lib/modules/all-firmware -a "$ZDRVINIT" = "yes" ];then
   delete_func /initrd/pup_rw #save layer is at top, delete mods.
  else
   LANG=$OLDLANG WARNMSG="$(gettext 'WARNING: Personal storage getting full, strongly recommend you resize it or delete files!')" #130208
  fi
 fi
 VIRTUALFREEM=$SIZEFREEM
 if [ "$ZDRVINIT" = "yes" ];then #full set of modules present, moved from initrd.
  if [ -d /initrd/pup_rw/lib/modules/all-firmware ];then #have not yet deleted modules.
   #calc the "virtual" free space (would have if modules not there...)
   VIRTUALFREEM=`expr $SIZEFREEM + $SIZE_MODS_M`
   VIRTUALFREEM=`expr $VIRTUALFREEM - 1` #allow for some mods will not be deleted.
  fi
 fi
 #save to a file, freememapplet can read this...
 echo "$VIRTUALFREEM" > /tmp/pup_event_sizefreem
 [ $PUPMODE -eq 5 -o $PUPMODE -eq 2 ] && return 0 #5=first boot, no msgs at top of screen.
 if [ "$WARNMSG" != "" ];then
  killall yaf-splash
  #yaf-splash -margin 2 -bg red -bw 0 -placement top -font "9x15B" -outline 0 -text "$WARNMSG" &
  yaf-splash -bg red -placement top -text "$WARNMSG" &
 fi
}

free_flash_func() { #PUPMODE 3,7,13. called every 4 seconds.
 WARNMSG=""
 if [ -L /initrd/pup_ro1 ]; then
  SIZEFREEM=`df -m | grep ' /initrd/mnt/dev_save$' | tr -s ' ' | cut -f 4 -d ' '`
 else
  SIZEFREEM=`df -m | grep ' /initrd/pup_ro1$' | tr -s ' ' | cut -f 4 -d ' '`
 fi
 SIZETMPM=`df -m | grep ' /initrd/pup_rw$' | tr -s ' ' | cut -f 4 -d ' '`
 [ -s /tmp/pup_event_sizefreem ] && PREVSIZEFREEM=`cat /tmp/pup_event_sizefreem`
 [ -s /tmp/pup_event_sizetmpm ] && PREVSIZETMPM=`cat /tmp/pup_event_sizetmpm`
 [ $PREVSIZEFREEM -eq $SIZEFREEM -a $PREVSIZETMPM -eq $SIZETMPM ] && return
 if [ $SIZEFREEM -lt 10 ];then
  if [ -d /initrd/pup_ro1/lib/modules/all-firmware -a "$ZDRVINIT" = "yes" ];then
   delete_func /initrd/pup_ro1 #delete modules in save layer only.
  else
   LANG=$OLDLANG WARNMSG="$(gettext 'WARNING: Personal storage file getting full, strongly recommend you resize it or delete files!')" #130208
  fi
 fi
 if [ $SIZETMPM -lt 5 ];then
  if [ -d /initrd/pup_rw/lib/modules/all-firmware -a "$ZDRVINIT" = "yes" ];then
   delete_func /initrd/pup_rw #delete modules in top tmpfs layer only.
  else
   LANG=$OLDLANG WARNMSG="$(gettext 'WARNING: RAM working space only') ${SIZETMPM}$(gettext 'MB, recommend a reboot which will flush the RAM')"
  fi
 fi
 VIRTUALFREEM=$SIZEFREEM
 if [ "$ZDRVINIT" = "yes" ];then #full set of modules present at bootup.
  if [ -d /initrd/pup_ro1/lib/modules/all-firmware ];then #have not yet deleted modules.
   #calc the "virtual" free space (would have if modules not there...)
   VIRTUALFREEM=`expr $SIZEFREEM + $SIZE_MODS_M`
   VIRTUALFREEM=`expr $VIRTUALFREEM - 1` #allow for some mods will not be deleted.
  fi
 fi
 echo "$SIZETMPM" > /tmp/pup_event_sizetmpm
 #save to a file, freememapplet can read this...
 echo "$VIRTUALFREEM" > /tmp/pup_event_sizefreem
 if [ "$WARNMSG" != "" ];then
  killall yaf-splash
  #yaf-splash -margin 2 -bg red -bw 0 -placement top -font "9x15B" -outline 0 -text "$WARNMSG" &
  yaf-splash -bg red -placement top -text "$WARNMSG" &
 fi
}

savepuppy_func() { #called every 4 seconds.
 if [ -f /tmp/snapmergepuppyrequest ];then #by request.
  rm -f /tmp/snapmergepuppyrequest
  #yaf-splash -font "8x16" -outline 0 -margin 4 -bg orange -placement top -text "Saving RAM to 'pup_save' file..." &
  LANG=$OLDLANG yaf-splash -bg orange -placement top -close never -text "$(gettext "Saving RAM to 'pup_save' file...")" & #130116 set LANG.
  YAFPID=$!
  sync
  LANG=$OLDLANG nice -n 19 /usr/sbin/snapmergepuppy
  kill $YAFPID
 fi
}

#v407 fixed so works for sr* and hd* optical...
probe_optical_func() { #passed param is sr<0-9> or hd<a-z>
 DRV_NAME="$1"
 DRV_CATEGORY="optical"
 CDSTATUS2="0"
 opticalPATTERN='^/dev/'"${DRV_NAME}"' '
 [ "`grep "$opticalPATTERN" /proc/mounts`" != "" ] && return #only check cd if unmounted.
 cddetect_quick -d/dev/${DRV_NAME} >/dev/null 2>&1 #very fast.
 #...returns 0 if disc inserted, else 255.
 CDSTATUS2="$?"
 [ "$CDSTATUS2" != "0" ] && [ -d /root/.pup_event/drive_${DRV_NAME} ] && rm -rf /root/.pup_event/drive_${DRV_NAME} #v410 ensure no drive directory if drive empty.
 echo "$CDSTATUS2" > /tmp/pup_event_frontend_identify2_${DRV_NAME}
 [ ! -f /tmp/pup_event_frontend_identify1_${DRV_NAME} ] && cp /tmp/pup_event_frontend_identify2_${DRV_NAME} /tmp/pup_event_frontend_identify1_${DRV_NAME}
 CDSTATUS1="`cat /tmp/pup_event_frontend_identify1_${DRV_NAME}`"
 [ "$CDSTATUS1" = "$CDSTATUS2" ] && return
 cp -f /tmp/pup_event_frontend_identify2_${DRV_NAME} /tmp/pup_event_frontend_identify1_${DRV_NAME}
 if [ "$CDSTATUS2" = "0" ];then
  #VENDOR="`cat /sys/block/${DRV_NAME}/device/vendor | tr -s ' '`"
  #MODEL="`cat /sys/block/${DRV_NAME}/device/model | tr -s ' '`"
  #DRV_DESCRIPTION="$VENDOR $MODEL"
  opticalPATTERN='^/dev/'"${DRV_NAME}"'|'
  DRV_DESCRIPTION="`echo "$PROBEDISK" | grep "$opticalPATTERN" | cut -f 3 -d '|'`"
  if [ "$DRV_DESCRIPTION" = "" ];then #130610
   PROBEDISK="`probedisk`"
   DRV_DESCRIPTION="`echo "$PROBEDISK" | grep "$opticalPATTERN" | cut -f 3 -d '|'`"
  fi
  create_icon_func #requires DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION.
  [ "$HOTPLUGNOISY" = "true" ] && /root/.pup_event/drive_${DRV_NAME}/AppRun optical & #handler script.
 else
  rm -rf /root/.pup_event/drive_${DRV_NAME} 2>/dev/null
  remove_pinboard_func #needs DRV_NAME
 fi
}

#v407 detect ls120 and zip disc inserted...
probe_floppy_func() { #passed param is drive name (exs: hdc, sdc)
 DRV_NAME="$1"
 DRVMAIN="`echo -n "$DRV_NAME" | cut -c 1,2`" #sd or hd.
 DRV_CATEGORY="floppy"
 floppyPATTERN="^/dev/$DRV_NAME"
 [ "`grep "$floppyPATTERN" /proc/mounts`" != "" ] && return #mounted.
 if [ "$DRVMAIN" = "hd" ];then
  cat /proc/ide/${DRV_NAME}/identify > /tmp/pup_event_frontend_identify2_${DRV_NAME}
 else
  #this method not as good, as only detects disc if it has a partition...
  floppyPATTERN=" ${DRV_NAME}"
  grep "$floppyPATTERN" /proc/partitions > /tmp/pup_event_frontend_identify2_${DRV_NAME}
 fi
 [ ! -f /tmp/pup_event_frontend_identify1_${DRV_NAME} ] && cp /tmp/pup_event_frontend_identify2_${DRV_NAME} /tmp/pup_event_frontend_identify1_${DRV_NAME}
 [ "`diff -s /tmp/pup_event_frontend_identify1_${DRV_NAME} /tmp/pup_event_frontend_identify2_${DRV_NAME} | grep ' are identical'`" != "" ] && return
 cp -f /tmp/pup_event_frontend_identify2_${DRV_NAME} /tmp/pup_event_frontend_identify1_${DRV_NAME}
 FLOPPYSTATUS=1
 [ "`disktype /dev/${DRV_NAME} 2>&1 | grep 'I/O error'`" = "" ] && FLOPPYSTATUS=0
 if [ "$FLOPPYSTATUS" = "0" ];then
  if [ "$DRVMAIN" = "hd" ];then
   DRV_DESCRIPTION="`cat /proc/ide/${DRV_NAME}/model`"
  else
   DRV_DESCRIPTION="`cat /sys/block/${DRV_NAME}/device/model | tr -s ' '`"
  fi
  create_icon_func #requires DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION.
  [ "$HOTPLUGNOISY" = "true" ] && /root/.pup_event/drive_${DRV_NAME}/AppRun floppy & #handler script.
 else
  rm -rf /root/.pup_event/drive_${DRV_NAME} 2>/dev/null
  remove_pinboard_func #needs DRV_NAME
 fi
}

