#!/bin/dash
# redraw all drive icons - no root access needed.
# Copyright (C) James Budiono 2013, 2015
# License: GNU GPL Version 3 or later
#
# EVENTMANAGER_CONFIG defined in rc.Xservices

### configurations
# the following must be identical with those in udev-handler
UDEV_DRIVE_ICON_ROOT=/tmp/udev-fatdog-drive-icon
UDEV_DRIVE_ICON_EVENTS=$UDEV_DRIVE_ICON_ROOT/events
UDEV_DRIVE_ICON_DEVICES=$UDEV_DRIVE_ICON_ROOT/devices
UDEV_EVENT_COUNTER=$UDEV_DRIVE_ICON_ROOT/counter
COUNTER_MAX=100  # max number of queued events

PINBOARD_FILE=$HOME/.config/rox.sourceforge.net/ROX-Filer/PuppyPin
DRIVE_ICON_ROOT=/tmp/fatdog-drive-icons.$USER.$XSESSION_ID

# get the rox functions
. /usr/sbin/fatdog-drive-icon-roxlib.sh 
. $EVENTMANAGER_CONFIG

### helpers - copied from frontend
# $1 dev (without /dev)
action_add() {
	if [ -e $UDEV_DRIVE_ICON_DEVICES/$1 ]; then
		. $UDEV_DRIVE_ICON_DEVICES/$1 # DRIVENAME, DRIVE_TYPE, DEVNAME, SUMMARY, VOLABEL		
		mkdir -p "$DRIVE_ICON_ROOT/$DRIVENAME"
		create_rox_app "$DRIVE_ICON_ROOT/$DRIVENAME" "$DRIVE_TYPE" "$DEVNAME" "$SUMMARY"
		get_free_coord $PINBOARD_FILE $ICON_ALIGNMENT # returnx COORD_X, COORD_Y
		rox_add_icon "$DRIVE_ICON_ROOT/$DRIVENAME" $COORD_X $COORD_Y "$(get_icon_label $DRIVENAME "$VOLABEL")" "click $DEVNAME"
		fatdog-drive-icon-refresh-icon.sh $DEVNAME		
	fi
}

### main ###
# remove icons
sed "\_${DRIVE_ICON_ROOT}_ !d; s_.*${DRIVE_ICON_ROOT}_${DRIVE_ICON_ROOT}_; s_</icon>__" $PINBOARD_FILE |
while read -r p; do
	rox_remove_icon $p
	rm -rf $p
done

# add icons
ls -v $UDEV_DRIVE_ICON_DEVICES | while read -r p; do action_add $p; done
