#!/bin/sh
#
# This screen script allows the admin to present a console-based
# whiptail menu for the user to choose a screen script from
# available choices
#
# To use this script, specify it as the screen script to use and provide menu
# entries, for example:
#
# SCREEN_07=menu
# MENU_ITEM_01="Linux Server"
# MENU_COMMAND_01="ldm"
# MENU_ITEM_02="Windows Server A"
# MENU_COMMAND_02="rdesktop 192.168.0.253"
# MENU_ITEM_03="Windows Server B"
# MENU_COMMAND_03="rdesktop 192.168.0.252"
# MENU_TITLE="Please choose a desktop: "
# 
#
# Get the lts.conf entries, and assign them to shell
# variables.
#
. /usr/share/ltsp/ltsp_config

[ -z "${MENU_TITLE}" ] && MENU_TITLE="Please choose a desktop: "
[ -z "${MENU_ITEM_01}" ] && MENU_ITEM_01="Linux Server"
[ -z "${MENU_COMMAND_01}" ] && MENU_COMMAND_01="ldm"
    
################################################################

# Need to specify TERM
export TERM=linux

# Make RDP sessions return to menu
export RDP_DAEMON=False

# Make other sessions return to menu
export XINITRC_DAEMON=False

MENU_APPEND_PATH=${MENU_APPEND_PATH:-"/usr/share/ltsp/screen.d/"}
if [ "$MENU_APPEND_PATH" = "none" ]; then
    unset MENU_APPEND_PATH
fi

MENU_ITEMS_MAX=20
unset MENU_ITEMS
for i in $(seq -w 1 ${MENU_ITEMS_MAX}); do
    eval THIS_ITEM=\${MENU_ITEM_$i}
    if [ -n "${THIS_ITEM}" ]; then
        #MENU_ITEMS="${MENU_ITEMS} \"\${MENU_ITEM_$i}\" \"\${MENU_ITEM_$i}\""
        MENU_ITEMS="${MENU_ITEMS} \"$i\" \"\${MENU_ITEM_$i}\""
    fi
done

MENU_CHOICE=$(mktemp /tmp/.menu-XXXXXX)

unset CHOICE
while [ -z "$CHOICE" ]; do
    eval whiptail --menu \"$MENU_TITLE\" 0 0 ${MENU_LISTHEIGHT:-5} \
        --nocancel --notags \
        ${MENU_ITEMS} 2>${MENU_CHOICE}

    [ -r "${MENU_CHOICE}" ] && CHOICE=$(cat ${MENU_CHOICE}) && rm $MENU_CHOICE 

    if [ -n "${CHOICE}" ]; then
        eval exec \${MENU_APPEND_PATH}\${MENU_COMMAND_$CHOICE} 
    fi
done

