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