#!/bin/sh
 
 # g_prog
 # ======
 
 # This file is part of Gerolf Markup Shredder,
 # written by G. D. Brettschneider (1999-2006).
 
 GMSdateG_PROG=20060927
 
 # Prologue: ==================================================================
 
 # Not running:
 
 if [ "$GMS_SHELL" = "" ]; then
   echo "This is Gerolf Markup Shredder (g_prog)."
   exit
 fi 
 
 # Debug: ---------------------------------------------------------------------
 
 if [ "$GMS_DEBUG" = "X" -o "$GMS_DEBUG" = "Y" ]; then
   source "$GMS_SHELL/l_banner" -debug g_prog $1 $2 $3 $4
 elif  [ "$GMS_DEBUG" = "Z" ]; then
   echo " g_prog ($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_prog
   
 # Resize: --------------------------------------------------------------------
   
 elif [ $1 = -resize ]; then
   export REPLY_OFFSET=26
   export REPLY_SIZE=20
   export REPLY_ITEMS=7
   
 # 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_prog called_by g_prog -update
   else
     GMS_HOT="Q"
     case "$2" in
       "V" | "v" | "1" ) GMS_HOT="V";;
       "E" | "e" | "2" ) GMS_HOT="E";;
       "B" | "b" | "3" ) GMS_HOT="B";;
       "A" | "a" | "4" ) GMS_HOT="A";;
       "T" | "t" | "5" ) GMS_HOT="T";;
       "R" | "r" | "6" ) GMS_HOT="R";;
       "Q" | "q" | "7" | "" ) GMS_HOT="Q";;
     esac 
   fi
 # Handle old, update new:
   if [ "$GMS_HOT" = "$GMS_COLD" ]; then
     source "$GMS_SHELL/g_prog" -handle $GMS_HOT $GMS_COLD
   else 
   # Set programs:
     source "$GMS_SHELL/l_banner" -lower -select called_by g_prog -update
     if [ "$4" != "" ]; then
       case "$GMS_COLD" in
         "V" ) export GMS_VIEWER=$4;;
         "E" ) export GMS_EDITOR=$4;;
         "B" ) export GMS_BROWSER=$4;;
         "A" ) export GMS_ANALYST=$4;;
         "T" ) export GMS_TSETTER=$4;;
         "R" ) export GMS_READER=$4;;
       esac 
     fi
   # Normalize writing:
     if [ "$GMS_TSETTER" = "" ]; then export GMS_TSETTER=pdfetex
     elif [ "$GMS_TSETTER" = "PDFETEX" ]; then export GMS_TSETTER=pdfetex
     elif [ "$GMS_TSETTER" = "Pdfetex" ]; then export GMS_TSETTER=pdfetex
     elif [ "$GMS_TSETTER" = "pdfeTeX" ]; then export GMS_TSETTER=pdfetex
     elif [ "$GMS_TSETTER" = "PDFTEX" ]; then export GMS_TSETTER=pdftex
     elif [ "$GMS_TSETTER" = "Pdftex" ]; then export GMS_TSETTER=pdftex
     elif [ "$GMS_TSETTER" = "pdfTeX" ]; then export GMS_TSETTER=pdftex
     elif [ "$GMS_TSETTER" = "ETEX" ]; then export GMS_TSETTER=etex
     elif [ "$GMS_TSETTER" = "Etex" ]; then export GMS_TSETTER=etex
     elif [ "$GMS_TSETTER" = "eTeX" ]; then export GMS_TSETTER=etex
     elif [ "$GMS_TSETTER" = "TEX" ]; then export GMS_TSETTER=tex
     elif [ "$GMS_TSETTER" = "Tex" ]; then export GMS_TSETTER=tex
     elif [ "$GMS_TSETTER" = "TeX" ]; then export GMS_TSETTER=tex
     fi
   # Update program box:
     source "$GMS_SHELL/l_prog" -update $GMS_HOT $GMS_COLD # $4 
   fi
   export GMS_RECEIVE=1
 
 # Build: ---------------------------------------------------------------------
 
 elif [ $1 = -build ]; then 
   source "$GMS_SHELL/l_banner" -lower -program called_by g_prog -build
   source "$GMS_SHELL/g_prog" -resize called_by g_prog -build
   source "$GMS_SHELL/l_box" -t11_build called_by g_prog -build
   source "$GMS_SHELL/l_prog" -build called_by g_prog -build
   source "$GMS_SHELL/l_prog" -update Q Q called_by g_prog -build
   export REPLY_MODULE=g_prog
   export REPLY_ACTION=-update
   export GMS_RECEIVE=1
   export GMS_HOT="Q"
 
 # Remove: --------------------------------------------------------------------
 
 elif [ $1 = -remove ]; then 
   source "$GMS_SHELL/g_prog" -resize called_by g_prog -remove
   source "$GMS_SHELL/l_box" -t12_remove called_by g_prog -remove 
   source "$GMS_SHELL/g_select" -resize called_by g_prog -remove
   source "$GMS_SHELL/g_select" -update P Q called_by g_prog -remove
   export REPLY_MODULE=g_select
   export REPLY_ACTION=-update
   export GMS_RECEIVE=1 
   
 # Handle: --------------------------------------------------------------------
   
 elif [ $1 = -handle ]; then
   
 # Quit:   
   if [ $2 = Q ]; then
     source "$GMS_SHELL/g_launch" -build called_by g_prog -handle
     source "$GMS_SHELL/g_prog" -remove called_by g_prog -handle
   
 # Viewer:   
   elif [ $2 = V ]; then
     export GMS_RECEIVE=1
     source "$GMS_SHELL/l_banner" -lower -viewer called_by g_prog -handle
     source "$GMS_SHELL/l_prog" -update - V called_by g_prog -handle
     setterm -cursor on
       "$GMS_REPLY" -question 8 "$GMS_VIEWER" VV 1
     setterm -cursor off 
   
 # Editor:   
   elif [ $2 = E ]; then
     export GMS_RECEIVE=1
     source $GMS_SHELL/l_banner -lower -editor called_by g_prog -handle
     source $GMS_SHELL/l_prog -update - E called_by g_prog -handle
     setterm -cursor on
       $GMS_REPLY -question 10 "$GMS_EDITOR" EE 2
     setterm -cursor off 
   
 # Browser:   
   elif [ $2 = B ]; then
     export GMS_RECEIVE=1
     source "$GMS_SHELL/l_banner" -lower -browser called_by g_prog -handle
     source "$GMS_SHELL/l_prog" -update - B called_by g_prog -handle
     setterm -cursor on
       "$GMS_REPLY" -question 12 "$GMS_BROWSER" BB 3
     setterm -cursor off 
   
 # Analyst:   
   elif [ $2 = A ]; then
     export GMS_RECEIVE=1
     source "$GMS_SHELL/l_banner" -lower -analyst called_by g_prog -handle
     source "$GMS_SHELL/l_prog" -update - A called_by g_prog -handle
     setterm -cursor on
       "$GMS_REPLY" -question 14 "$GMS_ANALYST" AA 4
     setterm -cursor off 
   
 # Typesetter:   
   elif [ $2 = T ]; then
     export GMS_RECEIVE=1
     source "$GMS_SHELL/l_banner" -lower -tsetter called_by g_prog -handle
     source "$GMS_SHELL/l_prog" -update - T called_by g_prog -handle
     setterm -cursor on
       "$GMS_REPLY" -question 16 "$GMS_TSETTER" TT 5
     setterm -cursor off 
   
 # Reader:   
   elif [ $2 = R ]; then
     export GMS_RECEIVE=1
     source "$GMS_SHELL/l_banner" -lower -reader called_by g_prog -handle
     source "$GMS_SHELL/l_prog" -update - R called_by g_prog -handle
     setterm -cursor on
       "$GMS_REPLY" -question 18 "$GMS_READER" RR 6
     setterm -cursor off 
   
 # Not found: 
   else
     source "$GMS_SHELL/l_banner" -no_hotkey g_prog $2   
   fi
   
 # Not found: -----------------------------------------------------------------
 
 else
   source "$GMS_SHELL/l_banner" -no_action g_prog $1   
 fi
