#!/bin/sh
 
 # g_save
 # ======
 
 # This file is part of Gerolf Markup Shredder,
 # written by G. D. Brettschneider (1999-2006).
 
 GMSdateG_SAVE=20060927
 
 # Prologue: ==================================================================
 
 # Not running:
 
 if [ "$GMS_SHELL" = "" ]; then
   echo "This is Gerolf Markup Shredder (g_save)."
   exit
 fi 
 
 # Debug: ---------------------------------------------------------------------
  
 if [ "$GMS_DEBUG" = "X" -o "$GMS_DEBUG" = "Y" ]; then
   source "$GMS_SHELL/l_banner" -debug g_save $1 $2 $3 $4
 elif  [ "$GMS_DEBUG" = "Z" ]; then
   echo " g_save ($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_save
  
 # Resize: --------------------------------------------------------------------
  
 elif [ $1 = -resize ]; then
  export REPLY_OFFSET=3
  export REPLY_SIZE=44
  export REPLY_ITEMS=4
  
 # Build: ---------------------------------------------------------------------
 
 elif [ $1 = -build ]; then 
   source "$GMS_SHELL/l_banner" -lower -folder 
   export REPLY_LIST=0
   export GMS_FILE="New$$.htm"
   source "$GMS_SHELL/g_save" -resize
   source "$GMS_SHELL/l_box" -t8_build
   GMS_FILEBOX=" 3. Enter a better name for this new file "
     source "$GMS_SHELL/l_save" -build
   GMS_FILEBOX=
 # Consider existing file with same name:
   GMS_EXIST=0
     if [ -f "$GMS_FOLDER/$GMS_FILE" ]; then GMS_EXIST=1; fi   
     source "$GMS_SHELL/l_save" -update Q Q $GMS_EXIST
   GMS_EXIST=
   export REPLY_MODULE=g_save
   export REPLY_ACTION=-update
   export GMS_RECEIVE=1
   export GMS_HOT="Q"
 
 # Remove: --------------------------------------------------------------------
 
 elif [ $1 = -remove ]; then
   source "$GMS_SHELL/g_save" -resize
   source "$GMS_SHELL/l_box" -t8_remove
   source "$GMS_SHELL/g_menu" -resize
   source "$GMS_SHELL/l_menu" -update C Q
   export REPLY_MODULE=g_menu
   export REPLY_ACTION=-update
   export GMS_RECEIVE=1
  
 # 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_save
   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";;
       "N" | "n" | "4" ) GMS_HOT="N";;
     esac
   fi
 # Handle old, update new:
   if [ "$GMS_HOT" = "$GMS_COLD" ]; then
     source "$GMS_SHELL/g_save" -handle $GMS_HOT
 # Fixme for spacy names like DOS version
   else
     if [ "$4" != "" ]; then
       export GMS_FILE="$4"
     fi 
   # Update save box (and consider existing file with same name):
     GMS_EXIST=0
       if [ -f "$GMS_FOLDER/$GMS_FILE" ]; then GMS_EXIST=1; fi   
       source "$GMS_SHELL/l_save" -update $GMS_HOT $GMS_COLD $GMS_EXIST
     GMS_EXIST=
   fi
   export GMS_RECEIVE=1
 
 # Handle: --------------------------------------------------------------------
  
 elif [ $1 = -handle ]; then
 
 # Overwrite (copy template):  
   if [ $2 = "" -o $2 = O ]; then
     if [ -f "$GMS_TFOLDER/$GMS_TEMPLATEFILE" ]; then
       cp "$GMS_TFOLDER/$GMS_TEMPLATEFILE" "$GMS_FOLDER/$GMS_FILE"
     fi
     source "$GMS_SHELL/l_banner" -upper -file
     source "$GMS_SHELL/g_save" -remove
     if [ -d "$GMS_FOLDER" ]; then cd "$GMS_FOLDER"; fi
  
 # Quit:  
   elif [ $2 = Q ]; then
     if [ -f "$GMS_FOLDER/$GMS_FILE" ]; then
       source "$GMS_SHELL/l_banner" -upper -file
       source "$GMS_SHELL/g_save" -remove
     else
       source "$GMS_SHELL/g_save" -handle O
     fi
  
 # Cancel:  
   elif [ $2 = C ]; then
     source "$GMS_SHELL/l_banner" -upper -file
     source "$GMS_SHELL/g_save" -remove
  
 # Name:  
   elif [ $2 = N ]; then idle=  
  
 # Not found: 
   else
     source "$GMS_SHELL/l_banner" -no_hotkey g_save $2
  
 fi
  
 # Not found: -----------------------------------------------------------------
 
 else
  source "$GMS_SHELL/l_banner" -no_action g_save $1  
 fi
