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