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