#!/bin/bash
#set -x
MNAME=conmen
USER=`id -un`
. $HOME/.bash_profile
setterm -blank 0
. /usr/lib/conmen/cmfunk
. $GLOBALRC

for FILE in /usr/lib/conmen/cmmain/*[!~] ; do
    . $FILE
done

. /usr/lib/conmen/xwindow/start_x_window

# Warn if the Xdialog wrapper is not installed.
if_no_wrapper

# To fix problems with sudo and quakeforge
export CMUSER=$USER

function read_menu_entries {

    # read menuentries from the "menu_entry" files in each directory
    # in /usr/lib/conmen

    if [ "$USER" != "root" ]; then

	grep -qe ${USER}-shell ${DBFILE}-${USER} 2> /dev/null || unset SHACK
	grep -qe ${USER}-reboot ${DBFILE}-${USER} 2> /dev/null || unset RCOMP
	grep -qe ${USER}-shutdown ${DBFILE}-${USER} 2> /dev/null || unset SCOMP

    fi

    case $1 in

	--user)		if [ "${USER}" = "root" ] || \
			grep -qe ${USER}-$2 ${DBFILE}-${USER} 2> /dev/null ; then

			    for FILE in ${CONLIBDIR}/$2/menu_entry ; do

				. ${FILE}

			    done

			fi
			;;

	--only-user)	if [ "${USER}" != "root" ] && \
			grep -qe ${USER}-$2 ${DBFILE}-${USER} 2> /dev/null ; then

			    . ${CONLIBDIR}/$2/menu_entry

			fi
			;;

	--only-root)	if [ "${USER}" = "root" ]; then

			    . ${CONLIBDIR}/$2/menu_entry
			fi
			;;

	--only-console)	if [ -z "${DISPLAY}" ]; then

			    . ${CONLIBDIR}/$2/menu_entry
			fi
			;;	    

    esac

}

function make_selections {

    if [ "${USER}" != "root" ]; then

	[ -f "${DBFILE}-${USER}" ] || \
	msgbox "There is no \"${DBFILE}\" for \"${USER}\", run conmen as root to configure the menu items"

    fi

    # function		option		directory
    read_menu_entries 	--user		cmcdrecording
    read_menu_entries 	--only-root	cmcfg
    read_menu_entries 	--user		cmdialup
    read_menu_entries 	--user		cmfloppy
    read_menu_entries 	--only-user	cmgames
    read_menu_entries 	--user		cmgraphics
    read_menu_entries 	--user		cmmain
    read_menu_entries 	--user		cmmp3
    read_menu_entries 	--user		cmprint
    read_menu_entries 	--user		cmsound
    read_menu_entries 	--user		cmrpm
    read_menu_entries 	--user		cmlog
    read_menu_entries 	--only-root	cmsystem
    read_menu_entries 	--user		cmuser
    read_menu_entries 	--only-console	xwindow

}

function select_entry {
    if [ "${MAINCHOICE}" = "$1" ]; then
	if [ "$#" -gt "2" ]; then
	    case $2 in
		--sudo	   ) [ "${USER}" = "root" ] && $3 || sudo $3;;
		--sudo-home) [ "${USER}" = "root" ] && $3 || sudo -H $3;;
	    esac
	else
	    $2
	fi
    fi
}

function main {
    $DIALOG $XOPTS --backtitle "ConMen ${VERSION} (C) 2000 Max Heijndijk" \
	    --title "MAIN MENU" --menu "" ${SIZE} "${XWIN[@]}" \
						"${GAMES[@]}" \
						"${SHACK[@]}" \
						"${FLOPMEN[@]}" \
						"${PRIMEN[@]}" \
						"${GRAPH[@]}" \
						"${DIAL[@]}" \
						"${CDREC[@]}" \
						"${MPMEN[@]}" \
						"${RPMMEN[@]}" \
						"${LOGMEN[@]}" \
						"${SYSMAIN[@]}" \
						"${SCFG[@]}" \
						"${UCFG[@]}" \
						"${CMCFG[@]}" \
						"${RCOMP[@]}" \
						"${SCOMP[@]}" \
    2> /tmp/${MNAME}.tmp.$$
    MAINRETVAL=$?
    MAINCHOICE=`cat /tmp/$MNAME.tmp.$$`
    rm -f /tmp/${MNAME}.tmp.$$ 2> /dev/null
    case ${MAINRETVAL} in

    0)	#function	hotkey	option		command
	select_entry 	"XW" 			start_x_window
	select_entry 	"MP" 			cmmp3
	select_entry 	"PR" 			cmprint
	select_entry 	"FM" 			cmfloppy
	select_entry 	"SA" 			shell_acces
	select_entry 	"GR" 			cmgraphics
	select_entry 	"CE" 			change_editor
	select_entry 	"AA" 			add_alias
	select_entry 	"CC" 			cmcfg
	select_entry 	"CU" 			cmuser
	select_entry 	"RC" 			"shutdown_box --reboot"
	select_entry 	"SC" 			"shutdown_box --shutdown"
	select_entry 	"DU"	--sudo		cmdialup
	select_entry 	"SO"	--sudo		cmsound
#	select_entry 	"GA"			cmgames
	select_entry 	"GA"	--sudo		cmgames
	select_entry 	"SM"	--sudo-home	cmsystem
	select_entry 	"SR"	--sudo-home	cmrpm
	select_entry 	"VL"	--sudo		cmlog
#	select_entry 	"CD"			cmcdrecording
	select_entry 	"CD"	--sudo-home	cmcdrecording
#	select_entry 	$A_1	--sudo-home	cmcdrecording
	;;
    1|255)clear;exit;;

esac
main
}

dialogrc

[ -f "${USERRC}" ] && . ${USERRC}
. ${GLOBALRC}
[ -f "${USERRC}" ] || show_license
saverc_user && . ${USERRC}

make_selections

main

exit 0
