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