#!/bin/bash

[ ! -e ~/.config/autostart/zzzfm.desktop ] && exit # zzzfm desktop is disabled
[ -e ~/.config/zzzfm/particons_off ] && exit # desktop drive icons are disabled

. ~/.config/zzzfm/drive-labels 2>/dev/null

# define current application.desktop files and create/update drive.desktop files
chattr -i ~/.config/zzzfm/desktop0 # unlock desktop icons
mkdir -p /tmp/drive_icons
rm ~/Desktop/particon-*.desktop /tmp/newdesktop0 /tmp/drive_icons/* 2>/dev/null
grep -Ev -- 'rows=|particon-' ~/.config/zzzfm/desktop0 | sort -n > /tmp/olddesktop0 # application.desktop files for current icon layout

OLD=""
while read LINE; do
	LINE="${LINE##*/}"
	LINE="${LINE#particon-}"
	OLD="${OLD} ${LINE%.desktop}"
done < <(ls -1 ~/Desktop/particon-*.desktop 2>/dev/null)

ICON=""
NEW=""

while read TYPE NAME F3 F4 MP _; do

	if [ "$TYPE" = "disk" ]; then
		case "$NAME" in
		mmc*)
			ICON="/usr/share/pixmaps/puppy/flashcard.svg"
			;;
		*)
			if [ "$F3" = "usb" ]; then
				ICON="/usr/share/pixmaps/puppy/plug_usb.svg"
			else
				ICON="/usr/share/pixmaps/puppy/harddisk.svg"
			fi
			;;
		esac
	elif [ "$TYPE" = "part" -o "$TYPE" = "rom" -a -n "$ICON" -a "$F3" != "swap" ]; then
		[ "$DRIVE_LABELS" = "true" ] && LABEL="$F3 $F4" || LABEL=""
		[ "$TYPE" = "rom" ] && ICON="/usr/share/pixmaps/puppy/cdrom.svg"
		cp -f $ICON /tmp/drive_icons/${NAME}_icon.svg
		nICON=/tmp/drive_icons/${NAME}_icon.svg
		[ "`findmnt /mnt/${NAME}`" ] && sed -i 's%</svg>%<circle cx="12" cy="12" r="12" style="fill:#00ff00;stroke:#222222;stroke-width:2;fill-opacity:1.0;stroke-opacity:0.6"/></svg>%' $nICON
		[[ "$MP" = "/initrd/mnt/dev_save" ]] || [[ "$F4" = "/initrd/mnt/dev_save" ]] && sed -i 's%</svg>%<circle cx="12" cy="12" r="12" style="fill:#d04444;stroke:#222222;stroke-width:2;fill-opacity:1.0;stroke-opacity:0.6"/></svg>%' $nICON
		if [ -e ~/Desktop/.particon-${NAME}.desktop ]; then
			mv -f ~/Desktop/{.,}particon-${NAME}.desktop
		elif [ ! -e ~/Desktop/particon-${NAME}.desktop -a ! -L ~/Desktop/particon-${NAME}.desktop ]; then
			cat << EOF > ~/Desktop/particon-${NAME}.desktop
[Desktop Entry]
Encoding=UTF-8
Name=${NAME}\n${LABEL}
Icon=${nICON}
Exec=spacefm-open /dev/${NAME}
Terminal=false
Type=Application
EOF
			chmod 644 ~/Desktop/particon-${NAME}.desktop
		fi

		NEW="${NEW} ${NAME}"
	fi
done < <(lsblk -rno type,name,fstype,label,tran,mountpoint | tr -s ' ' | sed "s%\\\x20%-%g" | grep -E '^(disk|part|rom|initrd) ')

while read NAME; do
	mv -f ~/Desktop/{,.}particon-${NAME}.desktop
done < <(comm -23 <(echo -n "${OLD# }" | tr ' ' '\n' | sort) <(echo -n "${NEW# }" | tr ' ' '\n' | sort))

# define screen parameters
if [ ! -s ~/.config/zzzfm/row-calc ]; then
	particons-row                           # calculate screen parameters and position of grid row for drive partition icons
	sleep 1.0
fi
MAXCOL=$(cat ~/.config/zzzfm/col-max)       # maximum x-axis icons
MAXROW=$(cat ~/.config/zzzfm/row-max)       # last screen grid row
CALROW=$(cat ~/.config/zzzfm/row-calc)      # calculated drive row
[ -z "$CALROW" ] && CALROW=$MAXROW

SELROW=$(cat ~/.config/zzzfm/row-sel 2>/dev/null) # user-selected drive row
[ -z "$SELROW" ] && SELROW=$CALROW && \
echo "$SELROW" > ~/.config/zzzfm/row-sel
DIFF=$(($CALROW - $SELROW))                 # diff between calculated and user-selected drive row

# define screen position for drive icons
DPOS=0 NUM=0
while read NAME; do
	[ "$NUM" -gt "$MAXCOL" ] && \
	CALROW=$(($CALROW - 1))                 # define when no additional drive icons will fit on the selected grid row
	DPOS=$(($DPOS + $CALROW - $DIFF))       # assign drive icons to designated grid row (or wrap icon to preceding row)
	((NUM++))
	echo ${DPOS}=$NAME >> /tmp/newdesktop0  # prepend screen grid ID number to drive partition name
	DPOS=$(($DPOS + $DIFF))
done < <(cd ~/Desktop; ls -1 particon*.desktop)
cat /tmp/olddesktop0 /tmp/newdesktop0 > ~/.config/zzzfm/desktop0 # concatenate application and drive partition desktop files
sed -i "1i ~rows=$CALROW" ~/.config/zzzfm/desktop0
chattr +i ~/.config/zzzfm/desktop0          # lock desktop icons

# restart zzzfm desktop
zzzfmPID=$(pgrep -f "zzzfm --desktop")
[ -n "$zzzfmPID" ] && kill -9 $zzzfmPID
sleep 0.5
[[ $(pidof zzzfm) ]] && killall zzzfm       # ensure no persistent zzzfm PID
zzzfm --desktop &

exit
