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