#!/bin/sh
#BK called from /root/.xinitrc
#v410 remove icons when drives removed.
#v410 omit icon if optical drive; defer it to pup_event_frontend_d
#w014 bugfix, refresh icons if removable drive swapped when PC turned off.

# i18n gettext
export TEXTDOMAIN=pup_event
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
LANGORG=$LANG
#. /usr/bin/gettext.sh
#

export LANG=C
. /etc/eventmanager #has RAMSAVEINTERVAL, ICONDESK, ICONPARTITIONS, HOTPLUGNOISY, HOTPLUGON, FLOPPYICON.

ePUPPYPIN="`grep -v '/root/.pup_event/drive_' /root/Choices/ROX-Filer/PuppyPin | grep -v '</pinboard>'`"

#v403 /usr/sbin/eventmanager creates this file if all icons need to be rebuilt...
if [ -f /tmp/pup_event_icon_change_flag ];then
 rm -f /tmp/pup_event_icon_change_flag
 echo "$ePUPPYPIN" >  /root/Choices/ROX-Filer/PuppyPin
 echo '</pinboard>' >> /root/Choices/ROX-Filer/PuppyPin
 rm -rf /root/.pup_event/drive_* 2>/dev/null
 exit
fi

#remove all invalid drive icons off desktop...
echo -n "" > /tmp/pup_event_ok_pin
if [ "$ICONDESK" = "false" ];then
 #leave single 'drives' icon on desktop...
 grep '/root/.pup_event/drive_drives' /root/Choices/ROX-Filer/PuppyPin >> /tmp/pup_event_ok_pin
 rm -rf /root/.pup_event/drive_[^d]* 2>/dev/null #delete all except drive_drives.
else

 #v410 Delete drive_ directories for removed drives...
 #note, this will not detect removed optical and floppy discs (see /sbin/pup_event_frontend_d).
 #Get the directory names for the drives only, not the partitions, to avoid redundant iterations through the for-loop.
 DIR_DRVS="`ls -1 /root/.pup_event | sed 's/drive_//' | grep -E "^hd.$|^sd.$|^sr|^mmcblk.$" | tr '\n' ' '`"
 for ONEDRV in $DIR_DRVS
 do
  [ ! -d /root/.pup_event/drive_$ONEDRV ] && continue
  [ ! -e /sys/block/${ONEDRV} ] && rm -rf /root/.pup_event/drive_${ONEDRV}*
 done
 
 for ONEDRV in `ls -1 /sys/block | grep -vE 'loop|ram' | tr '\n' ' '`
 do
  odPATTERN="/root/.pup_event/drive_${ONEDRV}"
  OKDRV="`grep "$odPATTERN" /root/Choices/ROX-Filer/PuppyPin`"
  if [ "$OKDRV" = "" ];then
   rm -rf /root/.pup_event/drive_${ONEDRV}* 2>/dev/null
  else
   [ ! -d /root/.pup_event/drive_${ONEDRV} ] && continue #v408
   DRVCUT="`echo -n "$ONEDRV" | cut -c 1,2`" #v410
   [ "$DRVCUT" = "sr" ] && continue #v410 omit icon if optical drive; defer it to pup_event_frontend_d
   [ "$DRVCUT" = "sc" ] && continue #v410 scd, ditto.
   if [ "$DRVCUT" = "hd" ];then
    MEDIACAT="`cat /proc/ide/${ONEDRV}/media`"
    [ "$MEDIACAT" = "cdrom" ] && continue #v410 omit icon if optical drive; defer it to pup_event_frontend_d
   fi
   #w014 user may have swapped removable drives while pc turned off...
   
   LANG=$LANGORG T_size="`gettext \"Size:\"`"  # same as in /sbin/pup_event_frontend_d create_icon_func() 
   
   DRVMODEL1="`grep -o '<Summary>.*${T_Size:}' /root/.pup_event/drive_${ONEDRV}/AppInfo.xml | cut -f 2-20 -d ' ' | rev | cut -f 2-20 -d ' ' | rev`"
   #note, this must be same as done in /sbin/probedisk2...
   DRVMODEL2="`cat /sys/block/$ONEDRV/device/vendor | tr -s ' '``cat /sys/block/${ONEDRV}/device/model | tr -s ' '`"   
   if [ "$DRVMODEL1" != "$DRVMODEL2" ];then
    rm -rf /root/.pup_event/drive_${ONEDRV}* 2>/dev/null
    continue
   fi
   echo "$OKDRV" >> /tmp/pup_event_ok_pin
  fi
 done
 
fi

#if [ "`cat /tmp/pup_event_ok_pin`" != "" ];then
 echo "$ePUPPYPIN" >  /root/Choices/ROX-Filer/PuppyPin
 cat /tmp/pup_event_ok_pin >> /root/Choices/ROX-Filer/PuppyPin
 echo '</pinboard>' >> /root/Choices/ROX-Filer/PuppyPin
#fi

###END###
