first check-in of feisty meow codebase. many things broken still due to recent
[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 # applies a chown and a chgrp to the files specified, but the user name must
10 # have a private group of the same name for this to work.
11 function chowngrp {
12   chown $*
13 # $2 $3 $4 $5 $6 $7 $8 $9
14   chgrp $*
15 # $2 $3 $4 $5 $6 $7 $8 $9
16 }
17
18 # makes a directory of the name specified and then tries to change the
19 # current directory to that directory.
20 function mcd {
21   if [ ! -d "$1" ]; then mkdir "$1"; fi
22   cd "$1"
23 }
24
25 # locates a process given a search pattern to match in the process list.
26 function psfind {
27   PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
28   appropriate_pattern='s/^[-a-zA-Z_0-9][-a-zA-Z_0-9]*  *\([0-9][0-9]*\).*$/\1/p'
29     # pattern to use for peeling off the process numbers.
30   extra_flags=
31     # flags to pass to ps if any special ones are needed.
32   if [ "$OS" = "Windows_NT" ]; then
33     # on win32, there is some weirdness to support msys.
34     appropriate_pattern='s/^[   ]*\([0-9][0-9]*\).*$/\1/p'
35     extra_flags=-W
36   fi
37   /bin/ps $extra_flags wuax >$PID_DUMP
38   # remove the first line of the file, search for the pattern the
39   # user wants to find, and just pluck the process ids out of the
40   # results.
41   PIDS_SOUGHT=$(cat $PID_DUMP \
42     | sed -e '1d' \
43     | grep -i "$1" \
44     | sed -n -e "$appropriate_pattern")
45   if [ ! -z "$PIDS_SOUGHT" ]; then echo "$PIDS_SOUGHT"; fi
46   /bin/rm $PID_DUMP
47 }
48
49 # finds all processes matching the pattern specified and shows their full
50 # process listing (whereas psfind just lists process ids).
51 function psa {
52   p=$(psfind "$1")
53   if [ ! -z "$p" ]; then
54     echo ""
55     echo "Processes containing \"$1\"..."
56     echo ""
57     if [ -n "$IS_DARWIN" ]; then
58       unset fuzil_sentinel
59       for i in $p; do
60         # only print the header the first time.
61         if [ -z "$fuzil_sentinel" ]; then
62           ps $i -w -u
63         else
64           ps $i -w -u | sed -e '1d'
65         fi
66         fuzil_sentinel=true
67       done
68     else 
69       # cases besides darwin OS (for macs).
70       extra_flags=
71       if [ "$OS" = "Windows_NT" ]; then
72         # special case for windows.
73         extra_flags=-W
74         ps | head -1
75         for curr in $p; do
76           ps $extra_flags | grep "^ *$curr" 
77         done
78       else
79         # normal OSes can handle a nice simple query.
80         ps wu $p
81       fi
82     fi
83   fi
84 }
85
86 # an unfortunately similarly named function to the above 'ps' as in process
87 # methods, but this 'ps' stands for postscript.  this takes a postscript file
88 # and converts it into pcl3 printer language and then ships it to the printer.
89 # this mostly makes sense for an environment where one's default printer is
90 # pcl.  if the input postscript causes ghostscript to bomb out, there has been
91 # some good success running ps2ps on the input file and using the cleaned
92 # postscript file for printing.
93 function ps2pcl2lpr {
94   for $i in $*; do
95     gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l 
96   done
97 }
98
99 function fix_alsa {
100   sudo /etc/init.d/alsasound restart
101 }
102
103 # switches from a /X/path form to an X:/ form.
104 function msys_to_dos_path() {
105   # we always remove dos slashes in favor of forward slashes.
106   echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
107 }
108
109 # switches from an X:/ form to an /X/path form.
110 function dos_to_msys_path() {
111   # we always remove dos slashes in favor of forward slashes.
112   echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
113 }
114
115 if [ ! -z "$SHELL_DEBUG" ]; then echo function definitions end....; fi
116