refurbishing for more auto-repair
[feisty_meow.git] / scripts / core / launch_feisty_meow.sh
index 581b950eebb026a6208f96f99656ba419a592f45..80226899a83bad279656c1986bfc825ebc186775 100644 (file)
 
 ##############
 
-#export SHELL_DEBUG=true
-  # this variable causes the scripts that listen to it to print more information
-  # when they run.
+# DEBUG_FEISTY_MEOW: if this variable is non-empty, then it causes the feisty meow
+# scripts to print more diagnostic information when they run.  not all
+# scripts support this, but the core ones do.
 
-##############
+#export DEBUG_FEISTY_MEOW=true
 
-# FEISTY_MEOW_GENERATED is where the generated files yeti uses are located.
-# this is our single entry point we can use without knowing any variables
-# yet in the initialization process.
-export FEISTY_MEOW_GENERATED="$HOME/.zz_auto_gen"
-if [ ! -z "$WINDIR" -o ! -z "$windir" ]; then
-  # assume they are using windoze.
-  export FEISTY_MEOW_GENERATED="$TMP/zz_auto_gen"
-fi
+##############
 
-# make sure our main variables are established.
-GENERATED_FEISTY_MEOW_VARIABLES="$FEISTY_MEOW_GENERATED/feisty_meow_variables.sh"
-if [ ! -f "$GENERATED_FEISTY_MEOW_VARIABLES" ]; then
-  echo -e '\n\n'
-  echo "The yeti scripts need to be initialized via the bootstrap process, e.g.:"
-  echo "  bash $HOME/feisty_meow/scripts/core/bootstrap_shells.sh"
-  echo -e '\n\n'
+# this script cannot handle figuring out where it lives, so approaches that
+# get the WORKDIR will fail.  this is a consequence of this always being used
+# in bash's 'source' directive, which does not pass the script name as
+# argument 0.  instead, we just check for the bad condition of a malconfigured
+# script system and try to repair it.
+
+# check if any crucial folder is hosed.  we will torch the existing config
+# to the extent we can.
+if [ ! -d "$FEISTY_MEOW_SCRIPTS" -o ! -d "$FEISTY_MEOW_APEX" ]; then
+  # wipe out the offending variable(s).
+  unset FEISTY_MEOW_SCRIPTS FEISTY_MEOW_APEX
+  # clean out any unfortunate wrongness that may exist in our generated areas.
+  if [ -d "$"FEISTY_MEOW_LOADING_DOCK ]; then \rm -rf "$FEISTY_MEOW_LOADING_DOCK"; fi
+  if [ -d "$FEISTY_MEOW_GENERATED_STORE" ]; then \rm -rf "$FEISTY_MEOW_GENERATED_STORE"; fi
+  # also wipe any values from the variables pointing at generated stuff.
+  unset FEISTY_MEOW_LOADING_DOCK FEISTY_MEOW_GENERATED_STORE
+  exec "$*"
 fi
 
-# pull in our variable set.
-source "$GENERATED_FEISTY_MEOW_VARIABLES"
-
 ##############
 
-# Set up all of the environment's system variables.  This is the first step
-# in the majestic plan we have for this shell's initialization.
-source $FEISTY_MEOW_SCRIPTS/core/variables.sh
+# some preconditions we want to establish before loading anything...
+
+# make sure that aliases can be used in non-interactive shells.
+shopt -s expand_aliases
 
-# Set up the temporary directory...
-source $FEISTY_MEOW_SCRIPTS/core/create_tempdir.sh
+# patch the user variable if we were launched by one of our cron jobs.
+if [ -z "$USER" -a ! -z "$CRONUSER" ]; then
+  export USER="$CRONUSER"
+fi
 
 ##############
 
-# check if this is dos/windows.
-if [ "$OS" == "Windows_NT" ]; then
-  if [ -z "$HOME" ]; then
-    # set a default that might not be appropriate for everyone, but should
-    # still work.
-    export HOME=/c/home
+export ERROR_OCCURRED=
+  # there have been no errors to start with, at least.  we will set this
+  # to non-empty if something bad happens.
+
+if [ -z "$FEISTY_MEOW_LOADING_DOCK" ]; then
+  # FEISTY_MEOW_LOADING_DOCK is where the generated files are located.
+  # this is our single entry point we can use without knowing any variables
+  # yet in the initialization process.
+  export FEISTY_MEOW_LOADING_DOCK="$HOME/.zz_feisty_loading"
+#hmmm: the above is kind of a constant.  that's not so great.
+
+  # make sure our main variables are established.
+  FEISTY_MEOW_VARIABLES_LOADING_FILE="$FEISTY_MEOW_LOADING_DOCK/fmc_variables.sh"
+  if [ ! -f "$FEISTY_MEOW_VARIABLES_LOADING_FILE" ]; then
+    echo -e "\
+
+The feisty meow scripts need initialization via the bootstrap process.  For\n\
+example, if the feisty meow folder lives in '$DEFAULT_FEISTYMEOW_ORG_DIR', then this\n\
+command bootstraps feisty meow:\n\
+\n\
+  bash $example_dir/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\n\
+\n\
+\n"
+    ERROR_OCCURRED=true
   fi
-  if [ ! -d "$HOME" ]; then mkdir $HOME; fi
-fi
 
-##############
+  ##############
 
-if [ -z "$LIGHTWEIGHT_INIT" ]; then
-  # perform the bulkier parts of the login and initialization.
+  if [ -z "$ERROR_OCCURRED" ]; then
 
-  if [ ! -z "$SHELL_DEBUG" ]; then echo heavyweight login begins...; fi
+    # pull in our generated variables that are the minimal set we need to find
+    # the rest of our resources.
+    source "$FEISTY_MEOW_VARIABLES_LOADING_FILE"
 
-#FEISTY_MEOW_GENERATED=$(echo $FEISTY_MEOW_GENERATED | sed -e 's/\\/\//g')
-#FEISTY_MEOW_SCRIPTS="$(echo $FEISTY_MEOW_SCRIPTS | sed -e 's/\\/\//g')"
-#FEISTY_MEOW_SCRIPTS="$FEISTY_MEOW_SCRIPTS"
+    # Set up the temporary directory.
+    source "$FEISTY_MEOW_SCRIPTS/core/create_tempdir.sh"
+  fi
 
+fi
+
+##############
+
+if [ -z "$ERROR_OCCURRED" ]; then
+
+  # load the larger body of standard feisty meow variables into the environment.
+  # we actually want this to always run also; it will decide what variables need
+  # to be set again.
+  source "$FEISTY_MEOW_SCRIPTS/core/variables.sh"
+
+  ##############
+  
+  # include helpful functions.  we do this every time rather than making it part
+  # of variable initialization, because functions cannot be exported to
+  # sub-shells in bash.
+  source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
+  
+  # load some helper methods for the terminal which we'll use below.
+  source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
+
+  ##############
+  
+  # check hash table before searching path.
+  shopt -s checkhash
+  # don't check path for sourced files.
+  shopt -u sourcepath
+  # ignore duplicate lines.
+  HISTCONTROL=ignoredups
+  # append to the history file.
+  shopt -s histappend
+  # automatically update window size if needed.
+  shopt -s checkwinsize
+
+  ##############
+
+  # make history writes immediate to avoid losing history if bash is zapped.
+  echo $PROMPT_COMMAND | grep -q history
+  if [ $? -ne 0 ]; then
+    # we only change the prompt command if we think it hasn't already been done.
+    export PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
+  fi
+  
+  ##############
+  
+  # perform the bulkier parts of the initialization process.
+  
+  if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo "heavyweight init begins..."; fi
+  
   # set up the aliases for the shell, but only if they are not already set.
-  alias regenerated &>/dev/null  # see if a crucial alias exists.
+  type CORE_ALIASES_LOADED &>/dev/null
   if [ $? -ne 0 ]; then
-    if [ ! -z "$SHELL_DEBUG" ]; then
-      echo the aliases were missing, now they are added...
+    if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then
+      echo "the aliases were missing, now they are being added..."
     fi
-    source "$FEISTY_MEOW_GENERATED/core_and_custom_aliases.sh"
+    source "$FEISTY_MEOW_LOADING_DOCK/fmc_core_and_custom_aliases.sh"
   fi
-
-  # allow connections to our x server from the local host.
-  if [ ! -z "$DISPLAY" ]; then
-    if [ ! -z "$(echo "$OS_TYPE" | grep -i darwin)" ]; then
-      if [ ! -z "$SHELL_DEBUG" ]; then echo Enabling localhost X connections...; fi
-      xhost + localhost >/dev/null 2>&1
-    fi
+  
+  #echo before the new labelling, terminal titles have:
+  #show_terminal_titles
+  
+  # a minor tickle of the title of the terminal, unless we already have some history.
+  label_terminal_with_info
+  
+  if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo "heavyweight init is done."; fi
+  
+  if [ -z "$ERROR_OCCURRED" ]; then
+    # set a sentinel variable to say we loaded the feisty meow environment.
+    export FEISTY_MEOW_SCRIPTS_LOADED=true
   fi
 
-  # a minor tickle of the title of the terminal, in case there is one.
-  bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
-
-  if [ ! -z "$SHELL_DEBUG" ]; then echo heavyweight login ends....; fi
-fi
+fi  # no error occurred.