#!/bin/sh
 
 # g_menu
 # ======
 
 # This file is part of Gerolf Markup Shredder,
 # written by G. D. Brettschneider (1999-2006).

 GMSdateG_MENU=20060927

 # Prologue: ==================================================================

 # Not running:

 if [ "$GMS_SHELL" = "" ]; then
   echo "This is Gerolf Markup Shredder (g_menu)."
   exit
 fi 

 # Debug: ---------------------------------------------------------------------
   
 if [ "$GMS_DEBUG" = "X" -o "$GMS_DEBUG" = "Y" ]; then
   source "$GMS_SHELL/l_banner" -debug g_menu $1 $2 $3 $4
 elif  [ "$GMS_DEBUG" = "Z" ]; then
   echo " g_menu ($1) ($2) ($3) ($4) ($5) ($6) ($7) ($8) ($9)" >> "$Z"
 fi 
  
 # Chapters: ==================================================================
 
 # Not defined:
 
 if [ "$1" = "" ]; then source "$GMS_SHELL/l_banner" -no_action g_menu
 
 # Resize: --------------------------------------------------------------------
 
 elif [ $1 = -resize ]; then 
   export REPLY_OFFSET=52
   export REPLY_SIZE=23
   export REPLY_ITEMS=13
 
 # Build: --------------------------------------------------------------------- 
 
 elif [ $1 = -build ]; then
   if [ "$GMS_FILE" = "" ]; then
     source "$GMS_SHELL/l_banner" -upper -first g_menu -build
   else
     source "$GMS_SHELL/l_banner" -upper -file g_menu -build
   fi 
   source "$GMS_SHELL/g_menu" -resize
   source "$GMS_SHELL/l_box" -build
   source "$GMS_SHELL/l_menu" -build
   if  [ "$GMS_HOT" = "" ]; then GMS_HOT=Q; fi
   source "$GMS_SHELL/l_menu" -update $GMS_HOT $GMS_HOT g_menu -build
   export REPLY_MODULE=g_menu
   export REPLY_ACTION=-update
   export GMS_RECEIVE=1
   export GMS_HOT="Q"
   
 # Remove: -------------------------------------------------------------------- 
 
 elif [ $1 = -remove ]; then
   source "$GMS_SHELL/l_box" -remove 
 
 # Update: --------------------------------------------------------------------
 
 elif [ $1 = -update ]; then
 # Cold:
   if [ "$3" = "" ]; then GMS_COLD=$GMS_HOT; else GMS_COLD=$3; fi
 # Hot:
   if [ "$2" = "" ]; then
     source "$GMS_SHELL/l_banner" -no_hot g_menu $2
   else
     GMS_HOT="Q"
     case "$2" in
       "Q" | "q" | "1" ) GMS_HOT="Q" ;;
       "C" | "c" | "2" ) GMS_HOT="C" ;;
       "O" | "o" | "3" ) GMS_HOT="O" ;;
       "V" | "v" | "4" ) GMS_HOT="V" ;;
       "E" | "e" | "5" ) GMS_HOT="E" ;;
       "B" | "b" | "6" ) GMS_HOT="B" ;;
       "A" | "a" | "7" ) GMS_HOT="A" ;;
       "T" | "t" | "8" ) GMS_HOT="T" ;;
       "R" | "r" | "9" ) GMS_HOT="R" ;;
       "L" | "l" | "10" ) GMS_HOT="L" ;;
       "S" | "s" | "11" ) GMS_HOT="S" ;;
       "W" | "w" | "12" ) GMS_HOT="W" ;;
       "I" | "i" | "13" ) GMS_HOT="I" ;;
     esac 
   fi
 # Handle old, update new:
   if [ "$GMS_HOT" = "$GMS_COLD" ]; then
     source "$GMS_SHELL/g_menu" -handle $GMS_HOT called_by g_menu -update
   else
     source "$GMS_SHELL/l_menu" -update $GMS_HOT $GMS_COLD cd_by g_menu -update
   fi 
   export GMS_RECEIVE=1
   
 # Execute: -------------------------------------------------------------------
   
 elif [ $1 = -execute ]; then
   backup=$2
   source "$GMS_SHELL/l_box" -remove
   source "$GMS_SHELL/l_desk" -remove 
   # Run program: 
   if [ "$GMS_FOLDER" != "" -a "$GMS_FILE" != "" ]; then
     source "$GMS_SHELL/gms" $backup
   elif [ "$GMS_HOT" = "W" -o "$GMS_HOT" = "I" ]; then
     source "$GMS_SHELL/gms" $backup     
   else
     source "$GMS_SHELL/l_banner" -no_file g_menu
   fi 
   export REPLY_OFFSET=0 
   export REPLY_SIZE=0
     "$GMS_REPLY" -random 2 
   export GMS_RECEIVE=1
   export GMS_TEXTMODE=1
   export REPLY_MODULE=g_palet
   export REPLY_ACTION=-rebuild
   export GMS_RETURN=1
   unset backup
   
 # Handle: --------------------------------------------------------------------
   
 elif [ $1 = -handle ]; then
 
 # Analyse:   
   if [ $2 = A ]; then
     if [ "$GMS_FILE" = "" ]; then export GMS_FILE="_folder.err"; fi
     source "$GMS_SHELL/g_menu" -execute -A   
   
 # View, Edit, Browse, Typeset, Read, Write, Initialize:   
   elif [ $2 = V -o $2 = E -o $2 = B -o $2 = T -o \
     $2 = R -o $2 = W -o $2 = I ]; then
       source "$GMS_SHELL/g_menu" -execute $2
   
 # Quit:   
   elif [ $2 = Q ]; then
     source "$GMS_SHELL/l_menu" -update - Q
     export REPLY_MODULE=g_good
     export REPLY_ACTION=-build
     export GMS_RECEIVE=1
   
 # Create:   
   elif [ $2 = C ]; then 
     source "$GMS_SHELL/l_menu" -update - C
     source "$GMS_SHELL/l_menu" -update - O
     export GMS_LASTDIR=$PWD
     export GMS_FOLDER=$GMS_TEMPLATE
     export GMS_FILE=
     export GMS_RECEIVE=1
     if  [ -d "$GMS_FOLDER" ]; then cd "$GMS_FOLDER"; fi
     source "$GMS_SHELL/l_banner" -upper -first
     source "$GMS_SHELL/l_banner" -lower -folder
     export GMS_FILEBOX=" 1. Select a template to create a copy of "
       source "$GMS_SHELL/g_file" -build -template   
     export GMS_FILEBOX=
     
 # Open:
   
   elif [ $2 = O ]; then 
     source "$GMS_SHELL/l_menu" -update - O
     source "$GMS_SHELL/l_menu" -update - V      
     if [ "$GMS_FOLDER" = "$GMS_BINARIES" ]; then
       export GMS_FOLDER="$GMS_SETTING"
       export GMS_FILE=
       source "$GMS_SHELL/l_banner" -upper -first
       source "$GMS_SHELL/l_banner" -lower -folder
     fi
     if [ -d "$GMS_FOLDER" ]; then cd "$GMS_FOLDER"; fi
     export GMS_FILEBOX=" Change folder and open .htm* markup file "
       source "$GMS_SHELL/g_file" -build -open
     export GMS_FILEBOX=
   
 # Learn:   
   elif [ $2 = L ]; then
     export GMS_FOLDER="$GMS_TEMPLATE/handbook"
     export GMS_FILE="handbook.htm"
     export GMS_BASE="handbook"
     cd "$GMS_FOLDER"
     source "$GMS_SHELL/l_banner" -lower -folder
     source "$GMS_SHELL/l_banner" -upper -handbook
   # source "$GMS_SHELL/l_menu" -update B L
     source "$GMS_SHELL/l_menu" -update - L
     export REPLY_MODULE=g_palet
     export REPLY_ACTION=-rebuild
     export GMS_RETURN=1
   
 # Select:   
   elif [ $2 = S ]; then
     source "$GMS_SHELL/l_menu" -update - S
     source "$GMS_SHELL/l_menu" -update - W
     export REPLY_MODULE=g_select
     export REPLY_ACTION=-build
   
 # Not found:
   else
     source "$GMS_SHELL/l_banner" -no_hotkey g_menu $GMS_HOT

   fi
   
 # Not found: -----------------------------------------------------------------

 else
   source "$GMS_SHELL/l_banner" -no_action g_menu $1 
 fi
