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