some changes required by naggy baby ms-windows, which can't sit up straight or feed...
[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 appropriate_pattern='s/^.*  *\([0-9][0-9]*\) *$/\1/p'
61       for i in "${patterns[@]}"; do
62         PIDS_SOUGHT+=$(cat $PID_DUMP \
63           | grep -i "$i" \
64           | sed -n -e "$appropriate_pattern")
65         if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
66           # we want to bail as soon as we get matches, because on the same
67           # platform, the same set of patterns should work to find all
68           # occurrences of the genesis java.
69           break;
70         fi
71       done
72     else
73       /bin/ps $extra_flags wuax >$PID_DUMP
74       # pattern to use for peeling off the process numbers.
75       local appropriate_pattern='s/^[-a-zA-Z_0-9][-a-zA-Z_0-9]*  *\([0-9][0-9]*\).*$/\1/p'
76       # remove the first line of the file, search for the pattern the
77       # user wants to find, and just pluck the process ids out of the
78       # results.
79       for i in "${patterns[@]}"; do
80         PIDS_SOUGHT=$(cat $PID_DUMP \
81           | sed -e '1d' \
82           | grep -i "$i" \
83           | sed -n -e "$appropriate_pattern")
84         if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
85           # we want to bail as soon as we get matches, because on the same
86           # platform, the same set of patterns should work to find all
87           # occurrences of the genesis java.
88           break;
89         fi
90       done
91     fi
92     if [ ! -z "$PIDS_SOUGHT" ]; then echo "$PIDS_SOUGHT"; fi
93     /bin/rm $PID_DUMP
94   }
95   
96   # finds all processes matching the pattern specified and shows their full
97   # process listing (whereas psfind just lists process ids).
98   function psa() {
99     p=$(psfind "$1")
100     if [ ! -z "$p" ]; then
101       echo ""
102       echo "Processes containing \"$1\"..."
103       echo ""
104       if [ -n "$IS_DARWIN" ]; then
105         unset fuzil_sentinel
106         for i in $p; do
107           # only print the header the first time.
108           if [ -z "$fuzil_sentinel" ]; then
109             ps $i -w -u
110           else
111             ps $i -w -u | sed -e '1d'
112           fi
113           fuzil_sentinel=true
114         done
115       else 
116         # cases besides darwin OS (for macs).
117         extra_flags=
118         if [ "$OS" = "Windows_NT" ]; then
119           # special case for windows.
120           extra_flags=-W
121           ps | head -1
122           for curr in $p; do
123             ps $extra_flags | grep "^ *$curr" 
124           done
125         else
126           # normal OSes can handle a nice simple query.
127           ps wu $p
128         fi
129       fi
130     fi
131   }
132   
133   # an unfortunately similarly named function to the above 'ps' as in process
134   # methods, but this 'ps' stands for postscript.  this takes a postscript file
135   # and converts it into pcl3 printer language and then ships it to the printer.
136   # this mostly makes sense for an environment where one's default printer is
137   # pcl.  if the input postscript causes ghostscript to bomb out, there has been
138   # some good success running ps2ps on the input file and using the cleaned
139   # postscript file for printing.
140   function ps2pcl2lpr() {
141     for $i in $*; do
142       gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l 
143     done
144   }
145   
146   function fix_alsa() {
147     sudo /etc/init.d/alsasound restart
148   }
149   
150   # switches from a /X/path form to an X:/ form.
151   function msys_to_dos_path() {
152     # we always remove dos slashes in favor of forward slashes.
153     echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
154   }
155   
156   # switches from an X:/ form to an /X/path form.
157   function dos_to_msys_path() {
158     # we always remove dos slashes in favor of forward slashes.
159     echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
160   }
161   
162   # su function: makes su perform a login.
163   # for some OSes, this transfers the X authority information to the new login.
164   function su() {
165     # decide if we think this is debian or ubuntu or a variant.
166     DEBIAN_LIKE=$(if [ ! -z "$(grep -i debian /etc/issue)" \
167         -o ! -z "$(grep -i ubuntu /etc/issue)" ]; then echo 1; else echo 0; fi)
168   
169     if [ $DEBIAN_LIKE -eq 1 ]; then
170       # debian currently requires the full version which imports X authority
171       # information for su.
172   
173       # get the x authority info for our current user.
174       source $FEISTY_MEOW_SCRIPTS/x_win/get_x_auth.sh
175   
176       if [ -z "$X_auth_info" ]; then
177         # if there's no authentication info to pass along, we just do a normal su.
178         /bin/su -l $*
179       else
180         # under X, we update the new login's authority info with the previous
181         # user's info.
182         (unset XAUTHORITY; /bin/su -l $* -c "$X_auth_info ; export DISPLAY=$DISPLAY ; bash")
183       fi
184     else
185       # non-debian supposedly doesn't need the extra overhead any more.
186       # or at least suse doesn't, which is the other one we've tested on.
187       /bin/su -l $*
188     fi
189   
190     # relabel the console after returning.
191     bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
192   }
193   
194   # sudo function wraps the normal sudo by ensuring we replace the terminal
195   # label if they're doing an su with the sudo.
196   function sudo() {
197     local first_command="$1"
198     /usr/bin/sudo $*
199     if [ "$first_command" == "su" ]; then
200       # yep, they were doing an su, but they're back now.
201       bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
202     fi
203   }
204   
205   # buntar is a long needed uncompressing macro that feeds into tar -x.
206   # it takes a list of bz2 file names and extracts their contents into
207   # sequentially numbered directories.
208   function buntar() {
209     index=1
210     for i in $*; do
211       mkdir buntar_$index
212       pushd buntar_$index &>/dev/null
213       file=$i
214       # if the filename has no directory component, we will assume it used to
215       # be above our unzipping directory here.
216       if [ "$(basename $file)" = $file ]; then
217         file=../$file
218       fi
219       bunzip2 -d -c $file | tar -xf -
220       popd &>/dev/null
221       index=$(expr $index + 1)
222     done
223   }
224   
225   # trashes the .#blah files that cvs and svn leave behind when finding conflicts.
226   # this kind of assumes you've already checked them for any salient facts.
227   function clean_cvs_junk() {
228     for i in $*; do
229       find $i -follow -type f -iname ".#*" -exec perl $FEISTY_MEOW_SCRIPTS/files/safedel.pl {} ";" 
230     done
231   }
232   
233   # recreates all the generated files that the feisty meow scripts use.
234   function regenerate() {
235     bash $FEISTY_MEOW_SCRIPTS/core/bootstrap_shells.sh
236     echo
237     local wheres_nechung=$(which nechung 2>/dev/null)
238     if [ -z "$wheres_nechung" ]; then
239       echo "The nechung oracle program cannot be found.  You may want to consider"
240       echo "rebuilding the feisty meow applications with this command:"
241       echo "   bash $FEISTY_MEOW_DIR/scripts/generator/bootstrap_build.sh"
242     else
243       nechung
244     fi
245   }
246
247   function function_sentinel() { return 0; }
248   
249   if [ ! -z "$SHELL_DEBUG" ]; then echo function definitions end....; fi
250   
251 fi
252