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