dnl CF_WITH_DESKTOP_CATEGORY version: 4 updated: 2013/01/01 10:50:14
dnl ------------------------
dnl Taking into account the absence of standardization of desktop categories
dnl take a look to see whether other applications on the current system are
dnl assigned any/all of a set of suggested categories.
dnl
dnl $1 = program name
dnl $2 = case-pattern to match comparable desktop files to obtain category
dnl      This pattern may contain wildcards.
dnl $3 = suggested categories, also a case-pattern but without wildcards,
dnl      since it doubles as a default value for a shell case-statement.
dnl $4 = categories to use if no match is found on the build-machine for the
dnl      --with-desktop-category "auto" setting.
dnl
dnl The macro tells the configure script to substitute the $DESKTOP_CATEGORY
dnl value.
AC_DEFUN([CF_WITH_DESKTOP_CATEGORY],[
AC_REQUIRE([CF_DISABLE_DESKTOP])

if test -z "$desktop_utils"
then
	AC_MSG_CHECKING(for requested desktop-category)
	AC_ARG_WITH(desktop-category,
		[  --with-desktop-category=XXX  one or more desktop catgories or auto],
		[cf_desktop_want=$withval],
		[cf_desktop_want=auto])
	AC_MSG_RESULT($cf_desktop_want)

	if test "$cf_desktop_want" = auto
	then
		rm -rf conftest*
		cf_desktop_also=
		for cf_desktop_dir in  \
			/usr/share/app-install \
			/usr/share/applications
		do
			if test -d $cf_desktop_dir
			then
				find $cf_desktop_dir -name '*.desktop' | \
				while true
				do
					read cf_desktop_path
					test -z "$cf_desktop_path" && break
					cf_desktop_name=`basename $cf_desktop_path .desktop`
					case $cf_desktop_name in #(vi
					$1|*-$1|$2)
						CF_VERBOSE(inspect $cf_desktop_path)
						egrep '^Categories=' $cf_desktop_path | \
							tr ';' '\n' | \
							sed -e 's%^.*=%%' -e '/^$/d' >>conftest.1
						;;
					esac
				done
			fi
		done
		if test -s conftest.1
		then
			cf_desktop_last=
			sort conftest.1 | \
			while true
			do
				read cf_desktop_this
				test -z "$cf_desktop_this" && break
				case $cf_desktop_this in #(vi
				Qt*|GTK*|KDE*|GNOME*|*XFCE*|*Xfce*) #(vi
					;;
				$3)
					test "x$cf_desktop_last" != "x$cf_desktop_this" && echo $cf_desktop_this >>conftest.2
					;;
				esac
				cf_desktop_last=$cf_desktop_this
			done
			cf_desktop_want=`cat conftest.2 | tr '\n' ';'`
		fi
		if test -n "$cf_desktop_want"
		then
			if test "$cf_desktop_want" = auto
			then
				cf_desktop_want=
			else
				# do a sanity check on the semicolon-separated list, ignore on failure
				cf_desktop_test=`echo "$cf_desktop_want" | sed -e 's/[[^;]]//g'`
				test -z "$cf_desktop_test" && cf_desktop_want=
				cf_desktop_test=`echo "$cf_desktop_want" | sed -e 's/^.*;$/./g'`
				test -z "$cf_desktop_test" && cf_desktop_want=
			fi
		fi
		if test -z "$cf_desktop_want"
		then
			cf_desktop_want="ifelse([$4],,ifelse([$3],,[Application;],[`echo "$3" | sed -e 's/\*//g' -e 's/|/;/g' -e 's/[[;]]*$/;/g'`]),[$4])"
			CF_VERBOSE(no usable value found for desktop category, using $cf_desktop_want)
		fi
	fi
	DESKTOP_CATEGORY=`echo "$cf_desktop_want" | sed -e 's/[[ ,]]/;/g'`
	CF_VERBOSE(will use Categories=$DESKTOP_CATEGORY)
	AC_SUBST(DESKTOP_CATEGORY)
fi
])
