#!/bin/sh
 
 # g_vars
 # ======
 
 # This file is part of Gerolf Markup Shredder,
 # written by G. D. Brettschneider (1999-2006).
 
 GMSdateG_VARS=20060927
 
 # Prologue: ==================================================================
 
 # Not running:
 
 if [ "$GMS_SHELL" = "" ]; then
   echo "This is Gerolf Markup Shredder (g_vars)."
   exit
 fi
  
 # Debug: ---------------------------------------------------------------------
   
 if [ "$GMS_DEBUG" = "X" -o "$GMS_DEBUG" = "Y" ]; then
   source "$GMS_SHELL/l_banner" -debug g_vars $1 $2 $3 $4
 elif  [ "$GMS_DEBUG" = "Z" ]; then
   echo " g_vars ($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_vars
   
 # Initialize file and folders: -----------------------------------------------

 elif [ "$1" = "-ini_file" ]; then
   arg="############################### New work file: #######################"
     if [ "$GMS_DEBUG" = "Z" -a "$GMS_MODE" != "quiet" ]; then 
       echo " $arg########" >> "$Z"; fi
   unset arg
   # Dismiss -ini_file and caller(s):
   if [ "$2" = "called_by" ]; then shift; fi
   if [ "$1" = "called_by" ]; then shift; fi
   if [ "$2" = "called_by" ]; then shift; fi
   if [ "$1" = "called_by" ]; then shift; fi
   if [ "$2" = "-passive" ]; then shift; fi
   if [ "$1" = "-passive" ]; then shift; fi
   if [ "$2" = "-a" ]; then shift; fi
   if [ "$2" = "-t" ]; then shift; fi
   shift
   export GMS_RETURN=  
   # Now $1 ... $9 can be a file name:
   if [ "$1" != "" ]; then
     # $1 ... $9 may include path, so get filename and pathname: ..............
     BACKUP_FILE="$GMS_FILE"
       BACKUP_FOLDER="$GMS_FOLDER"
         export GMS_FILE="$1"
           if [ "$2" != "" ]; then export GMS_FILE="$GMS_FILE $2"; fi
           if [ "$3" != "" ]; then export GMS_FILE="$GMS_FILE $3"; fi
           if [ "$4" != "" ]; then export GMS_FILE="$GMS_FILE $4"; fi
           if [ "$5" != "" ]; then export GMS_FILE="$GMS_FILE $5"; fi
           if [ "$6" != "" ]; then export GMS_FILE="$GMS_FILE $6"; fi
           if [ "$7" != "" ]; then export GMS_FILE="$GMS_FILE $7"; fi
           if [ "$8" != "" ]; then export GMS_FILE="$GMS_FILE $8"; fi
           if [ "$9" != "" ]; then export GMS_FILE="$GMS_FILE $9"; fi
           export GMS_FOLDER="$GMS_FILE"
           # Get file name if $GMS_FILE contains path name:
           export file="$(basename "$GMS_FILE")" # remove dirname
         export GMS_FILE=$file           
           # Get path name if $GMS_FOLDER contains file name:
           export path_="$(dirname "$GMS_FOLDER")" # remove filename
           export GMS_FOLDER="$BACKUP_FOLDER"
         export GMS_FILE="$BACKUP_FILE"
       unset BACKUP_FOLDER
     unset BACKUP_FILE 
     # Check if file is in current folder: ....................................
     if [ -f "$PWD/$file" ]; then
       if [ "$GMS_DEBUG" = "Z" -a "$GMS_MODE" != "quiet" ]; then 
         echo "   File is in current folder:" >> "$Z"; fi
       export GMS_RETURN=1
       export GMS_FILE="$file"
       export GMS_FOLDER="$PWD"
       export GMS_MESSAGE="$file"
       cd "$GMS_FOLDER"
       export GMS_FOLDER="$PWD"
       export file=
       export path_=
       source "$GMS_SHELL/g_vars" -write_memo called_by g_vars -ini_file
     fi
     # Check if file is in a related folder: ..................................
     if [ -f "$PWD/$path_/$file" ]; then
       if "$GMS_DEBUG" = "Z" -a "$GMS_MODE" != "quiet" ]; then
         echo "   File is in a related folder:" >> "$Z"; fi
       export GMS_RETURN=1
       export GMS_FILE="$file"
       export GMS_FOLDER="$PWD/$path_"
       export GMS_MESSAGE="$file"
       cd "$GMS_FOLDER"
       export GMS_FOLDER="$PWD"
       export file=
       export path_=
       source "$GMS_SHELL/g_vars" -write_memo called_by g_vars -ini_file
     fi
     # Check if file is at a remote place: ....................................
     if [ -f "$path_/$file" ]; then
       if [ "$GMS_DEBUG" = "Z" -a "$GMS_MODE" != "quiet" ]; then
         echo "   File is in a remote folder:" >> "$Z"; fi
       export GMS_RETURN=1
       export GMS_FILE="$file"
       export GMS_FOLDER="$path_"
       export GMS_MESSAGE="$file"
       export PWD="$path_"
       if [ "$GMS_DEBUG" = "Z" -a "$GMS_MODE" != "quiet" ]; then 
         echo "  PWD=$PWD" >> "$Z"; fi
       export file=
       export path_=
       source "$GMS_SHELL/g_vars" -write_memo called_by g_vars -ini_file
     fi
   fi
   export file=
   export path_=
   # If no file is given, get last one and folder from memo: ..................
   if [ "$GMS_FILE" = "" ]; then
     if [ "$GMS_DEBUG" = "Z" ]; then
       echo "   Reading file and folder from gms_memo:" >> "$Z"
     fi
     if [ -f "$GMS_SETTING/gms_memo" ]; then
       source "$GMS_SETTING/gms_memo"; fi
     # Fixme: Set GMS_MESSAGE, if file is in a remote folder
     if [ -d "$GMS_FOLDER" ]; then cd "$GMS_FOLDER"; fi
     export GMS_FOLDER="$PWD"
     export GMS_RETURN=
   fi
   # If no folder is present, set to current folder: ..........................
   if [ "$GMS_FOLDER" = "" ]; then
     if [ "$GMS_DEBUG" = "Z" ]; then
       echo "   File is in current folder:" >> "$Z"
     fi
     export GMS_FOLDER="$PWD"
     export GMS_RETURN=
   fi
   if [ "$GMS_DEBUG" = "Z" ]; then
     echo "   GMS_FILE=$GMS_FILE" >> "$Z"
     echo "   GMS_FOLDER=$GMS_FOLDER" >> "$Z"
   fi
   arg="######################################################################"
     if [ "$GMS_DEBUG" = "Z" ]; then echo " $arg########" >> "$Z"; fi
   unset arg

 # Write file name and folder to gms_memo.bat startup file: -------------------

 elif [ "$1" = "-write_memo" ]; then
   if [ -d "$GMS_FOLDER" ]; then cd "$GMS_FOLDER"; fi
   str="$GMS_SETTING/gms_memo"
     echo "#!/bin/sh" > "$str"
     echo "export GMS_FOLDER=\"$GMS_FOLDER\"" >> "$str"
     echo "export GMS_FILE=\"$GMS_FILE\"" >> "$str"
   unset str
  
 # Clear environment variables: -----------------------------------------------
 
 elif [ $1 = -clear ]; then
 
   source "$GMS_SHELL/g_list" -clear called_by g_vars -clear

   # Build new gmsdebug log file header:
     export Z="$GMS_SETTING/_gmshead.txt"
       echo " gmsdebug.log" > "$Z"
       arg="//////////////////////////////////////"
         echo " $arg$arg" >> "$Z"
       unset arg
       echo " This is the debug log file for Gerolf Markup Shredder." >> "$Z"
       echo " To disable the run time call tracing, set GMS_DEBUG=0." >> "$Z"
       echo " Format: called script (parameter 1) ... (parameter 9)," >> "$Z"
       echo " followed by values of important environment variables." >> "$Z"
       echo " 'shredder': reads user input and calls script modules." >> "$Z"
       echo >> "$Z"
     export Z="$GMS_SETTING/gmsdebug.log"

 # Not found: -----------------------------------------------------------------
 
 else
   source "$GMS_SHELL/l_banner" -no_action g_vars $1   
 fi
