#!/bin/sh -e
#
# Integration with debian menu system:
#
# If debian/menu file exists, save it to $TMP/usr/lib/menu/$PACKAGE
# If debian/menu-method file exists, save it to 
# $TMP/etc/menu-methods/$PACKAGE
#
# Also, add to postinst and postrm.

PATH=debian:$PATH:/usr/lib/debhelper
. dh_lib

for PACKAGE in $DH_DOPACKAGES; do
	TMP=`tmpdir $PACKAGE`
	menu=`pkgfile $PACKAGE menu`
	menu_method=`pkgfile $PACKAGE menu-method`

	if [ "$menu" ]; then
		if [ ! -d $TMP/usr/lib/menu ]; then
			doit "install -d $TMP/usr/lib/menu"
		fi
		doit "install -p -m644 $menu $TMP/usr/lib/menu/$PACKAGE"

		# Add the scripts if a menu-method file doesn't exist.
		# The scripts for menu-method handle everything these do, too.
		if [ ! "$menu_method" -a ! "$DH_NOSCRIPTS" ]; then
			autoscript "postinst" "postinst-menu"
			autoscript "postrm" "postrm-menu"
		fi
	fi

	if [ "$menu_method" ]; then
		if [ ! -d $TMP/etc/menu-methods ]; then
			doit "install -d $TMP/etc/menu-methods"
		fi
		doit "install -p $menu_method $TMP/etc/menu-methods/$PACKAGE"

		if [ ! "$DH_NOSCRIPTS" ]; then
			autoscript "postinst" "postinst-menu-method" "s/#PACKAGE#/$PACKAGE/"
			autoscript "postrm" "postrm-menu-method" "s/#PACKAGE#/$PACKAGE/"
		fi
	fi
done
