plugged in timestamper in a couple places
[feisty_meow.git] / scripts / core / launch_feisty_meow.sh
1 #!/usr/bin/env bash
2
3 ##############
4
5 # Fred Hamster's Feisty Meow Concerns Ltd. Startup Profile.
6 #
7 # This file is useful within .profile or other initialization scripts.
8 #
9 # Author: Chris Koeritz
10
11 ##############
12
13 # DEBUG_FEISTY_MEOW: if this variable is non-empty, then it causes the feisty meow
14 # scripts to print more diagnostic information when they run.  not all
15 # scripts support this, but the core ones do.
16
17 #export DEBUG_FEISTY_MEOW=true
18
19 ##############
20
21 # first step--capture the current path if we haven't already.
22 if [ -z "$FEISTY_MEOW_ORIGINAL_PATH" ]; then
23   export FEISTY_MEOW_ORIGINAL_PATH="$PATH"
24 fi
25
26 ##############
27
28 # this script cannot handle figuring out where it lives, so approaches that
29 # get the THISDIR will fail.  this is a consequence of this always being used
30 # in bash's 'source' directive, which does not pass the script name as
31 # argument 0.  instead, we just check for the bad condition of a malconfigured
32 # script system and try to repair it.
33
34 # we start out thinking things are good.
35 NO_REPAIRS_NEEDED=true
36
37 # check if any crucial folder is hosed.  we will torch the existing config
38 # to the extent we can.
39 if [ ! -d "$FEISTY_MEOW_APEX" ]; then
40   # flag some problems.
41   unset NO_REPAIRS_NEEDED
42   # wipe out the offending variable(s).
43   unset FEISTY_MEOW_SCRIPTS FEISTY_MEOW_APEX
44   # clean out any unfortunate wrongness that may exist in our generated areas.
45   if [ -d "$FEISTY_MEOW_LOADING_DOCK" ]; then \rm -rf "$FEISTY_MEOW_LOADING_DOCK"; fi
46   if [ -d "$FEISTY_MEOW_GENERATED_STORE" ]; then \rm -rf "$FEISTY_MEOW_GENERATED_STORE"; fi
47   # also wipe any values from the variables pointing at generated stuff.
48   unset FEISTY_MEOW_LOADING_DOCK FEISTY_MEOW_GENERATED_STORE
49   echo "
50
51 The feisty meow configuration is damaged somehow.  Please change to the
52 directory where it is stored, e.g.:
53
54   cd /opt/feistymeow.org/feisty_meow
55
56 and run this command (the whole unwieldy multiple line chunk inside the bars):
57
58
59 ##############
60   exec bash -i 3<<EOF 4<&0 <&3
61     echo -e '\n\n^^^ errors above here indicate potential problems in .bashrc ^^^';
62     export FEISTY_MEOW_APEX=\"\$(pwd)\"; export FEISTY_MEOW_SCRIPTS=\$FEISTY_MEOW_APEX/scripts;
63     export FEISTY_MEOW_SHOW_LAUNCH_GREETING=yes;
64     /bin/bash \$(pwd)/scripts/core/reconfigure_feisty_meow.sh;
65     source \$(pwd)/scripts/core/launch_feisty_meow.sh; exec 3>&- <&4
66 EOF
67 ##############
68
69
70 This code snippet assumes that the .bashrc file could still need editing to
71 fix an erroneous FEISTY_MEOW_APEX variable, so we skip it above when bash
72 runs.  Check \$HOME/.bashrc to see if a change there will fix the problem.
73
74 "
75 else
76   # apex is good, so let's make the scripts good too.
77   if [ -z "$FEISTY_MEOW_SCRIPTS" -o ! -d "$FEISTY_MEOW_SCRIPTS" ]; then
78     export FEISTY_MEOW_SCRIPTS="$FEISTY_MEOW_APEX/scripts"
79   fi
80   # check again to test our belief system...
81   if [ ! -d "$FEISTY_MEOW_SCRIPTS" ]; then
82     unset NO_REPAIRS_NEEDED
83     echo -e "The feisty meow scripts cannot be found under the current top:\n  FEISTY_MEOW_APEX=$FEISTY_MEOW_APEX"
84   fi
85 fi
86
87 if [ "$NO_REPAIRS_NEEDED" == "true" ]; then
88
89   # we believe it's safe to run through the rest of this script.
90
91   ##############
92   
93   # some preconditions we want to establish before loading anything...
94   
95   # make sure that aliases can be used in non-interactive shells.
96   # this causes all aliases that are currently defined for this shell to
97   # be inherited by subshells that this shell starts.  this is unusual,
98   # but is preferred for my workflow in feisty meow scripts; it saves me
99   # time re-adding aliases if i can count on them already being there.
100   # this is a problem if you *don't* want the aliases there though.  we can
101   # solve that problem by running bash with the "-O expand_aliases" flags to
102   # stop the expansion for the next subshell.
103   shopt -s expand_aliases
104   # set the directory tab completion to behave properly and not start escaping
105   # the dollar signs in variable names.
106   shopt -u progcomp
107   
108   # patch the user variable if we were launched by one of our cron jobs.
109   if [ -z "$USER" -a ! -z "$CRONUSER" ]; then
110     export USER="$CRONUSER"
111   fi
112
113   # use the xauth info if we were given one in the environment.
114   # this allows root or other su'd identities to create windows with same
115   # display variable.
116   if [ ! -z "$DISPLAY" -a ! -z "$IMPORTED_XAUTH" ]; then
117     xauth add $DISPLAY . $IMPORTED_XAUTH
118   fi
119   
120   ##############
121   
122   export ERROR_OCCURRED=
123     # there have been no errors to start with, at least.  we will set this
124     # to non-empty if something bad happens.
125   
126   if [ -z "$FEISTY_MEOW_LOADING_DOCK" ]; then
127     # FEISTY_MEOW_LOADING_DOCK is where the generated files are located.
128     # this is our single entry point we can use without knowing any variables
129     # yet in the initialization process.
130     export FEISTY_MEOW_LOADING_DOCK="$HOME/.zz_feisty_loading"
131 #hmmm: the above is kind of a constant.  that's not so great.
132   
133     # make sure our main variables are established.
134     FEISTY_MEOW_VARIABLES_LOADING_FILE="$FEISTY_MEOW_LOADING_DOCK/fmc_variables.sh"
135     if [ ! -f "$FEISTY_MEOW_VARIABLES_LOADING_FILE" ]; then
136       echo -e "\
137   
138   The feisty meow scripts need initialization via the bootstrap process.  For\n\
139   example, if the feisty meow folder lives in '$DEFAULT_FEISTYMEOW_ORG_DIR', then this\n\
140   command bootstraps feisty meow:\n\
141   \n\
142     bash $example_dir/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\n\
143   \n\
144   \n"
145       ERROR_OCCURRED=true
146     fi
147   
148     ##############
149
150     if [ -z "$ERROR_OCCURRED" ]; then
151       # pull in our generated variables that are the minimal set we need to find
152       # the rest of our resources.
153       source "$FEISTY_MEOW_VARIABLES_LOADING_FILE"
154   
155       # Set up the temporary directory.
156       source "$FEISTY_MEOW_SCRIPTS/core/create_tempdir.sh"
157     fi
158   
159   fi
160
161   ##############
162
163   if [ -z "$ERROR_OCCURRED" ]; then
164     # no error occurred in our tests above, so load the larger body of standard feisty
165     # meow variables into the environment.  we actually want this to always run also;
166     # it will decide what variables need to be set again.
167     source "$FEISTY_MEOW_SCRIPTS/core/variables.sh"
168
169     ##############
170
171     # include helpful functions.  we do this every time rather than making it part
172     # of variable initialization, because functions cannot be exported to
173     # sub-shells in bash.
174     source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
175
176     # load some helper methods for the terminal which we'll use below.
177     source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
178
179     ##############
180
181 #hmmm: abstract this to a twiddle shell options method.
182     # check hash table before searching path.
183     shopt -s checkhash
184     # don't check path for sourced files.
185     shopt -u sourcepath
186     # ignore duplicate lines.
187     HISTCONTROL=ignoredups
188     # append to the history file.
189     shopt -s histappend
190     # automatically update window size if needed.
191     shopt -s checkwinsize
192
193     ##############
194
195     # make history writes immediate to avoid losing history if bash is zapped.
196     echo $PROMPT_COMMAND | grep -q history
197     if [ $? -ne 0 ]; then
198       # we only change the prompt command if we think it hasn't already been done.
199       export PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
200     fi
201
202     ##############
203
204     # perform the bulkier parts of the initialization process.
205
206     if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo "heavyweight init begins..."; fi
207
208     # set up the aliases for the shell, but only if they are not already set.
209     type CORE_ALIASES_LOADED &>/dev/null
210     if [ $? -ne 0 ]; then
211       if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then
212         echo "the aliases were missing, now they are being added..."
213       fi
214       source "$FEISTY_MEOW_LOADING_DOCK/fmc_core_and_custom_aliases.sh"
215     fi
216
217     #echo before the new labelling, terminal titles have:
218     #show_terminal_titles
219
220     # a minor tickle of the title of the terminal, unless we already have some history.
221     label_terminal_with_info
222
223     if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo "heavyweight init is done."; fi
224
225     if [ -z "$ERROR_OCCURRED" ]; then
226       # set a sentinel variable to say we loaded the feisty meow environment.
227       export FEISTY_MEOW_SCRIPTS_LOADED=true
228     fi
229
230   fi  # no error occurred.
231
232   if [ ! -z "$FEISTY_MEOW_SHOW_LAUNCH_GREETING" ]; then
233     echo
234     echo
235     echo "welcome to the feisty meow zone of peace, one of many refuges in the uncountably"
236     echo "infinite multiverses that are hypothetically possible."
237     echo
238     echo
239     unset FEISTY_MEOW_SHOW_LAUNCH_GREETING
240   fi
241
242   # only run this hello file if the core feisty meow support haven't been loaded already.  this
243   # hopefully guarantees we show the info at most once in one shell continuum.
244   # this can also be disabled if the NO_HELLO variable has a non-empty value.
245   type CORE_VARIABLES_LOADED &>/dev/null
246   if [ $? -ne 0 -a -z "$NO_HELLO" ]; then
247     # print out a personalized hello file if we find one.
248     if [ -f ~/hello.txt ]; then
249       echo
250       sep 28
251       perl $FEISTY_MEOW_SCRIPTS/*/filedump.pl ~/hello.txt
252       sep 28
253       echo
254     fi
255     # from now on there should be no extra helloing.
256     export NO_HELLO=true
257   fi
258
259   # load the last bits we do here.
260   source "$FEISTY_MEOW_LOADING_DOCK/fmc_ending_sentinel.sh"
261
262 fi # "$NO_REPAIRS_NEEDED" was == "true" 
263