#!/bin/csh -f
#
#  make-olwm-menu
#
# Script to generate an ol[v]wm menu based upon what modules
# a user has loaded.  It is expected that this script will
# be used in conjunction with the Modules1.0 package written
# by John Furlani <john.furlani@East.Sun.com> available from
# ftp.cme.nist.gov via anonymous ftp.
#
# This script was written as part of the Auburn University
# College of Engineering user-setup suite of environment
# configuration tools.  These tools are in the care of
# Richard Elling <richard.elling@eng.auburn.edu> and will be
# available for anonymous ftp from ftp.eng.auburn.edu.
#
# History:
#  2/22/93	2.5	Richard Elling	beautification changes
#  2/02/93	2.4	Richard Elling	converted to Modules2.0
#  9/16/92	2.3	Richard Elling	converted to SVR4
#  1/30/92	2.2	Richard Elling	converted exclusively to Modules1.0
#
# @(#)make-olwm-menu	2.5	22 Feb 1993	Auburn University Engineering

set path = (/usr/ucb /usr/bin /usr/local/bin)
set PROGNAME = `basename $0`
if ($?LOADEDMODULES == 0) then
	cat << EOT
ERROR: ${PROGNAME}: no modules loaded
You must be running in the Modules Package environment.
EOT
	exit (1)
endif
set APPS = ( `echo $_LMFILES_ | tr ':' ' '` )
set VERSION = "2.5"
set DATE = `date`
set TMPFILE = /tmp/${PROGNAME}.$$
set OPENWIN_MENU = ~/.openwin-menu
set OPENWIN_MENU_BAK = ~/.openwin-menu.bak
set USERNAME = `whoami`
set FULLNAME = `ypcat passwd | egrep -e "^${USERNAME}:" | cut -d: -f5 | cut -d, -f1`
set PERSONAL_MENU = ~/.personal-menu
set TITLE_SUFFIX = title
set MENU_SUFFIX = menu
set BASE_SUFFIX = base-menu
set FOUND_BASE = ""

onintr cleanup

# prompt user for okey dokey to start
cat << EOT

Hello.  I am ready to customize your OpenLook menu based upon the
modules you have loaded.  The menu will be custom tailored to the
applications you've selected.  We hope this will help make the
system easier to use.

Feel free to run this program anytime to update your menu.

EOT

set ANSWER = `ckyorn -p "Do you want to update your menu now" | tr '[A-Z]' '[a-z]'`
if ($status) exit (1)
if ($ANSWER != "yes" && $ANSWER != "y") exit (0)

echo "Creating new menu..."

cat << EOT > $TMPFILE
################################################################################
##	.openwin-menu file for $USERNAME ($FULLNAME)
##	Created automatically by the program $PROGNAME version $VERSION
##	on $DATE
################################################################################
##
##	The .openwin-menu file contains the list of commands available
##	from the background menu in OpenWindows or X11 under the OpenLook
##	window manager.  This file is automatically generated by the
##	program $PROGNAME.  If you want to add your own personal goodies
##	to the menu then add them in the file $PERSONAL_MENU
##	See the man page on olwm (or olvwm) for more information on menus.
##
##	NOTE: if, for some reason, there is an error in this file or any
##	sourced menu files, the window manager will use a system default
##	menu.
##
################################################################################
"$FULLNAME's Menu"	TITLE
EOT

# if the user has a personal menu file, use it
if ( -f $PERSONAL_MENU ) then
	echo "Adding $FULLNAME's personal menu..."
	cat << EOT >> $TMPFILE
"Personal"	MENU
EOT
	cat $PERSONAL_MENU >> $TMPFILE
	cat << EOT >> $TMPFILE
"Personal"	END	PIN
EOT
endif

cat << EOT >> $TMPFILE

"Applications"	MENU
EOT

## go through each loaded modulefile and add the menu for
## the application
foreach i ( $APPS)
	## check for normal app menus first
	if ( -r $i.$TITLE_SUFFIX && -r $i.$MENU_SUFFIX ) then
		echo "Adding menu for modulefile: $i"
		set title = `head -1 $i.$TITLE_SUFFIX`
		echo "Applications menu entry: $title"
		cat << EOT >> $TMPFILE
	"$title"	MENU
EOT
		cat $i.$MENU_SUFFIX >> $TMPFILE
		cat << EOT >> $TMPFILE
	"$title"	END	PIN
EOT
	endif

	## check for base menus second
	## Note that there should only be one base-menu and it
	## will be added at the end.
	if ( -r $i.$BASE_SUFFIX ) then
		set FOUND_BASE = $i.$BASE_SUFFIX
	endif
end

cat << EOT >> $TMPFILE
"Applications"	END	PIN
EOT

## make sure a base menu was found
if ( $FOUND_BASE == "" ) then
	cat << EOT
ERROR: ${PROGNAME}: no base menu found.
No changes have been made to $OPENWIN_MENU
Are you sure you have a window system module loaded?
EOT
	exit (1)
endif

## now add the rest of the base-menu
echo "Adding base menu: $FOUND_BASE"
cat $FOUND_BASE >> $TMPFILE

# the new menu is ready.  If there exists an $OPENWIN_MENU
# then back it up first.
if ( -f $OPENWIN_MENU ) then
	echo "Renaming your existing .openwin-menu to $OPENWIN_MENU_BAK"
	mv -f $OPENWIN_MENU $OPENWIN_MENU_BAK
endif

cp $TMPFILE $OPENWIN_MENU

cleanup:
echo "${PROGNAME}: removing temporary file."
rm -f $TMPFILE

exit (0)
