xref: /netbsd-src/external/mit/ctwm/libexec/ctwm_app_menu (revision 469d372e5b024fc3d5e1247e573ebfdbdcca39e7)
1#!/bin/sh
2#	$NetBSD: ctwm_app_menu,v 1.6 2022/07/24 07:38:15 nia Exp $
3#
4# Copyright (c) 2020-2022 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Nia Alarie.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31LOCALBASE=$(pkg_info -Q LOCALBASE pkg_install 2>/dev/null || echo /usr/pkg)
32DESKTOPFILES=$(find $LOCALBASE/share/applications -name '*.desktop')
33OFS=$IFS
34IFS='
35'
36do_category()
37{
38	printf 'menu "%s"\n' "$1"
39	printf '{\n'
40	printf '\t"%s"\tf.title\n' "$1"
41	for app in $DESKTOPFILES;
42	do
43		name=""
44		exec=""
45		terminal=""
46		nodisplay=""
47		category=$(grep -m 1 '^Categories=' "$app")
48		case "$category" in
49			*Audio*)
50				if [ "$1" != "Multimedia" ]; then
51					continue
52				fi
53			;;
54			*Development*)
55				if [ "$1" != "Programming" ]; then
56					continue
57				fi
58			;;
59			*Graphics*)
60				if [ "$1" != "Graphics" ]; then
61					continue
62				fi
63			;;
64			*Game*)
65				if [ "$1" != "Games" ]; then
66					continue
67				fi
68			;;
69			*Office*)
70				if [ "$1" != "Office" ]; then
71					continue
72				fi
73			;;
74			*Network*)
75				if [ "$1" != "Internet" ]; then
76					continue
77				fi
78			;;
79			*System*)
80				if [ "$1" != "System" ]; then
81					continue
82				fi
83			;;
84			*Utility*)
85				if [ "$1" != "Accessories" ]; then
86					continue
87				fi
88			;;
89			*)
90				if [ "$1" != "Misc" ]; then
91					continue
92				fi
93			;;
94		esac
95		while read line;
96		do
97			case $line in
98				Name=*)
99					if [ -z "$name" ];
100					then
101						name=$(printf '%s' "${line#Name=}" | tr -d '\r"')
102					fi
103				;;
104				Exec=*)
105					if [ -z "$exec" ];
106					then
107						exec=$(printf '%s' "${line#Exec=}" | sed -e 's/ %.*//g' | tr -d '\r')
108						# results in malformed config file, better way
109						# to handle this...?
110						if printf '%s' "$exec" | grep -q '"'; then
111							nodisplay="true"
112						fi
113					fi
114				;;
115				Terminal=true)
116					terminal=true
117				;;
118				OnlyShowIn=*|NoDisplay=true)
119					nodisplay=true
120				;;
121			esac
122		done < "$app"
123		if [ -n "$nodisplay" ];
124		then
125			continue
126		fi
127		if [ -n "$name" -a -n "$exec" ];
128		then
129			if [ -n "$terminal" ];
130			then
131				printf '\t" %s" !"xterm -class UXTerm -e %s &" \n' "$name" "$exec"
132			else
133				printf '\t" %s" !"%s &" \n' "$name" "$exec"
134			fi
135		fi
136	done | sort
137	printf '}\n'
138}
139
140do_category Accessories
141do_category Games
142do_category Graphics
143do_category Internet
144do_category Multimedia
145do_category Office
146do_category Programming
147do_category System
148do_category Misc
149
150IFS=$OIFS
151