Small changes to system and cPanel
[clearscm.git] / rc / functions
1 #!/bin/bash
2 ###############################################################################
3 #
4 # File:         $RCSfile: functions,v $
5 # Revision:     $Revision: 1.20 $
6 # Description:  Common bash functions
7 # Author:       Andrew@DeFaria.com
8 # Created:      Thu Jun  6 08:31:57 PDT 1996
9 # Modified:     $Date: 2013/03/26 20:38:23 $
10 # Language:     bash
11 #
12 # (c) Copyright 2000-2005, Andrew@DeFaria.com, all rights reserved.
13 #
14 ############################################################################### 
15 ESC=$(echo "\033")
16 CR=$(echo "\015")
17
18 view_name=
19
20 # Function to set the title bar. Works on the terminal emulators listed.
21 function title_bar {
22   if [ $# -gt 1 ]; then
23     ROOT=shift
24     ROOT="${NORMAL}\($ROOT\)"
25   fi
26   
27   prefix="$@"
28   # Change $HOME -> ~
29   if [ "${PWD#$HOME*}" != "$PWD" ]; then
30     current_dir="~${PWD#$HOME*}"
31   elif [ "$PWD" = "$HOME" ]; then
32     current_dir=~
33   else
34     current_dir="$PWD"
35   fi
36
37   # Remove view name
38   current_dir="${current_dir#/view/$view_name*}"
39   current_dir="${current_dir#/sview/$view_name*}"
40
41   # Add CVS/Root if there is one
42   if [ -f "CVS/Root" ]; then
43     current_dir="$current_dir ($(cat CVS/Root | tr -d $CR))"
44   fi
45
46   # Add GIT info if available
47   if in_git_repo; then
48     current_dir="$current_dir [git: $(git branch | sed -n -e 's/^\* \(.*\)/\1/p')]"
49   fi
50
51   if [ "$TERM" = "hpterm" -o \
52        "$TERM" = "hp"     -o \
53        "$TERM" = "2392A" ]; then
54     string=$(echo "${SYSNAME##*:}:$@")
55     echo -n "${ESC}&f0k${#string}D$string"
56   elif [ "$TERM" = "dtterm" -o \
57          "$TERM" = "vt221" ]; then
58     string=$(echo "${SYSNAME##*:}:$@")
59     echo -n "${ESC}]2;$string\007"
60   elif [ "$TERM" = "cygwin" -o \
61          "$TERM" = "vt100"  -o \
62          "$TERM" = "xterm"  -o \
63          "$TERM" = "xtermc" -o \
64          "$TERM" = "xterm-256color" ]; then
65     PS1="\[\e]0;$prefix$current_dir\007\]$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]"
66   fi
67 } # title_bar
68
69 # Function to set the icon name. Works on the terminal emulators listed.
70 function icon_name {
71   if [ "$TERM" = "hpterm" -o \
72        "$TERM" = "hp"     -o \
73        "$TERM" = "2392A" ]; then
74     string=$(echo "$1")
75     echo -n "${ESC}&f-1k${#string}D$string"
76   elif [ "$TERM" = "dtterm" -o \
77          "$TERM" = "vt100"  -a "$DTTERM" = "True" ]; then
78     # Note setting icon_name on vt100 overwrites the title bar so skip it
79     echo -n "${ESC}]1;$@\007"
80   fi
81 } # icon_name
82
83 # Sets both the title bar and the icon name. 
84 function title {
85   title_bar "$@"
86   icon_name "${SYSNAME##*:}"
87 } # title
88
89 # Sets title bar to machine name and path. Will include a view name if in a 
90 # view and a string to indicate that you are root.
91 function set_title {
92   if [ $($id -u) -eq 0 ]; then
93     root="Wizard "
94   else
95     root=
96   fi
97
98   view_name=$(scm pwv -short 2> /dev/null);
99
100   if [ $? -ne 0 -o -z "$view_name" ]; then
101     view_name='*NONE*'
102   fi
103
104   if [[ $view_name = *NONE* ]]; then
105     view_name=""
106     title_bar "$root"
107   else
108     title_bar "${root}View: $view_name: "
109   fi
110
111   icon_name "${SYSNAME##*:}"
112 } # set_title
113
114 # Sets prompt on terminals listed.
115 function set_prompt {
116   if [ $($id -u) -eq 0 ]; then
117     if [ "$TERM" = "hpterm"         -o \
118          "$TERM" = "hp"             -o \
119          "$TERM" = "2392A"          -o \
120          "$TERM" = "dtterm"         -o \
121          "$TERM" = "vt100"          -o \
122          "$TERM" = "cygwin"         -o \
123          "$TERM" = "xterm"          -o \
124          "$TERM" = "xtermc"         -o \
125          "$TERM" = "xterm-256color" -o \
126          "$TERM" = "vt220" ]; then
127       ROOT="\[${ROOT_COLOR}\]Wizard\[$NORMAL\] "
128     else
129       ROOT="Wizard "
130     fi
131   else
132     ROOT=""
133   fi
134
135   if [ "$TERM" = "vt100"          -o \
136        "$TERM" = "cygwin"         -o \
137        "$TERM" = "xterm"          -o \
138        "$TERM" = "xtermc"         -o \
139        "$TERM" = "xterm-256color" -o \
140        "$TERM" = "vt220" ]; then
141     PS1="$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]"
142   else
143     PS1="$ROOT$SYSNAME:"
144   fi
145   
146   set_title
147 } # set_prompt
148
149 # Function to override the normal cd command, setting title and prompt.
150 function mycd {
151   if [ -z "$1" ]; then
152     \cd ~
153   else
154     \cd "$1"
155   fi
156   set_title
157   set_prompt
158 } # mycd
159 export mycd
160
161 # Functions to override the normal push/popd commands, setting title and prompt.
162 function mypushd {
163   if [ -z "$1" ]; then
164     \pushd > /dev/null
165   else
166     \pushd "$1" > /dev/null
167   fi
168   set_title
169   set_prompt
170 } # mypushd
171
172 function mypopd {
173   if [ -z "$1" ]; then
174     cd - > /dev/null
175   else
176     \popd "$1" > /dev/null
177   fi
178   set_title
179   set_prompt
180 } # mypopd
181
182 # Function to override rlogin. Note that it fixes up the title and prompt 
183 # upon return.
184 function rlogin {
185   /usr/bin/rlogin "$@"
186   set_title
187   set_prompt
188 } # rlogin
189
190 # Function to override rsh. Note that it fixes up the title and prompt 
191 # upon return.
192 function rsh {
193   /usr/bin/rsh "$@"
194   set_title
195   set_prompt
196 } # rsh
197
198 # Function to override ssh. Note that it fixes up the title and prompt 
199 # upon return.
200 function ssh {
201   /usr/bin/ssh "$@"
202   set_title
203   set_prompt
204 } # ssh
205
206 function processRunning {
207   if [ $ARCHITECTURE = "FreeBSD" ]; then
208     psopts="-aux"
209   else
210     psopts="-ef"
211   fi
212
213   if [ $1 != '' ]; then
214     return $(ps $psopts | grep "$1" | grep -v "grep $1" | grep -v "grep -d skip" | wc -l)
215   fi
216 } # processRunning
217
218 function sj {
219   if [ $ARCHITECTURE = "FreeBSD" ]; then
220     psopts="-aux"
221   else
222     psopts="-ef"
223   fi
224
225   if [ $# = 0 ]; then
226     ps $psopts | $PAGER
227   else
228     for str; do
229       ps $psopts | grep "$str" | grep -ve "grep $str" -e "grep -d skip" -e "grep --color=auto"
230     done
231   fi
232 } # sj
233
234 function start_imap {
235   # Starts an ssh tunnel for IMAP
236   ssh -C -L 143:defaria.com:143 andrew@defaria.com
237 } # start_imap
238
239 function cmdline {
240   # Displays the command line from the /proc filesystem (if present)
241
242   me=$0;
243
244   if [ $# -ne 1 ]; then
245     error "Usage: cmdline <pid>"
246     return 1
247   fi
248
249   pid=$1;
250
251   if [ ! -d "/proc" ]; then
252     error "This OS has no /proc filesystem"
253     return 1
254   fi
255
256   if [ ! -d "/proc/$pid" ]; then
257     error "PID $pid does not exist"
258     return 1
259   fi
260
261   if [ ! -f "/proc/$pid/cmdline" ]; then
262     error "PID $pid has no cmdline!"
263     return 1
264   fi
265
266   cat /proc/$pid/cmdline | tr -c [:print:] " "
267   display
268 } # cmdline
269
270 function user {
271   processRunning ypbind;  ypbind=$?
272   processRunning winbind; winbind=$?
273
274   if [ $# -gt 0 ]; then
275     if [ $ypbind -gt 0 ]; then
276       ypcat passwd | grep -i $@
277     elif [ $winbind -gt 0 ]; then
278       for user in $(wbinfo -u | grep -i $@); do
279         wbinfo --user-info $user
280       done
281     fi
282   else
283     if [ $ypbind -gt 0 ]; then
284       ypcat passwd | $PAGER
285     elif [ $wbind -gt 0 ]; then
286       for user in $(wbinfo -u); do
287         wbinfo --user-info $user
288       done | $PAGER
289     fi
290   fi
291 } # user
292
293 function group {
294   processRunning ypbind;  ypbind=$?
295   processRunning winbind; winbind=$?
296
297   if [ $# -gt 0 ]; then
298     if [ $ypbind -gt 0 ]; then
299       ypcat group | grep -i $@
300     elif [ $winbind -gt 0 ]; then
301       for group in $(wbinfo -g | grep -i $@); do
302         wbinfo --group-info $group
303       done
304     fi
305   else
306     if [ $ypbind -gt 0 ]; then
307       ypcat group | $PAGER
308     elif [ $winbind -gt 0 ]; then
309       for group in $(wbinfo -g); do
310         wbinfo --group-info $group
311       done | $PAGER
312     fi
313   fi
314 } # group