#!/bin/sh

runwmlist="$(runwm --list)" || exit 1
[ -n "$runwmlist" ] || exit 1

if [ -d /etc/X11/sessions ]; then
	rm -f /etc/X11/sessions/*
	printf '%s\n' "$runwmlist" |while read n; do
		[ -n "$n" ] || continue
		icon=
		# Icon finding (optional)
		wmsessionfile=`grep -rl "$n" /etc/X11/wmsession.d/`
		if [ -e "$wmsessionfile" ]; then
			icon=`awk 'BEGIN {FS="="}; /ICON=/ {print $2}' "$wmsessionfile"`
		fi
		cat >"/etc/X11/sessions/$n.desktop" <<__EOF__
[Desktop Entry]
Encoding=UTF-8
Name=$n
Comment=$n session
Exec=/etc/X11/Xsession "$n"
Icon=$icon
Type=Application
__EOF__
	done
fi
