#!/bin/ash
# update drive icon based on whether it is mounted or unmounted
# (C) James Budiono 2012, 2015
# License: GNU GPL Version 3 or later
#
# Called with: $1-devname (with /dev/ prefix)
# Note: assumed that $USER and $DISPLAY points to the right user/display
#       (rox_set_icon_image will be run as $USER and $DISPLAY)
#

### configuration
. $BOOTSTATE_PATH # AUFS_ROOT
TEMPLATE_DRIVE_ICON_ROOT=/tmp/fatdog-drive-icons	# will be appended with $USER

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

############### helpers ###############
# $1-devname
get_mount_point() {
	dmdev=$(ls -l /dev/mapper/* 2> /dev/null | awk -v dmdev=${1##*-} '$6 == dmdev {print $10; exit; }')
	[ "$dmdev" ] || dmdev=$(ls -l /dev/mapper/* 2> /dev/null | awk -v dmdev="/${1##*/}$" '$NF ~ dmdev { print $(NF-2); exit; }')
	awk -v dev=$1 -v dmdev=$dmdev '$1 == dev || $1 == dmdev { print $2; exit; }' /proc/mounts
}

# $1-devname 
get_icon_dir() {
	icondir=$TEMPLATE_DRIVE_ICON_ROOT.$USER.$XSESSION_ID/${1##*/}	
	if ! [ -e $icondir ]; then	# may LVM - convert to actual dm-xxx name
		dmdev=$(ls -l /dev/mapper 2>/dev/null | awk -v dmdev=${1##*/} '$10 == dmdev {print "dm-" $6; exit;}')
		[ "$dmdev" ] || dmdev=$(ls -l /dev/mapper 2>/dev/null | awk -v dmdev=${1##*/} '$(NF-2) == dmdev {sub(/.*\//,"",$NF); print $NF; exit;}')
		icondir=$TEMPLATE_DRIVE_ICON_ROOT.$USER.$XSESSION_ID/$dmdev
	fi
}


############### main ###############
mountpoint=$(get_mount_point $1)
get_icon_dir ${1##*/} # returns icondir

iconfile=$icondir/.DirIcon
[ ! -e $iconfile ] && exit # continue	# don't do if our icon file is not there

if [ "$mountpoint" ]; then
	# mounted, add mounted status if not already added
	if ! grep -qE "<rect|<path" $iconfile 2> /dev/null; then
		xh=${HOTSPOT_X:-0} yh=${HOTSPOT_Y:-0}
		wh=${HOTSPOT_W:-12} hh=${HOTSPOT_H:-12} ph=${HOTSPOT_P:-2}
		xph=$((xh + ph)) xpwh=$((xh + ph + wh)) yph=$((yh + ph)) yphh=$((yh + ph + hh))		
		sed -i -e 's_</svg>__' $iconfile	
					
		# emblem originally designed for 96x96, we scale it for 12x12 hotspot
		# parameter: scaling 0.25, offset -3,-24, dimension is 24x24
		# with 0.25 scaling and 24x24 wxh (so wh & hh must be multiplied by 2)
		sx="$(awk "END{print $wh/(12*4),$hh/(12*4)}" /dev/null)"; sy=${sx#* }; sx=${sx% *}
		case $mountpoint in
			# system - don't allow to unmount, yellow colour / fatdog emblem
			$AUFS_ROOT*) cat >> $iconfile << EOF
<!-- <rect x="$xph" y="$yph" width="$wh" height="$hh" fill="#ffff00" stroke="black" stroke-width="2"/> -->
<svg x="$((xph-1))" y="$((yph-1))" width="$((wh*2))" height="$((hh*2))"><defs><linearGradient id="G"><stop offset="5%" stop-color="#CC8B35"/><stop offset="50%" stop-color="#E3AD2F"/><stop offset="95%" stop-color="#F0C727"/></linearGradient></defs><path transform="scale($sx,$sy) translate(-4,-24)" fill="url(#G)" stroke="black" stroke-width="2" d="M18 37l6 1-7 2zm-7-1c7-3 14-3 21 0m3-1l4 4-7-3zm7 11c-17 18-40 1-20-8m-8 8c0 4-5 6-8 2m42-22c17 1 25 3 36 14 3-6 0-10-8-10 9-10 20 6 12 16 2 4 3 7 3 11 0 8-5 33-8 36-3 2-12 2-12-1 2-3 3-6 3-10-5 3-8 4-14 5 0 2 0 3-2 4-3 1-12 2-12-1v-2h-3c-1 3-1 4-3 5-4 2-12 2-12-1 1-2 2-4 2-7-3-1-3.596-.626-6-2 1 2 1 4 0 6-2 2-13 4-13 1 2-3 4-7-2-20-2-3-4-8-4-13 0-2 0-5 1-9-1-2-1-3-1-5 0-3 3-5 6-7l1-1s-3-1-3-3c0-4 5-7 8-7s6 3 6 3c4-1 6-1 7-1h5c4-1 7-1 13-1zm-9 2c6-5 16 0 16 4 0 2-5 4-7 4s-1-5-6-5m9 11c0 9-10 16-21 16-6 0-9 0-13-2m7 27c-2-1-4-2-6-4m28 9c5 0 8 0 14-1m14-5c3-2 5-4 7-7m7-29c-2-3-3-5-4-6m-72-5c3-3 6-5 11-7m0 26c-3 1-8 4-11 1-2-3 0-4 4-5M6 48c1 2 3 4 5 5m-1-1l5-4m5 7c8 0 15 0 22-9m1 33c1 4 1 5 0 9M28 77c2 3 2 5 2 8"/></svg>
</svg>
EOF
			;;
			# normal - use green colour
			*)	cat >> $iconfile << EOF
<rect x="$xph" y="$yph" width="$wh" height="$hh" fill="#00ff00" stroke="black" stroke-width="2"/>
<line x1="$xph" y1="$yph" x2="$xpwh" y2="$yphh" stroke="black"/>
<line x1="$xph" y1="$yphh" x2="$xpwh" y2="$yph" stroke="black"/>
</svg>			
EOF
			;;
		esac
	fi
else
	# removed, remove mounted status if not already removed
	if grep -qF "<rect" $iconfile 2> /dev/null; then
		sed -i -e '/rect/ d; /line/ d; /path/ d' $iconfile
	fi		
fi
rox_set_icon_image $icondir $iconfile

