not adding eclipse and java at front of path, since they don't need to be there.
[feisty_meow.git] / scripts / core / functions.sh
1 #!/bin/bash
2
3 # This defines some general, useful functions.
4
5 # test whether we've been here before or not.
6 skip_all=
7 function_sentinel &>/dev/null
8 if [ $? -eq 0 ]; then
9   # there was no error, so we can skip the inits.
10   if [ ! -z "$SHELL_DEBUG" ]; then
11     echo skipping functions.sh because already defined.
12   fi
13   skip_all=yes
14 fi
15
16 if [ -z "$skip_all" ]; then
17   if [ ! -z "$SHELL_DEBUG" ]; then
18     echo function definitions begin...
19   fi
20   
21   # a handy little method that can be used for date strings.  it was getting
22   # really tiresome how many different ways the script did the date formatting.
23   function date_stringer() {
24     date +"%Y_%m_%d_%H%M_%S" | tr -d '/\n/'
25   }
26   
27   # makes a directory of the name specified and then tries to change the
28   # current directory to that directory.
29   function mcd() {
30     if [ ! -d "$1" ]; then mkdir -p "$1"; fi
31     cd "$1"
32   }
33   
34   # locates a process given a search pattern to match in the process list.
35   function psfind() {
36     local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
37     local PIDS_SOUGHT=()
38     local patterns=($*)
39     if [ "$OS" == "Windows_NT" ]; then
40       # needs to be a windows format filename for 'type' to work.
41       if [ ! -d c:/tmp ]; then
42         mkdir c:/tmp
43       fi
44       # windows7 magical mystery tour lets us create a file c:\\tmp_pids.txt, but then it's not really there
45       # in the root of drive c: when we look for it later.  hoping to fix that problem by using a subdir, which
46       # also might be magical thinking from windows perspective.
47       tmppid=c:\\tmp\\pids.txt
48       # we have abandoned all hope of relying on ps on windows.  instead
49       # we use wmic to get full command lines for processes.
50       # this does not exist on windows home edition.  we are hosed if that's
51       # what they insist on testing on.
52       wmic /locale:ms_409 PROCESS get processid,commandline </dev/null >"$tmppid"
53       local flag='/c'
54       if [ ! -z "$(uname -a | grep "^MING" )" ]; then
55         flag='//c'
56       fi
57       # we 'type' the file to get rid of the unicode result from wmic.
58       cmd $flag type "$tmppid" >$PID_DUMP
59       \rm "$tmppid"
60       local CR='\r'  # embedded carriage return.
61       local appropriate_pattern="s/^.*  *\([0-9][0-9]*\)[ $CR]*\$/\1/p"
62       for i in "${patterns[@]}"; do
63         PIDS_SOUGHT+=$(cat $PID_DUMP \
64           | grep -i "$i" \
65           | sed -n -e "$appropriate_pattern")
66         if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
67           # we want to bail as soon as we get matches, because on the same
68           # platform, the same set of patterns should work to find all
69           # occurrences of the genesis java.
70           break;
71         fi
72       done
73     else
74       /bin/ps $extra_flags wuax >$PID_DUMP
75       # pattern to use for peeling off the process numbers.
76       local appropriate_pattern='s/^[-a-zA-Z_0-9][-a-zA-Z_0-9]*  *\([0-9][0-9]*\).*$/\1/p'
77       # remove the first line of the file, search for the pattern the
78       # user wants to find, and just pluck the process ids out of the
79       # results.
80       for i in "${patterns[@]}"; do
81         PIDS_SOUGHT=$(cat $PID_DUMP \
82           | sed -e '1d' \
83           | grep -i "$i" \
84           | sed -n -e "$appropriate_pattern")
85         if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
86           # we want to bail as soon as we get matches, because on the same
87           # platform, the same set of patterns should work to find all
88           # occurrences of the genesis java.
89           break;
90         fi
91       done
92     fi
93     if [ ! -z "$PIDS_SOUGHT" ]; then echo "$PIDS_SOUGHT"; fi
94     /bin/rm $PID_DUMP
95   }
96   
97   # finds all processes matching the pattern specified and shows their full
98   # process listing (whereas psfind just lists process ids).
99   function psa() {
100     p=$(psfind "$1")
101     if [ ! -z "$p" ]; then
102       echo ""
103       echo "Processes containing \"$1\"..."
104       echo ""
105       if [ -n "$IS_DARWIN" ]; then
106         unset fuzil_sentinel
107         for i in $p; do
108           # only print the header the first time.
109           if [ -z "$fuzil_sentinel" ]; then
110             ps $i -w -u
111           else
112             ps $i -w -u | sed -e '1d'
113           fi
114           fuzil_sentinel=true
115         done
116       else 
117         # cases besides mac os x's darwin.
118         extra_flags=
119         if [ "$OS" = "Windows_NT" ]; then
120           # special case for windows.
121           extra_flags=-W
122           ps | head -1
123           for curr in $p; do
124             ps $extra_flags | grep "$curr" 
125           done
126         else
127           # normal OSes can handle a nice simple query.
128           ps wu $p
129         fi
130       fi
131     fi
132   }
133   
134   # an unfortunately similarly named function to the above 'ps' as in process
135   # methods, but this 'ps' stands for postscript.  this takes a postscript file
136   # and converts it into pcl3 printer language and then ships it to the printer.
137   # this mostly makes sense for an environment where one's default printer is
138   # pcl.  if the input postscript causes ghostscript to bomb out, there has been
139   # some good success running ps2ps on the input file and using the cleaned
140   # postscript file for printing.
141   function ps2pcl2lpr() {
142     for $i in $*; do
143       gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l 
144     done
145   }
146   
147   function fix_alsa() {
148     sudo /etc/init.d/alsasound restart
149   }
150   
151   # switches from a /X/path form to an X:/ form.
152   function msys_to_dos_path() {
153     # we always remove dos slashes in favor of forward slashes.
154     echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
155   }
156   
157   # switches from an X:/ form to an /X/path form.
158   function dos_to_msys_path() {
159     # we always remove dos slashes in favor of forward slashes.
160     echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
161   }
162   
163   # su function: makes su perform a login.
164   # for some OSes, this transfers the X authority information to the new login.
165   function su() {
166     # decide if we think this is debian or ubuntu or a variant.
167     DEBIAN_LIKE=$(if [ ! -z "$(grep -i debian /etc/issue)" \
168         -o ! -z "$(grep -i ubuntu /etc/issue)" ]; then echo 1; else echo 0; fi)
169   
170     if [ $DEBIAN_LIKE -eq 1 ]; then
171       # debian currently requires the full version which imports X authority
172       # information for su.
173   
174       # get the x authority info for our current user.
175       source $FEISTY_MEOW_SCRIPTS/x_win/get_x_auth.sh
176   
177       if [ -z "$X_auth_info" ]; then
178         # if there's no authentication info to pass along, we just do a normal su.
179         /bin/su -l $*
180       else
181         # under X, we update the new login's authority info with the previous
182         # user's info.
183         (unset XAUTHORITY; /bin/su -l $* -c "$X_auth_info ; export DISPLAY=$DISPLAY ; bash")
184       fi
185     else
186       # non-debian supposedly doesn't need the extra overhead any more.
187       # or at least suse doesn't, which is the other one we've tested on.
188       /bin/su -l $*
189     fi
190   
191     # relabel the console after returning.
192     bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
193   }
194   
195   # sudo function wraps the normal sudo by ensuring we replace the terminal
196   # label if they're doing an su with the sudo.
197   function sudo() {
198     local first_command="$1"
199     /usr/bin/sudo $*
200     if [ "$first_command" == "su" ]; then
201       # yep, they were doing an su, but they're back now.
202       bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
203     fi
204   }
205   
206   # buntar is a long needed uncompressing macro that feeds into tar -x.
207   # it takes a list of bz2 file names and extracts their contents into
208   # sequentially numbered directories.
209   function buntar() {
210     index=1
211     for i in $*; do
212       mkdir buntar_$index
213       pushd buntar_$index &>/dev/null
214       file=$i
215       # if the filename has no directory component, we will assume it used to
216       # be above our unzipping directory here.
217       if [ "$(basename $file)" = $file ]; then
218         file=../$file
219       fi
220       bunzip2 -d -c $file | tar -xf -
221       popd &>/dev/null
222       index=$(expr $index + 1)
223     done
224   }
225   
226   # trashes the .#blah files that cvs and svn leave behind when finding conflicts.
227   # this kind of assumes you've already checked them for any salient facts.
228   function clean_cvs_junk() {
229     for i in $*; do
230       find $i -follow -type f -iname ".#*" -exec perl $FEISTY_MEOW_SCRIPTS/files/safedel.pl {} ";" 
231     done
232   }
233   
234   # recreates all the generated files that the feisty meow scripts use.
235   function regenerate() {
236     bash $FEISTY_MEOW_SCRIPTS/core/bootstrap_shells.sh
237     echo
238     local wheres_nechung=$(which nechung 2>/dev/null)
239     if [ -z "$wheres_nechung" ]; then
240       echo "The nechung oracle program cannot be found.  You may want to consider"
241       echo "rebuilding the feisty meow applications with this command:"
242       echo "   bash $FEISTY_MEOW_DIR/scripts/generator/bootstrap_build.sh"
243     else
244       nechung
245     fi
246   }
247
248   function function_sentinel() { return 0; }
249   
250   if [ ! -z "$SHELL_DEBUG" ]; then echo function definitions end....; fi
251   
252 fi
253