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