even tastier
[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 WORKDIR 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 thing on multiple lines):
50
51 ##############
52   exec bash -i 3<<EOF 4<&0 <&3
53     echo -e '\n\n^^^ errors above here indicate potential problems in .bashrc ^^^';
54     export FEISTY_MEOW_APEX=\"\$(pwd)\"; export FEISTY_MEOW_SCRIPTS=\$FEISTY_MEOW_APEX/scripts;
55     export FEISTY_MEOW_SHOW_LAUNCH_GREETING=yes;
56     /bin/bash \$(pwd)/scripts/core/reconfigure_feisty_meow.sh;
57     source \$(pwd)/scripts/core/launch_feisty_meow.sh; exec 3>&- <&4
58 EOF
59 ##############
60
61 Note that this assumes that the .bashrc file could still need editing to fix
62 an erroneous FEISTY_MEOW_APEX variable, so we skip it above when bash runs.
63 Check \$HOME/.bashrc to see if a change there will fix the problem.
64
65 "
66 else
67   # apex is good, so let's make the scripts good too.
68   if [ ! -d "$FEISTY_MEOW_SCRIPTS" ]; then
69     export FEISTY_MEOW_SCRIPTS="$FEISTY_MEOW_APEX/scripts"
70   fi
71   # check again to test our belief system...
72   if [ ! -d "$FEISTY_MEOW_SCRIPTS" ]; then
73     unset NO_REPAIRS_NEEDED
74     echo -e "The feisty meow scripts cannot be found under the current top:\n  FEISTY_MEOW_APEX=$FEISTY_MEOW_APEX"
75   fi
76 fi
77
78 #; /bin/bash -i --norc --noprofile\" > \$HOME/fm-fix 
79 #; exec /bin/bash -i --norc --noprofile -c 'bash \$HOME/fm-fix ; echo hello ; read line'
80 #--norc --noprofile 
81 #; source \$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh
82
83 if [ "$NO_REPAIRS_NEEDED" == "true" ]; then
84
85   # we believe it's safe to run through the rest of this script.
86
87   ##############
88   
89   # some preconditions we want to establish before loading anything...
90   
91   # make sure that aliases can be used in non-interactive shells.
92   shopt -s expand_aliases
93   
94   # patch the user variable if we were launched by one of our cron jobs.
95   if [ -z "$USER" -a ! -z "$CRONUSER" ]; then
96     export USER="$CRONUSER"
97   fi
98   
99   ##############
100   
101   export ERROR_OCCURRED=
102     # there have been no errors to start with, at least.  we will set this
103     # to non-empty if something bad happens.
104   
105   if [ -z "$FEISTY_MEOW_LOADING_DOCK" ]; then
106     # FEISTY_MEOW_LOADING_DOCK is where the generated files are located.
107     # this is our single entry point we can use without knowing any variables
108     # yet in the initialization process.
109     export FEISTY_MEOW_LOADING_DOCK="$HOME/.zz_feisty_loading"
110   #hmmm: the above is kind of a constant.  that's not so great.
111   
112     # make sure our main variables are established.
113     FEISTY_MEOW_VARIABLES_LOADING_FILE="$FEISTY_MEOW_LOADING_DOCK/fmc_variables.sh"
114     if [ ! -f "$FEISTY_MEOW_VARIABLES_LOADING_FILE" ]; then
115       echo -e "\
116   
117   The feisty meow scripts need initialization via the bootstrap process.  For\n\
118   example, if the feisty meow folder lives in '$DEFAULT_FEISTYMEOW_ORG_DIR', then this\n\
119   command bootstraps feisty meow:\n\
120   \n\
121     bash $example_dir/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\n\
122   \n\
123   \n"
124       ERROR_OCCURRED=true
125     fi
126   
127     ##############
128   
129     if [ -z "$ERROR_OCCURRED" ]; then
130   
131       # pull in our generated variables that are the minimal set we need to find
132       # the rest of our resources.
133       source "$FEISTY_MEOW_VARIABLES_LOADING_FILE"
134   
135       # Set up the temporary directory.
136       source "$FEISTY_MEOW_SCRIPTS/core/create_tempdir.sh"
137     fi
138   
139   fi
140
141   ##############
142
143   if [ -z "$ERROR_OCCURRED" ]; then
144
145     # load the larger body of standard feisty meow variables into the environment.
146     # we actually want this to always run also; it will decide what variables need
147     # to be set again.
148     source "$FEISTY_MEOW_SCRIPTS/core/variables.sh"
149
150     ##############
151
152     # include helpful functions.  we do this every time rather than making it part
153     # of variable initialization, because functions cannot be exported to
154     # sub-shells in bash.
155     source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
156
157     # load some helper methods for the terminal which we'll use below.
158     source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
159
160     ##############
161
162     # check hash table before searching path.
163     shopt -s checkhash
164     # don't check path for sourced files.
165     shopt -u sourcepath
166     # ignore duplicate lines.
167     HISTCONTROL=ignoredups
168     # append to the history file.
169     shopt -s histappend
170     # automatically update window size if needed.
171     shopt -s checkwinsize
172
173     ##############
174
175     # make history writes immediate to avoid losing history if bash is zapped.
176     echo $PROMPT_COMMAND | grep -q history
177     if [ $? -ne 0 ]; then
178       # we only change the prompt command if we think it hasn't already been done.
179       export PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
180     fi
181
182     ##############
183
184     # perform the bulkier parts of the initialization process.
185
186     if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo "heavyweight init begins..."; fi
187
188     # set up the aliases for the shell, but only if they are not already set.
189     type CORE_ALIASES_LOADED &>/dev/null
190     if [ $? -ne 0 ]; then
191       if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then
192         echo "the aliases were missing, now they are being added..."
193       fi
194       source "$FEISTY_MEOW_LOADING_DOCK/fmc_core_and_custom_aliases.sh"
195     fi
196
197     #echo before the new labelling, terminal titles have:
198     #show_terminal_titles
199
200     # a minor tickle of the title of the terminal, unless we already have some history.
201     label_terminal_with_info
202
203     if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo "heavyweight init is done."; fi
204
205     if [ -z "$ERROR_OCCURRED" ]; then
206       # set a sentinel variable to say we loaded the feisty meow environment.
207       export FEISTY_MEOW_SCRIPTS_LOADED=true
208     fi
209
210   fi  # no error occurred.
211
212   if [ ! -z "$FEISTY_MEOW_SHOW_LAUNCH_GREETING" ]; then
213     echo
214     echo
215     echo "welcome to the feisty meow zone of peace, one of many refuges in the uncountably"
216     echo "infinite multiverses that are hypothetically possible."
217     echo
218     echo
219     unset FEISTY_MEOW_SHOW_LAUNCH_GREETING
220   fi
221
222 fi # "$NO_REPAIRS_NEEDED" was == "true" 
223