#!/bin/sh
 
 # g_file
 # ======
 
 # This file is part of Gerolf Markup Shredder,
 # written by G. D. Brettschneider (1999-2006).
 
 GMSdateG_FILE=20060927
 
 # Prologue: ##################################################################
 
 # Not running:
 
 if [ "$GMS_SHELL" = "" ]; then
   echo "This is Gerolf Markup Shredder (g_file)."
   exit
 fi 
 
 # Debug: ---------------------------------------------------------------------
 
 if [ "$GMS_DEBUG" = "X" -o "$GMS_DEBUG" = "Y" ]; then
   source "$GMS_SHELL/l_banner" -debug g_file $1 $2 $3 $4
 elif  [ "$GMS_DEBUG" = "Z" ]; then
   echo " g_file ($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_file
   
 # Resize: --------------------------------------------------------------------
 
 elif [ $1 = -resize ]; then
   export REPLY_OFFSET=3
   export REPLY_SIZE=44
   export REPLY_ITEMS=13
   
 # Build: ---------------------------------------------------------------------
 
 elif [ $1 = -build ]; then 
   export GMS_STATE=$2
   BACKUP=$3
     source "$GMS_SHELL/g_file" -resize called_by g_file -build
     source "$GMS_SHELL/l_box" -build called_by g_file -build
     source "$GMS_SHELL/l_file" -build "$BACKUP" called_by g_file -build
     export GMS_FOLDER=$PWD
     source "$GMS_SHELL/l_banner" -lower -folder called_by g_file -build
     export REPLY_LIST=0
     source "$GMS_SHELL/g_list" -handle called_by g_file -build
     source "$GMS_SHELL/l_list" -build called_by g_file -build
     export GMS_HOT=N
     if [ "$GMS_STATE" = -folder ]; then export GMS_HOT=H; fi
     source "$GMS_SHELL/l_file" -update $GMS_HOT $GMS_HOT called_by g_file -build
     export REPLY_MODULE=g_file
     export REPLY_ACTION=-update
   BACKUP= 
   export GMS_RECEIVE=1
   
 # Remove:---------------------------------------------------------------------
 
 elif [ $1 = -remove ]; then
   source "$GMS_SHELL/g_file" -resize called_by g_file -remove
   source "$GMS_SHELL/l_box" -remove called_by g_file -remove
   if [ $GMS_STATE = -folder ]; then
   # Selected a folder to save the new file:
     source "$GMS_SHELL/g_save" -build called_by g_file -remove
   elif [ $GMS_STATE = -template ]; then
   # Selected a template for the new file:
     source "$GMS_SHELL/g_menu" -resize called_by g_file -remove
     source "$GMS_SHELL/l_menu" -update C Q called_by g_file -remove
     export REPLY_MODULE=g_menu
     export REPLY_ACTION=-update
     export GMS_RECEIVE=1
   else
   # Changed folder and opened markup file:
     source "$GMS_SHELL/g_menu" -resize called_by g_file -remove
     source "$GMS_SHELL/l_menu" -update O Q called_by g_file -remove
     export REPLY_MODULE=g_menu
     export REPLY_ACTION=-update
     export GMS_RECEIVE=1
   fi 
   
 # 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_file called_by g_file -update
   else
     GMS_HOT="Q" 
     case "$2" in
       "1" ) GMS_HOT="1";;
       "2" ) GMS_HOT="2";;
       "3" ) GMS_HOT="3";;
       "4" ) GMS_HOT="4";;
       "5" ) GMS_HOT="5";;
       "6" ) GMS_HOT="6";;
       "7" ) GMS_HOT="7";;
       "8" ) GMS_HOT="8";;
       "9" ) GMS_HOT="9";;
       "Q" | "q" | "10" ) GMS_HOT="Q";;
       "P" | "p" | "11" ) GMS_HOT="P";;
       "N" | "n" | "12" ) GMS_HOT="N";;
       "H" | "h" | "13" ) GMS_HOT="H";;
     esac
   fi 
 # Handle old, update new:
   if [ "$GMS_HOT" = "$GMS_COLD" ]; then
     source "$GMS_SHELL/g_file" -handle $GMS_HOT
   else
     source "$GMS_SHELL/l_file" -update $GMS_HOT $GMS_COLD
   fi 
   export GMS_RECEIVE=1
   export GMS_HOT="Q"
   
 # Handle: --------------------------------------------------------------------
   
 elif [ $1 = -handle ]; then
 
 # Quit:   
   if [ $2 = Q ]; then 
     source "$GMS_SHELL/g_file" -remove called_by g_file -handle Q
   
 # Higher:   
   elif [ $2 = H ]; then
     cd ..
     export GMS_FOLDER=$PWD
     export GMS_FILE=
     export REPLY_LIST=0
     export GMS_RECEIVE=1
     source "$GMS_SHELL/g_list" -clear called_by g_file -handle H
     source "$GMS_SHELL/l_banner" -upper -first called_by g_file -handle H
     source "$GMS_SHELL/g_list" -handle called_by g_file -handle H
     source "$GMS_SHELL/l_list" -build  called_by g_file -handle H
     source "$GMS_SHELL/l_banner" -lower -folder called_by g_file -handle H
     source "$GMS_SHELL/l_file" -update H - called_by g_file -handle H
   
 # Previous:   
   elif [ $2 = P ]; then
     if [ $REPLY_LIST = 0 -o $REPLY_LIST = 1 -o $REPLY_LIST = 2 \
       -o $REPLY_LIST = 3 -o $REPLY_LIST = 4 -o $REPLY_LIST = 5 \
       -o $REPLY_LIST = 6 -o $REPLY_LIST = 7 -o $REPLY_LIST = 8 ]; then
     # Keep list offset non-negative:
       export REPLY_LIST=0
     else
     # Diminish list offset by 8:
       export REPLY_LIST=$[$REPLY_LIST - 8]
     fi
     export GMS_RECEIVE=1
     source "$GMS_SHELL/g_list" -clear called_by g_file -handle P
     source "$GMS_SHELL/g_list" -handle called_by g_file -handle P
     source "$GMS_SHELL/l_list" -build called_by g_file -handle P
     source "$GMS_SHELL/l_file" -update P - called_by g_file -handle P
   
 # Next:   
   elif [ $2 = N ]; then
     if [ "$REPLY9" != "" ]; then
       export REPLY_LIST=$[$REPLY_LIST + 8]
     fi 
     export GMS_RECEIVE=1 
     source "$GMS_SHELL/g_list" -clear called_by g_file -handle N
     source "$GMS_SHELL/g_list" -handle called_by g_file -handle N
     source "$GMS_SHELL/l_list" -build called_by g_file -handle N
     source "$GMS_SHELL/l_file" -update N - called_by g_file -handle N
   
 # Selected file no. 1 - 9:   
   elif [ $2 = 1 ]; then
     export GMS_NEWFILE=$REPLY1
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 1
   elif [ $2 = 2 ]; then
     export GMS_NEWFILE=$REPLY2
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 2
   elif [ $2 = 3 ]; then
     export GMS_NEWFILE=$REPLY3
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 3
   elif [ $2 = 4 ]; then
     export GMS_NEWFILE=$REPLY4
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 4
   elif [ $2 = 5 ]; then
     export GMS_NEWFILE=$REPLY5
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 5
   elif [ $2 = 6 ]; then
     export GMS_NEWFILE=$REPLY6
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 6
   elif [ $2 = 7 ]; then
     export GMS_NEWFILE=$REPLY7
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 7
   elif [ $2 = 8 ]; then
     export GMS_NEWFILE=$REPLY8
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 8
   elif [ $2 = 9 ]; then
     export GMS_NEWFILE=$REPLY9
     source "$GMS_SHELL/g_file" -handle -entry called_by g_file -handle 9
   
 # Handle file or folder: 
   elif [ $2 = -entry ]; then
     # Check if it is a folder:     
     export GMS_FOLDER=$GMS_NEWFILE
     if [ -d "$GMS_FOLDER" ]; then
       cd "$GMS_FOLDER"
       export GMS_FILE=
       source "$GMS_SHELL/g_file" -handle -folder \
         called_by g_file -handle -entry
     else 
     # Then it will be a file:
       export GMS_FOLDER=$PWD
       export GMS_FILE=$GMS_NEWFILE
       if [ "$GMS_STATE" = -open ]; then
         source "$GMS_SHELL/g_file" -handle -file \
           called_by g_file -handle -entry
       elif [ "$GMS_STATE" = -template ]; then
         source "$GMS_SHELL/g_file" -handle -template \
           called_by g_file -handle -entry
       fi
     fi
     
 # Handle folder:   
   elif [ $2 = -folder ]; then
     export GMS_FOLDER="$PWD"
     source "$GMS_SHELL/l_banner" -upper -first \
       called_by g_file -handle -folder
     source "$GMS_SHELL/l_banner" -lower -folder \
       called_by g_file -handle -folder
     export REPLY_LIST=0
     source "$GMS_SHELL/g_list" -clear called_by g_file -handle -folder
     source "$GMS_SHELL/g_list" -handle called_by g_file -handle -folder
     source "$GMS_SHELL/l_list" -build  called_by g_file -handle -folder
     source "$GMS_SHELL/l_file" -update H 1 called_by g_file -handle -folder
     export REPLY_MODULE=g_file
     export REPLY_ACTION=-update
     export GMS_RECEIVE=1
 
 # Handle file: 
   elif [ $2 = -file ]; then
     if [ "$GMS_FILE" = "" ]; then
       source "$GMS_SHELL/l_banner" -upper -first \
         called_by g_file -handle -file
     else
       export REPLY_SIZE=50
       export REPLY_OFFSET=0
       "$GMS_REPLY" -banner 1 "$GMS_FILE"     
     fi 
     source "$GMS_SHELL/g_file" -resize called_by g_file -handle -file
     source "$GMS_SHELL/l_box" -remove called_by g_file -handle -file
     source "$GMS_SHELL/g_menu" -resize called_by g_file -handle -file
     source "$GMS_SHELL/l_menu" -update O Q called_by g_file -handle -file
     source "$GMS_SHELL/g_vars" -write_memo called_by g_file -handle -file
     export REPLY_MODULE=g_menu
     export REPLY_ACTION=-update
     export GMS_RECEIVE=1
 
 # Handle template: 
   elif [ $2 = -template ]; then
     export GMS_TEMPLATEFILE="$GMS_NEWFILE"
     export GMS_TFOLDER="$PWD"
     source "$GMS_SHELL/l_banner" -upper -template \
       called_by g_file -handle -template
     export GMS_FOLDER="$GMS_LASTDIR"
     export GMS_LASTDIR=
   # Select folder:
     BACKUP_ANIMATE=$GMS_ANIMATE 
       GMS_ANIMATE="off"
         export GMS_FILEBOX=" 2. Select a folder to save this new copy "
           export GMS_FOLDER="$GMS_TEMPLATE"
           if [ -d "$GMS_FOLDER" ]; then cd "$GMS_FOLDER"; fi
           source "$GMS_SHELL/g_file" -build -folder          
         export GMS_FILEBOX=
       GMS_ANIMATE=$BACKUP_ANIMATE
     BACKUP_ANIMATE=
      
 # Not found: 
   else
     source "$GMS_SHELL/l_banner" -no_hot g_file $2
   
   fi
   
 # Not found: -----------------------------------------------------------------
 
 else
   source "$GMS_SHELL/l_banner" -no_action g_file $1   
 fi
