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