#!/bin/ksh
#######################################################
#
# amp.sh - configurable menu script
#
#######################################################
trap "" 1 2 3

#VARIABLES
AMPNAME="${1}"        # User name indentifier 
AMPDIR=/usr/bin/amp   # Directory for Config file
AMPCFG=ampusers       # Configuration file
AMPTITLE="\n\n\tAMP - Administration Menu Program\n" 
AMPLEVEL=${AMPLEVEL:-0}  
let "AMPLEVEL = AMPLEVEL + 1"
DT="`date +'%D - %T'`"
export AMPNAME DT AMPLEVEL 

# Get user menu info from config file
USRINFO=`awk 'BEGIN {FS = "\n"; RS = ""}
{ if ( $1 == NAME )
  { print $0 } 
}' NAME=$AMPNAME   ${AMPDIR}/${AMPCFG}`

# Determine total number of menu options
TOTLOPTS=`echo "$USRINFO" | awk 'BEGIN {FS = "\n"
RS = ""} { print NF }'`

# Load MENUPGM and MENUTEXT arrays 
CNT=2
MENU_NUMBR=1
while [ $MENU_NUMBR -lt $TOTLOPTS ]
do
  # MENUINFO - configuration file menu line
  MENUINFO=`echo "$USRINFO" | awk 'BEGIN {FS = "\n"
            RS = ""} {print $FLD}' FLD=$CNT`

  # MENUTEXT - menu option text string
  MENUTEXT[${MENU_NUMBR}]=`echo "$MENUINFO" | awk -F"~" '{print $1}'`

  # MENUPGM - program/script to execute 
  #           for menu option
  MENUPGM[${MENU_NUMBR}]=`echo "$MENUINFO" | awk -F"~" '{print $2}'`
  let "CNT = CNT + 1"
  let "MENU_NUMBR = MENU_NUMBR + 1"
done

# Set up Menu Prompts depending on 
#   number of menu options
if [ $TOTLOPTS -gt 9 ]
then 
  PROMPT="\n\tPlease enter option number (\"n\" for next page): \c"
else
  PROMPT="\n\tPlease enter option number: \c"
fi

# Set up exit method depending on level of 
#   menu structure
if [ $AMPLEVEL -eq 1 ] 
then
  EXITLINE="\tx) Exit\n"
  EXITCMD="trap 1 2 3; exit 0"
else 
  EXITLINE="\tx) Exit Sub-Menu\n"
  EXITCMD="exit 0"
fi

# Set up Menu Page depending on number 
#   of total menu options
MENUPAGE=0
let "TEMPVAR = TOTLOPTS - MENUPAGE"
if [ $TEMPVAR -lt 10 ] 
then
  MENUOPTIONS="$TEMPVAR"
else 
  MENUOPTIONS=10
fi

#
# Loop and present the menu options
#  until user chooses the exit menu option
#
while [ true ] 
do 
  clear
  echo $AMPTITLE
  echo $EXITLINE
  MENU_NUMBR=1
  # Start Menu Options number at 0 if  MENUPAGE > 10
  [ $MENUPAGE -ge 10 ] && MENU_NUMBR=0     
  let "PGM_NUMBR = MENU_NUMBR + MENUPAGE"
  # print menu options
  while [ $MENU_NUMBR -lt $MENUOPTIONS ]
  do 
    echo  "\t${MENU_NUMBR}) ${MENUTEXT[${PGM_NUMBR}]}"
    let "MENU_NUMBR = MENU_NUMBR + 1"
    let "PGM_NUMBR = PGM_NUMBR + 1"
  done  
  echo $PROMPT
  read ANSWER
  
  # Process menu choice response
  case $ANSWER in
    x|X|e|E|q|Q) eval $EXITCMD 
       ;;
    # Execute menu choice after 
    #   adding menu page offset
    [0-9]) let "PGM_NUMBR = ANSWER + MENUPAGE"
       eval ${MENUPGM[${PGM_NUMBR}]}
       ;;
    # Go to next menu page
    n|N) let "MENUPAGE = MENUPAGE + 10"
       # Set MENUPAGE back to 0 
       #   if greater than last option
       [ $MENUPAGE -gt $TOTLOPTS ] && MENUPAGE=0
       let "TEMPVAR = TOTLOPTS - MENUPAGE"
       if [ $TEMPVAR -lt 10 ] 
       then
         MENUOPTIONS=$TEMPVAR
       else 
         MENUOPTIONS=10
       fi
       ;;
    *) ;;
  esac
done

trap 1 2 3 
exit 0
