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