#!/bin/sh
#	$NetBSD: ctwm_app_menu,v 1.1.2.3 2022/07/24 11:51:22 martin Exp $
OFS=$IFS
IFS='
'
printf 'menu "appmenu"\n'
printf '{\n'
printf '\t"Applications"\tf.title\n'
LOCALBASE=$(pkg_info -Q LOCALBASE pkg_install 2>/dev/null || echo /usr/pkg)
for app in $(find $LOCALBASE/share/applications -name '*.desktop')
do
	name=""
	exec=""
	terminal=""
	nodisplay=""
	while read line;
	do
		case $line in
			Name=*)
				if ! [ -n "$name" ];
				then
					name=$(printf '%s' "$line" | cut -c6- | tr -d '\r"')
				fi
			;;
			Exec=*)
				if ! [ -n "$exec" ];
				then
					exec=$(printf '%s' "$line" | cut -c6- | sed -e 's/ %.*//g' | tr -d '\r')
					if printf '%s' "$exec" | grep -q '"'; then
						# results in malformed config file, better way
						# to handle this...?
						nodisplay="true"
					fi
				fi
			;;
			Terminal=true)
				terminal="true"
			;;
			OnlyShowIn=*|NoDisplay=true)
				nodisplay="true"
			;;
		esac
	done < "$app"
	if [ -n "$nodisplay" ];
	then
		continue
	fi
	if [ -n "$name" -a -n "$exec" ];
	then
		if [ -n "$terminal" ];
		then
			printf '\t" %s" !"uxterm %s &" \n' "$name" "$exec"
		else
			printf '\t" %s" !"%s &" \n' "$name" "$exec"
		fi
	fi
done | sort
printf '}\n'
IFS=$OIFS
