#!/bin/bash

# Generation of the FVWM-Crystal Desktop Manager (icons on the desktop)
# Written by Dominique Michel <dominique_libre@users.sourceforge.net>, 2015
# Generate the desktop manager in $[FVWM_USERDIR]/tmp/DesktopIcons.

# Syntax:
# PipeRead '$[FVWM_SYSTEMDIR]/scripts/desktop_manager $[infostore.DeskIconsSign] $[infostore.cs_transparent] $[infostore.SILENT] $[infostore.FileBrowser1] $[infostore.FileBrowser2] \
# $[vp.height] $[infostore.tmpdrivefile]'
# Include tmp/DesktopIcons

deskmanfile="${FVWM_USERDIR}/tmp/DesktopIcons"
echo "" > "${deskmanfile}"

# We must calculate the size of the button box {{{1
# Home: 1 icon, XDG user dirs: 8 icons, partitions: variable, directories: variable
IconNumber=1
if [[ "${ShowXDG}" == "yes" ]]; then IconNumber=9; fi
if [[ "${ShowPartitions}" == "yes" ]]
	then	gawk '/^\/.*|^simple-mtpfs.*\// { print $2; }' </proc/mounts |sort > ${7}
		IconNumber="$((${IconNumber}+`wc -l < ${7}`))"
		IconNumber="$((${IconNumber}+`wc -l < ${HOME}/.fvwm-crystal/preferences/ShowDirectories`))"
fi
IconRowsNumber="$((((${6}-((${EWMH_BS_Panel_Top}+${EWMH_BS_Panel_Bottom}))))/50))"
tmpvar=$((${IconNumber}/${IconRowsNumber}))
if [[ "$((${IconRowsNumber}*${tmpvar}))" != "${IconNumber}" ]]
	then IconColumnsNumber="$((${tmpvar}+1))"
	else IconColumnsNumber="${tmpvar}"
fi

# Preference menu
echo "DestroyMenu /Preferences/IconLauncher" >> "${deskmanfile}"
echo "AddToMenu /Preferences/IconLauncher" >> "${deskmanfile}"
if [ -f pmount-gui ]
	then echo "+ PMount \"Exec exec pmount-gui\"" >> "${deskmanfile}"
	     echo "+ PUMount \"Exec exec pmount-gui -u\"" >> "${deskmanfile}"; fi
if [[ "${ShowPartitions}" == "yes" ]]
then cat /etc/fstab|grep user|grep -v \#| awk '{print $2}'|while read line
do  	partition=${line}
	exist=0
	while read lines
		do if [ ${partition} = ${lines} ]
			then exist=1
		   fi
	done <${7}
	if [ ${exist} = 0 ]
		then echo "+ \"\$[gt.Mount] ${line}\" Exec exec mount ${line}" >> "${deskmanfile}"
	fi
    done
fi
echo "+ '\$[gt.XDG user directories]' PopUp /Preferences/XDG" >> "${deskmanfile}"
echo "+ '\$[gt.Mounted partitions]' PopUp /Preferences/Partitions" >> "${deskmanfile}"
echo "+ '\$[gt.Show directories]' LaunchFileWithHelp ShowDirectories" >> "${deskmanfile}"
echo "+ '\$[gt.Actions]' PopUp /Preferences/FileBrowser" >> "${deskmanfile}"
echo "+ '\$[gt.Emplacement]' Popup /Preferences/DeskIconsPlace" >> "${deskmanfile}"
echo "+ '\$[gt.Show on top with panels]' Popup /Preferences/DeskIconsTop" >> "${deskmanfile}"

echo "" >> "${deskmanfile}"

# The button {{{1
echo "DestroyFunc ShowDesktopIcons" >> "${deskmanfile}"
echo "AddToFunc ShowDesktopIcons" >> "${deskmanfile}"
#echo "+ I GetIconsNumber" >> "${deskmanfile}"
echo "+ I KillModule FvwmButtons DesktopIcons" >> "${deskmanfile}"
echo "+ I DestroyModuleConfig DesktopIcons: *" >> "${deskmanfile}"
if [ "$1" = "-" ]
	then echo "+ I *DesktopIcons: Geometry $((${IconColumnsNumber}*48))x$((${IconRowsNumber}*48))${1}+${EWMH_BS_Panel_Right}+${EWMH_BS_Panel_Top}" >> "${deskmanfile}"
	else echo "+ I *DesktopIcons: Geometry $((${IconColumnsNumber}*48))x$((${IconRowsNumber}*48))${1}+${EWMH_BS_Panel_Left}+${EWMH_BS_Panel_Top}" >> "${deskmanfile}"
fi
echo "+ I *DesktopIcons: Padding 1 1" >> "${deskmanfile}"
echo "+ I *DesktopIcons: BoxSize Fixed" >> "${deskmanfile}"
echo "+ I *DesktopIcons: Rows ${IconRowsNumber}" >> "${deskmanfile}"
echo "+ I *DesktopIcons: Columns ${IconColumnsNumber}" >> "${deskmanfile}"
echo "+ I *DesktopIcons: Font \"xft:${panel_font}:pixelsize=${panel_font_size}:${panel_font_style}\""  >> "${deskmanfile}"
echo "+ I *DesktopIcons: Colorset ${2}" >> "${deskmanfile}"
# Home
echo "+ I *DesktopIcons: (1x1+0+0, Padding 1 1, Frame 0, Icon 48x48/categories/Home.png, ActiveIcon 48x48/categories/Home.png.active.png, Action (Mouse 1) \"${4} ${HOME} ${3}\", Action (Mouse 2) \"${5} /home/`id -un` ${3}\", Action (Mouse 3) \"Menu /Preferences/IconLauncher\")" >> "${deskmanfile}"
# XDG user dirs
if [[ "${ShowXDG}" == "yes" ]]
	then xdgicons=""
		for name in DESKTOP DOWNLOAD TEMPLATES PUBLICSHARE DOCUMENTS MUSIC PICTURES VIDEOS
		do eval dir=\$$(echo XDG_"${name}"_DIR)
			echo "DestroyMenu /XDG${name}"
			echo "AddToMenu /XDG${name}"
			echo "+ ${dir} Nop"
			echo "+ I *DesktopIcons: (1x1, Padding 1 1, Frame 0, Icon 48x48/categories/${name}.png, ActiveIcon 48x48/categories/${name}.png.active.png, PressIcon 48x48/categories/${name}.png.active.png, Action (Mouse 1) \"${4} ${dir} ${3}\", Action(Mouse 2) \"${5} ${dir} ${3}\", Action (Mouse 3) \"Menu /XDG${name}\")" >> "${deskmanfile}"
			xdgicons="${xdgicons} ${dir}"
			y=++y
		done
		echo "InfoStoreAdd XDGIcons \"${xdgicons}\""
fi
# partitions
if [[ "${ShowPartitions}" == "yes" ]]
then partitions=""
	while read line
	do	echo "DestroyMenu /Partitions${line}"
		echo "AddToMenu /Partitions${line}"
		echo "+ ${line} Nop"
		echo "DesktopUmount ${line}"
		if [[ "${line}" == "/" ]]
		then 	icon="48x48/categories/Root.png"
		else 	if [[ "$(echo ${line} | grep -e cdrom -e dvd)" != "" ]]
			then 	icon="48x48/categories/CD.png"
			else	icon="48x48/categories/Work_4.png"
			fi
		fi
		echo "+ I *DesktopIcons: (1x1, Padding 1 1, Frame 0, Icon ${icon}, ActiveIcon ${icon}.active.png, PressIcon ${icon}.active.png, Action (Mouse 1) \"${4} ${line} ${3}\", Action (Mouse 2) \"${5} ${line} ${3}\", Action (Mouse 3) \"Menu /Partitions${line}\")" >> "${deskmanfile}"
      		partitions="${partitions} ${line}"
	done <${7}
      	echo "InfoStoreAdd PartitionModules \"${partitions}\""
# We don't want a PipeRead that will block FVWM every 5 seconds
pidof -x DesktopCheckMounts 1>2 2>/dev/null || echo "Exec exec ${FVWM_SYSTEMDIR}/scripts/DesktopCheckMounts 4"
else pidof -x DesktopCheckMounts 1>2 2>/dev/null && killall DesktopCheckMounts
fi
# directories
# To work with spaces into the directories names, you must quote them with single quotes in ${FVWM_USERDIR}/preferences/ShowDirectories
directories=""
while read line
do	echo "DestroyMenu /Directories${line}"
	echo "AddToMenu /Directories${line}"
	echo "+ ${line} Nop"
	icon="48x48/categories/Yellow_Drawer.png"
	echo "+ I *DesktopIcons: (1x1, Padding 1 1, Frame 0, ActiveTitle \"${line}\", Icon ${icon}, ActiveIcon ${icon}.active.png, PressIcon ${icon}.active.png, Action (Mouse 1) \"${4} ${line} ${3}\", Action (Mouse 2) \"${5} ${line} ${3}\", Action (Mouse 3) \"Menu /Directories${line}\")" >> "${deskmanfile}"
	directories="${directories} ${line}" >> "${deskmanfile}"
done <${FVWM_USERDIR}/preferences/ShowDirectories
echo "+ I Style DesktopIcons !Borders" >> "${deskmanfile}"
echo "+ I Module FvwmButtons DesktopIcons" >> "${deskmanfile}"

echo "ShowDesktopIcons" >> "${deskmanfile}"


# Laurent Gaudé - Eldorado
