forked from expectedbehavior/common-files
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bashrc
487 lines (415 loc) · 16.1 KB
/
.bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# This file is sourced by all bash shells on startup, whether interactive
# or not. This file *should generate no output* or it will break the
# scp and rcp commands.
[[ -f /etc/bash_completion ]] && source /etc/bash_completion
[[ -f $HOME/.bash_completions/git-completion ]] && source $HOME/.bash_completions/git-completion
export INPUTRC="$HOME/.inputrc"
export EDITOR="emacsclient --alternate-editor=emacs"
export GLOBIGNORE='.:..'
export HISTTIMEFORMAT='%c '
export LC_COLLATE="POSIX"
export ACKRC="$HOME/.ackrc"
export ARCHFLAGS="-arch x86_64"
# export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/mysql/lib
# remove the ':' from wordbreaks so we don't have to escape it on teh command line
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
alias ls='ls --color=auto -F -b -T 0'
alias ll='ls -lh --color=auto -F -b -T 0'
alias lobster="lobster.telaranrhiod.com"
#alias su='su -'
alias la='ls -alh'
alias es='eix'
alias eS='eix -S'
#alias sS='screen -S'
#alias sx='screen -x'
alias sls='screen -ls'
alias sw='screen -wipe'
#alias cfup='((svn info &> /dev/null && svn up) || (echo; echo -n "svn repository not detected, use tbz2? [Y,n]: "; read y; [ "$y" == "" -o "$y" == "y" -o "$y" == "Y" ] && (wget -O - http://cf.telaranrhiod.com/files/common/common_files.tbz2 | tar -xjov --no-same-permissions ./))); exec bash'
alias bgup='(wget -O - http://cf.telaranrhiod.com/files/common/backgrounds.tbz2 | tar -xjov --no-same-permissions -C ~/.fluxbox/backgrounds/)'
alias pgrep='pgrep -iL'
which md5 &> /dev/null || alias md5='md5sum'
alias g='git'
alias g{='git stash'
alias g}='git stash apply'
alias glg='git log --graph --pretty=format:"%C(bold red)%h%Creset -%C(yellow)%d%Creset %s %C(yellow)-%Creset %C(bold blue)%an%Creset %C(bold green)(%cr)%Creset"'
complete -o default -o nospace -F _git_log glg
alias gdc='git diff --cached'
complete -o default -o nospace -F _git_diff gdc
alias gcm='git commit -m'
alias gc="git commit"
alias gca='git commit -a'
alias gcam='git commit -a -m'
alias gs='git status'
alias gst='git status'
alias gco="git checkout"
alias gu="git reset HEAD"
alias ghr="git log -n1 --pretty=format:'%C(bold red)%h%Creset'"
alias gpp="git pull && git push"
complete -o default -o nospace -F _git_checkout gco
alias gpul="git pull"
complete -o default -o nospace -F _git_pull gpull
alias gpsh="git push"
complete -o default -o nospace -F _git_push gpush
alias gd="git diff"
complete -o default -o nospace -F _git_diff gd
alias gbr="git branch"
complete -o default -o nospace -F _git_branch gbr
alias ga="git add"
complete -o default -o nospace -F _git_add ga
alias cuwork="cucumber ./features -t @shouldwork"
alias cuwip="cucumber ./features -t @wip"
alias cufail="cucumber ./features -t @shouldfail"
alias cuke="cucumber ./features"
alias sc='script/console'
alias sct='RAILS_ENV="test" sc'
alias ss='script/server'
alias ackp='ack --pager="less -r"'
alias acki='ack -i'
alias a='acki'
alias hn='hcl note'
alias be='bundle exec'
export CF_TARBALL_BACKUP="true"
export CF_BACKUP_COUNT=5
cfup() {
if [[ "$PWD" != "$HOME" ]]; then
#you aren't in your home directory, prompt for continuing
echo
echo "You do not appear to be in your home directory"
echo -n "Do you wish to continue? [Y,n]: "
read
if ! [[ "$REPLY" == "" || "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
return 1
fi
fi
if svn info &> /dev/null; then
#is repos
if [[ "`svn info | grep '/svn/home/common_files'`" == "" ]]; then
#repos isn't common files
echo
echo "The repository in the current directory does not appear to be the common files."
echo -n "Do you wish to use the tarball? [Y,n]: "
read
if ! [[ "$REPLY" == "" || "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
return 3
fi
else
svn up
if [[ "$?" != "0" || "`svn stat | grep '^C'`" != "" ]]; then
echo
echo "Error updating repository, check above for more info."
return 4
fi
return 0
fi
else
#curr dir isn't repos
echo
echo -n "svn repository not detected, use tbz2? [Y,n]: "
read y
if ! [[ "$y" == "" || "$y" == "y" || "$y" == "Y" ]]; then
return 5
fi
fi
if [[ "$CF_TARBALL_BACKUP" == "true" ]]; then
TEMPDIR=`mktemp -d -t cf_backup_tmp.XXXXXXXXXXXXXX` &&
wget -O - http://cf.telaranrhiod.com/files/common/common_files.tbz2 | tar -xjov --no-same-permissions -C $TEMPDIR ./ &&
date="`date '+%Y-%m-%d--%H-%M-%S'`" &&
version=`echo $CF_RUNNING_VERSION | awk '{print $1}'` &&
backup_path="$HOME/.common_files/backups/${date}--r${version}/" &&
mkdir -p $backup_path &&
echo "moving new files into place and backing up old files to $backup_path" &&
rsync -av -b --backup-dir=$backup_path $TEMPDIR/ ./ &&
rm -rv $TEMPDIR &&
# cp --parents `find | grep '.*.cf.bkp$' | grep -v '\.common_files/backups/'` $backup_path &&
# rm -rf `find | grep '.*.cf.bkp$' | grep -v '\.common_files/backups/'` &&
if [[ "$CF_BACKUP_COUNT" != "" && "$CF_BACKUP_COUNT" -ge "0" ]] &> /dev/null; then
old_backups="`ls $HOME/.common_files/backups/ | sort | head -n -${CF_BACKUP_COUNT}`"
if [[ "$old_backups" != "" ]]; then
echo "Removing old backups ($old_backups) due to a CF_BACKUP_COUNT of $CF_BACKUP_COUNT"
(cd $HOME/.common_files/backups/ && rm -rf $old_backups)
fi
fi
else
wget -O - http://cf.telaranrhiod.com/files/common/common_files.tbz2 | tar -xjov --no-same-permissions ./
fi
if [[ "$?" != "0" ]]; then
echo
echo "Error downloading or extracting tar, check above for more info."
return 2
fi
echo
echo "Performing 'exec bash' to pick up updates."
exec bash
}
sS() {
if [[ "$2" != "" ]]; then
ssh -t $2 screen -S $1
else
screen -S $1
fi
}
sx() {
if [[ "$2" != "" ]]; then
ssh -t $2 screen -x $1
else
screen -x $1
fi
}
psg() {
ps aux | grep "$*" | grep -v "grep .*$*"
}
known_hosts_temp_file=~/.ssh/temp_known_hosts_file
set_temp_known_host() {
rm ${known_hosts_temp_file} &> /dev/null
# we use xxx because we want this to fail to login, just set the known host
ssh -o 'StrictHostKeyChecking=no' -o 'PreferredAuthentications="xxx"' -o "UserKnownHostsFile=${known_hosts_temp_file}" $* &> /dev/null
}
pssh() {
set_temp_known_host $*
cat ${known_hosts_temp_file} >> ~/.ssh/known_hosts
ssh $*
}
tssh() {
set_temp_known_host $*
ssh -o "UserKnownHostsFile=${known_hosts_temp_file}" $*
}
vncvia() {
if [[ "$*" == "" ]]; then
echo "usage: vncvia [user@]remotehost [other vnc options...]"
else
echo "vncviewer -via $* localhost"
vncviewer -via $* localhost
fi
}
alias cd='pushd -n $PWD &> /dev/null; cd'
cf_cd() {
pushd -n "$PWD" &> /dev/null
cd "$@" || popd -n &> /dev/null
}
#alias cd='cf_cd' # put this below cf_cd so when cf_cd is read 'cd' isn't expanded making it recursive
#min seconds between notifications of new common files.
export CF_TIME_BETWEEN_NOTIFICATIONS=86400
last_notified_date_path="$HOME/.common_files/.out_of_date_last_notified_date"
notification_message_path="$HOME/.common_files/.out_of_date_notification_message"
cf_date_check_notify() {
#if it isn't the same day as the last time we told you and
#we found that you were out of date last time you logged in we're going to tell you about it now.
last_date=0
[ -f $last_notified_date_path ] && last_date=`cat $last_notified_date_path`
let "new_date = $last_date + $CF_TIME_BETWEEN_NOTIFICATIONS"
if [[ "$new_date" -lt "`date '+%s'`" ]]; then
[ -f $notification_message_path ] && cat $notification_message_path && echo "`date '+%s'`" > $last_notified_date_path
fi
}
cf_get_latest_local_version() {
#get your current revision number
if which git &> /dev/null; then
my_rev=`(git --git-dir=$HOME/.git log -1 --pretty=format:"%H") 2> /dev/null`
fi
if [[ "$my_rev" == "" ]]; then
#couldn't get version from svn so we'll try .common_files/latest_revision.txt
my_rev=`cat "$HOME/.common_files/.latest_revision" 2> /dev/null`
fi
if [[ "$my_rev" == "" ]]; then
return 1
fi
CF_LOCAL_LATEST_VERSION=$my_rev
return 0
}
cf_get_latest_local_version
CF_RUNNING_VERSION="$CF_LOCAL_LATEST_VERSION"
CF_LOCAL_LATEST_VERSION="$CF_RUNNING_VERSION";
cf_check_for_updates() {
#first paren executes rest in subshell so we don't see the output from the job finishing
#second peren and its & lump this block together and execute it in the background to it happens asynchronously and we don't hold up shell startup
((
#checking to see if you're up to date
#make sure you have curl and svn
if which git curl &> /dev/null; then
#get the latest revision number, this should just be an integer.
#latest=`curl -sL http://cf.telaranrhiod.com/files/common/latest_revision.txt`
latest=`curl -sL http://github.com/api/v2/yaml/repos/show/naginata/common-files/branches | grep master | sed -e 's/master: //'`
#make sure curl returned successfully
if [[ "$?" == "0" ]]; then
# my_rev="`cf_get_latest_local_version`"
cf_get_latest_local_version
my_rev=$CF_LOCAL_LATEST_VERSION
#check if you're up to date
latest_hash=`echo $latest | awk '{print $1}'`
my_hash=`echo $my_rev | awk '{print $1}'`
if [[ "$latest_hash" != "$my_hash" ]]; then
#if not, create the .out_of_date file with the appropriate message so next time you start a terminal we can alert you.
echo "You have deviated from nfork/master. Remote: $latest, You: $my_rev" > $notification_message_path
else
#if you're up to date we don't need this file
[ -f $notification_message_path ] && rm $notification_message_path
fi
fi
fi
) &)
}
#min seconds between checking for new common files.
export CF_TIME_BETWEEN_UPDATES=86400
cf_date_check_for_updates() {
last_checked_for_updates_date_path="$HOME/.common_files/.last_checked_date"
last_date=0
[ -f $last_checked_for_updates_date_path ] && last_date=`cat $last_checked_for_updates_date_path`
let "new_date = $last_date + $CF_TIME_BETWEEN_UPDATES"
if [[ "$new_date" -lt "`date '+%s'`" ]]; then
cf_check_for_updates
echo "`date '+%s'`" > $last_checked_for_updates_date_path
fi
}
cf_prompt_command() {
# old_hist_time_format=$HISTTIMEFORMAT
# HISTTIMEFORMAT='%s '
# hist_cmd=`history 1`
# HISTTIMEFORMAT=$old_hist_time_format
# history 10
# begin_hist=`begin_hist_grep $hist_cmd`
# history -p '!!:1'
# set | grep sleep
# foo=">${hist_cmd/$begin_hist $BASH_COMMAND/}--$begin_hist--$BASH_COMMAND<"
# echo $foo
# [[ "${hist_cmd/$begin_hist $BASH_COMMAND/}" == "" ]] && echo "hooray"
# history 10
history -a
# history 10
((cf_date_check_for_updates) &)
cf_date_check_notify
[[ "$CF_RUNNING_VERSION" != "$CF_LOCAL_LATEST_VERSION" ]] && exec bash
# cf_long_running_task_check
[[ "`declare -f cf_user_prompt_hook`" != "" ]] && cf_user_prompt_hook
# the following line has gotten incredibly slow, but I'm not ready to give up on the concept, so I'm commenting. Deal with it.
# BATTERY_PERCENT="$(ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf("%.2f%%", $10/$5 * 100)}')"
BATTERY_PERCENT="$(pmset -g batt | grep 'InternalBattery' | awk '{printf("%.2f%%", $3)}')"
# BATTERY_PERCENT=66
let BATTERY="$(echo -n ${BATTERY_PERCENT} | sed 's/\...%//')"
#does your computer report 99.5% instead of 100%? You might find the following more useful than sed rounding.
#let BATTERY="$(echo -n ${BATTERY_PERCENT} | sed 's/[^0-9]//g')"
let HOST_LENGTH="`expr length ${HOST}`"
if [ $BATTERY -gt 50 ];then
ILLUMINATE=($BATTERY-50)*$HOST_LENGTH/50
GREEN_PART=${HOST:0:${ILLUMINATE}}
YELLOW_PART=${HOST:${ILLUMINATE}:${HOST_LENGTH}-${ILLUMINATE}}
RED_PART=""
else
ILLUMINATE=(2*$BATTERY*$HOST_LENGTH)/100
YELLOW_PART=${HOST:0:${ILLUMINATE}}
RED_PART=${HOST:${ILLUMINATE}:${HOST_LENGTH}-${ILLUMINATE}}
GREEN_PART=""
fi
}
#export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;
#35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;
#31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;
#31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jpg=01;
#35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;
#35:*.tif=01;35:*.png=01;35:*.mpg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;
#35:*.dl=01;35::'
#Keeps the terminal from doing funky wrapping stuff
shopt -s checkwinsize
#append to history file on exit instead of overwriting
shopt -s histappend
#don't try to tab complete an empty line
shopt -s no_empty_cmd_completion
#correct minor directory misspellings
shopt -s cdspell
#let * match files beginning with '.' but since GLOBIGNORE is set above it won't match '.' or '..'
shopt -s dotglob
FG_BLACK="\[\033[00;30m\]"
FG_RED="\[\033[00;31m\]"
FG_GREEN="\[\033[00;32m\]"
FG_YELLOW="\[\033[00;33m\]"
FG_BLUE="\[\033[00;34m\]"
FG_VIOLET="\[\033[00;35m\]"
FG_CYAN="\[\033[00;36m\]"
FG_WHITE="\[\033[00;37m\]"
NO_COLOR="\[\e[0m\]"
WHOAMI="`/usr/bin/whoami`"
HOST="${WHOAMI}@$(echo $HOSTNAME | sed 's/\..*//')"
last_2_pwd(){
tmp=${PWD%/*/*}
[ ${#tmp} -gt 0 -a "$tmp" != "$PWD" ] && echo ${PWD:${#tmp}+1} || echo $PWD
}
nice_pwd(){
echo "$(last_2_pwd | sed -e s/\\\/Users\\\/${WHOAMI}/~/ | sed -e s/${WHOAMI}/~/)"
}
svn_crap(){
if [ `ls -a | grep .svn | wc -l` != 0 ];then
REVISION=`svn info | grep Revision | sed -e 's/Revision: //'`
MODS=`svn status | grep ^[^?] | wc -l`
if [ $MODS -gt 0 ];then
echo -ne "${REVISION}+${MODS}"
else
echo -ne "${REVISION}"
fi
fi
}
#make eterm into xterm for emacs/ssh purposes
if [[ "$TERM" = "eterm-color" ]]; then
export CF_REAL_TERM=$TERM
export TERM="xterm-color"
fi
#build PS1
#don't set PS1 for dumb terminals
if [[ "$TERM" != 'dumb' ]] && [[ -n "$BASH" ]]; then
#don't modify titlebar on console
[[ "$TERM" != 'linux' && "$CF_REAL_TERM" != "eterm-color" ]] && PS1="${PS1}\[\e]2;\u@\H:\W\a"
GIT_PS1_SHOWDIRTYSTATE=1
PS1="${FG_GREEN}\${GREEN_PART}${FG_YELLOW}\${YELLOW_PART}${FG_RED}\${RED_PART} ${FG_YELLOW}\$(nice_pwd)${FG_CYAN} \$(__git_ps1 "[%s]")\$(svn_crap)"
#use a red $ if you're root, white otherwise
if [[ $WHOAMI = "root" ]]; then
#red prompt
PS1="${PS1}${FG_RED} \$ ${FG_WHITE}"
else
#green prompt
PS1="${PS1}${FG_WHITE} \$ "
fi
fi
if [[ $WHOAMI = 'root' ]]; then
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:${ROOTPATH}"
else
export PATH="/bin:/usr/bin:${PATH}"
fi
#make eterm into xterm for emacs/ssh purposes
if [[ "$TERM" = "eterm-color" ]]; then
export TERM="xterm-color"
fi
if [[ $WHOAMI = 'root' ]]; then
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:${ROOTPATH}"
else
export PATH="/bin:/usr/bin:${PATH}"
fi
#add mongo to path
export PATH="/mongo/mongodb/bin:${PATH}"
#coreutils
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
[[ "$HOME" == "" ]] && export HOME=`grep -e "^[^:]*\:[^:]*\:$UID\:" /etc/passwd | awk -F ':' '{print $6}'`
export BC_ENV_ARGS="$HOME/.bcrc"
#Extend command history
export HISTSIZE=5000000
#Repeated commands are only stored once
export HISTCONTROL=ignoreboth
#save history with each command
export PROMPT_COMMAND='[[ "`set | grep -E \"cf_prompt_command \(\)\"`" != "" ]] && cf_prompt_command'
if ! shopt -q login_shell; then
if [ -f /usr/bin/keychain ]; then
[ -f ~/.ssh/id_dsa ] && /usr/bin/keychain --noask ~/.ssh/id_dsa &> /dev/null
[ -f ~/.ssh/id_rsa ] && /usr/bin/keychain --noask ~/.ssh/id_rsa &> /dev/null
fi
[ -f ~/.keychain/$HOSTNAME-sh ] && source ~/.keychain/$HOSTNAME-sh > /dev/null &> /dev/null
fi
# load any OS specific changes we've made
[ -f ~/.common_files/cf.`uname -s`.conf ] && . ~/.common_files/cf.`uname -s`.conf
#last, but not least, source a configuration file so there's an easy place for users to make configuration changes from the default
[ -f ~/.common_files/cf.conf ] && . ~/.common_files/cf.conf
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
if [[ -s ~/.rvm/scripts/rvm ]]; then
source ~/.rvm/scripts/rvm
rvm use default > /dev/null
fi
[ -f ~/.bundler-exec.sh ] && source ~/.bundler-exec.sh
source ~/.cfcustom