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