#!/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.
#120131 rodin.s: internationalized. See warning about "Size:" translation.
#120213 /var/local/pup_event_icon_change_flag path changed from /tmp.
#120222 internationalization introduced bug, fixed it (caused drive icons to redraw every time X starts).
#170716 exit if /root/.pup_event not exist (first bootup).
#180408 support nvme drives.

[ ! -d /root/.pup_event ] && exit 0 #170718

# i18n gettext
TEXTDOMAIN=clean_desk_icons
export TEXTDOMAIN
. /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...
#120213 note, /var/local/pup_event_icon_change_flag is written to by /usr/sbin/xorgwizard, video-wizard, eventmanager.
if [ -f /var/local/pup_event_icon_change_flag ];then #120213 path changed from /tmp
 rm -f /var/local/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.
 #180408 nvme drive naming: nvme[0-9]n[1-9] partitions, append: p[1-9]
 DIR_DRVS="`ls -1 /root/.pup_event | sed 's/drive_//' | grep -E "^hd.$|^sd.$|^sr|^mmcblk.$|^nvme[0-9]n[1-9]$" | 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="`cat /etc/profile|grep '^LANG='`"
   #eval $LANG
   #T_size="`gettext \"Size:\"`"  #*WARNING* translation must be same as in /sbin/pup_event_frontend_d create_icon_func() 
   #LANG=C
   #120222 internationalization introduced bug, fix it...
   #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`"
   #120222 above is fixed, however go one step further and eliminate dependency on the translation of "Size:"...
   DRVMODEL1="`grep -o '<Summary>.*</Summary>' /root/.pup_event/drive_${ONEDRV}/AppInfo.xml | cut -f 2-9 -d ':' | cut -f 1 -d '<' | tr -s ' ' | rev | cut -f 3-99 -d ' ' | rev | sed -e 's%^ %%'`"
   #note, this must be same as done in /sbin/probedisk...
   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###
