#!/bin/sh
#	$NetBSD: ctwm_app_menu,v 1.6 2022/07/24 07:38:15 nia Exp $
#
# Copyright (c) 2020-2022 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Nia Alarie.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
LOCALBASE=$(pkg_info -Q LOCALBASE pkg_install 2>/dev/null || echo /usr/pkg)
DESKTOPFILES=$(find $LOCALBASE/share/applications -name '*.desktop')
OFS=$IFS
IFS='
'
do_category()
{
	printf 'menu "%s"\n' "$1"
	printf '{\n'
	printf '\t"%s"\tf.title\n' "$1"
	for app in $DESKTOPFILES;
	do
		name=""
		exec=""
		terminal=""
		nodisplay=""
		category=$(grep -m 1 '^Categories=' "$app")
		case "$category" in
			*Audio*)
				if [ "$1" != "Multimedia" ]; then
					continue
				fi
			;;
			*Development*)
				if [ "$1" != "Programming" ]; then
					continue
				fi
			;;
			*Graphics*)
				if [ "$1" != "Graphics" ]; then
					continue
				fi
			;;
			*Game*)
				if [ "$1" != "Games" ]; then
					continue
				fi
			;;
			*Office*)
				if [ "$1" != "Office" ]; then
					continue
				fi
			;;
			*Network*)
				if [ "$1" != "Internet" ]; then
					continue
				fi
			;;
			*System*)
				if [ "$1" != "System" ]; then
					continue
				fi
			;;
			*Utility*)
				if [ "$1" != "Accessories" ]; then
					continue
				fi
			;;
			*)
				if [ "$1" != "Misc" ]; then
					continue
				fi
			;;
		esac
		while read line;
		do
			case $line in
				Name=*)
					if [ -z "$name" ];
					then
						name=$(printf '%s' "${line#Name=}" | tr -d '\r"')
					fi
				;;
				Exec=*)
					if [ -z "$exec" ];
					then
						exec=$(printf '%s' "${line#Exec=}" | sed -e 's/ %.*//g' | tr -d '\r')
						# results in malformed config file, better way
						# to handle this...?
						if printf '%s' "$exec" | grep -q '"'; then
							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" !"xterm -class UXTerm -e %s &" \n' "$name" "$exec"
			else
				printf '\t" %s" !"%s &" \n' "$name" "$exec"
			fi
		fi
	done | sort
	printf '}\n'
}

do_category Accessories
do_category Games
do_category Graphics
do_category Internet
do_category Multimedia
do_category Office
do_category Programming
do_category System
do_category Misc

IFS=$OIFS
