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