Skip to content

Instantly share code, notes, and snippets.

@brandondrew
Created December 18, 2009 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandondrew/259348 to your computer and use it in GitHub Desktop.
Save brandondrew/259348 to your computer and use it in GitHub Desktop.
#!/bin/bash
###########################################################
# TRIBUNE-SPECIFIC BUILD HOME script:
# This script sets up your home directory fast with basic
# necessities and amenities, to give you sanity when you
# work on a Unix box where you don't have root access.
###########################################################
# TO USE:
# curl https://raw.github.com/gist/259348/build_home.sh | bash
# OR:
# wget https://raw.github.com/gist/259348/build_home.sh | bash
###########################################################
DATE=`date '+%Y%m%d'`
# TODO: clean this up with bash's version of files.each :p
# clean-up previous installations in case they exist:
mv .bash_profile{,.$DATE} 2> /dev/null
mv .keep_alive{,.$DATE} 2> /dev/null
mv .aliases{,.$DATE} 2> /dev/null
mv .functions{,.$DATE} 2> /dev/null
mv .prompt{,.$DATE} 2> /dev/null
mv .variables{,.$DATE} 2> /dev/null
mv .vimrc{,.$DATE} 2> /dev/null
mv .ackrc{,.$DATE} 2> /dev/null
mv .solaris{,.$DATE} 2> /dev/null
mv .profile{,.$DATE} 2> /dev/null #for crazy Solaris boxes with ksh :p
mv .bashrc{,.$DATE} 2> /dev/null #TODO: check whether I ever need this
mv .local{,.$DATE} 2> /dev/null
mv .screenrc{,.$DATE} 2> /dev/null
mv .inputrc{,.$DATE} 2> /dev/null
rm bin/rebuild 2> /dev/null
rm bin/follow 2> /dev/null
rm bin/protect_session 2> /dev/null
# in case there are local-only settings we don't want to overwrite
# no other file should have been customized locally
# TODO: delete .local`datestamp` file if no different from the new one
# TODO: ACTUALLY, do that with all of them!
###########################################################
# no quotes on END so variables are interpolated:
cat << END >> .bash_profile
source $HOME/.keep_alive
source $HOME/.aliases
source $HOME/.functions
source $HOME/.prompt
source $HOME/.variables
source $HOME/.local
END
echo "Created new .bash_profile"
###########################################################
cat << 'END' >> .keep_alive
# keep alive #
#~/bin/protect_session &
END
echo "Created new .keep_alive"
###########################################################
cat << ENDALIASES >> .aliases
# editors #
alias e=vi
alias vi='vim -u $HOME/.vimrc' # experimental
# useful when I source my .bash_profile as another user
# ls aliases #
alias hal='ls -hal'
alias dhal='ls -dhal'
alias lsd='ls -d'
# cd aliases #
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
alias .......='cd ../../../../../..'
alias huntfor='find / -name $1 2> /dev/null 1> $2 &'
ENDALIASES
echo "Created new .aliases"
###########################################################
cat << 'END' >> .functions
function word() {
cat /usr/share/dict/words | grep $1
}
function where() {
whereis $1 | cut -d':' -f2 | awk '{ print $1 }'
}
function process() {
ps -ef$2 | grep $1 | grep -v 'grep'
}
# log `cd`: easier to pick up where you left off
function cd() {
builtin cd $@
echo $(pwd) >> "$HOME/.dirs"
}
END
echo "Created new .functions"
###########################################################
cat << 'ENDPROMPT' >> .prompt
ID='\[\033[0;32m\]`echo "$LOGNAME"`@\h'
ZONE='`date "+%Z"`'
TIME='\[\033[0;34m\]\T'
DIRECTORY='\[\033[0;36m\]\w\[\033[00m\]'
COLON='\n>: '
LOCAL_COLOR_ON='\[\033[40;0;33m\]'
LOCAL_COLOR_OFF='\[\033[0m\]'
export PS1="${LOCAL_COLOR_ON}$(echo -en '${LOCAL_PROMPT_OPEN}')${LOCAL_COLOR_OFF}${ID} $TIME $ZONE $DIRECTORY ${LOCAL_COLOR_ON}$(echo -en '${LOCAL_PROMPT_CLOSE}')${LOCAL_COLOR_OFF}${COLON}"
# define LOCAL_PROMPT_OPEN and LOCAL_PROMPT_CLOSE in your .local file, to insert info pertinent to the local box
# these variables can change on the fly and will still be updated in your prompt
# because it is so hard (impossible) to set the color locally and make it work,
# it is set here. Sorry, you're stuck with yellow. :p
ENDPROMPT
echo "Created new .prompt"
###########################################################
cat << 'ENDVARIABLES' >> .variables
# ENVIRONMENT VARIABLES #
export PATH=$PATH:~/bin:/usr/sfw/bin:~/usr/bin:/home/dbutler/bin:/www/TII/turbine.lib/native/bin:~/.ruby/bin
export GEM_HOME=~/.gems
export TZ=America/Chicago
#export RUBYOPT=rubygems
ENDVARIABLES
echo "Created new .variables"
###########################################################
if [[ ! -f .local ]]; then
cat << 'ENDLOCAL' >> .local
# from the ksh .profile for the Tribine Solaris boxes:
MAIL=/usr/mail/${LOGNAME:?}
alias turbine='sudo su - turbine -c "bash --rcfile /home/bzylstra/.bash_profile"'
stty erase ^?
ENDLOCAL
echo "Created new .local"
fi
###########################################################
cat << ENDBASHRC >> .bashrc
source $HOME/.bash_profile
ENDBASHRC
echo "Created new .bashrc"
###########################################################
cat << 'ENDSCREEN' >> .screenrc
# escape ^Zz
escape \e``
# get rid of the annoying splash screen:
startup_message off
# these provide a visual reminder we're in screen, and good info:
caption always "%{= BW}%4n %t%? (%u)%?%?: %h%?"
caption alwayslastline
hardstatus alwayslastline
backtick 1 0 0 uname -sr # platform architecture
backtick 2 0 0 echo "$USER"
# EXPLANATION: green user host arc ? white TIME
hardstatus string '%{= kG}[ %2` %{G}%H (%1`) %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][ %{W}%c %{g}]'
# REMOVED FROM HARDSTATUS: DATE
# %{B}%Y-%m-%d
# detach automatically on hangup
autodetach on
# change window with ALT-. ALT-,
bindkey "^[," prev
bindkey "^[." next
# Default screens
### screen -t shell1 0
### screen -t shell2 1
# remember enough to make scrolling back worthwhile:
defscrollback 50000
# easily open screen windows 10 through 19 with Shift-
bind ! select 11
bind @ select 12
bind \# select 13
bind $ select 14
bind % select 15
bind \^ select 16
bind & select 17
bind * select 18
bind ( select 19
bind ) select 10
# disable freezing:
bind s
# makes splitting screen less risky!
# stop using A for titles and use T!
bind A
bind T title
# prevent accidentally detaching:
bind d
# (you still have ^D)
#################################################
echo " "
clear
echo "Command character is backtick: `"
# echo "Use Option , and Option . (< and > keys) to cycle through windows."
# echo "Type 'example screen' for common & useful examples"
sleep 1
activity 'Action in window %n'
# set the bell message. %n is the window number.
bell_msg "ding ding ding! (%n)"
ENDSCREEN
###########################################################
cat << 'ENDVIMRC' >> .vimrc
" necessary for some vim features:
set nocompatible
" might cause pasting problems:
set autoindent
set smartindent
" line-numbering:
set number
" syntax coloring:
syntax on
" show rule
set ruler
" ???:
" map! c>`^
" a better escape from insertion mode
map! ;; <Esc>`^
" redo:
nmap U <C-R> " ^R
" show status line always
set laststatus=2
" fix jumping-arrow (ABCD) problem (on e.g. Solaris):
set term=iris-ansi "xterm "linux
if version >= 700
au InsertEnter * hi StatusLine ctermbg=3 ctermfg=4
" au InsertEnter * set nocuc cul | hi cursorline ctermbg=blue ctermfg=white
au InsertLeave * hi StatusLine term=reverse ctermfg=2 ctermbg=0
" au InsertLeave * set nocuc cul | hi cursorline ctermbg=black ctermfg=none
endif
" use 2 spaces instead of tabs:
set cindent
set shiftwidth=2
set tabstop=2
set expandtab
set softtabstop=2
" TODO: assign a keystroke to toggle this setting:
set nowrap
" for now I want to experiment with how it is to have this setting
" experimental:
augroup Makefile
au!
au BufReadPre Makefile set noexpandtab
augroup END
" auto-detect filetypes
filetype on
" colorscheme elflord
" colorscheme peachpuff
" colorscheme default
" Set automatic filetype detection to on
filetype on
" set the statusline with a bunch of info...
set statusline=%{Version()}\ \ \ %1*[%02n]%*\ %(%M%R%H%)\ %F%*\ %=%{Settings()}\ %3*<%l,%c%V>%*
fu! Version()
return version
endf
fu! Settings()
let settings=""
" autoindent
if &ai| let settings=settings." autoindent" |endif
" expandtab
if &et| let settings=settings." expandtab" |endif
" highlightsearch
if &hls| let settings=settings." highlight" |endif
" paste
if &paste|let settings=settings." paste"|endif
" shiftwidth
if &shiftwidth!=8|let settings=settings." ".&shiftwidth|endif
" textwidth - show always!
let settings=settings." textwidth=".&tw
" let settings=settings."\[".&lines.",".&columns."\]"
return settings
endf
" show command you're typing:
set showcmd
" tab completion:
set wildmenu
set wildmode=list:longest,full
" enable mouse support in console:
set mouse=a
" ii gets you out of insert mode
inoremap ii <Esc>
nnoremap IIII <Nop> " ???
" SEARCH
set incsearch " find terms as you type them
set magic " use * in search/replace patterns
" BACKUPS: get them out of the current directory!
set backupdir=~/.vim/backup
set backup
set directory=~/.vim/tmp
" show matching brackets
set showmatch
set cmdheight=2
ENDVIMRC
echo "Created new .vimrc"
###########################################################
# get directories ready, since vim won't create these:
mkdir -p ~/.vim/bnsickup ~/.vim/tmp
###########################################################
cat << 'ENDSOLARIS' >> .solaris
# load Solaris-specific settings & make up for its defects ;p #
if [[ "`uname`"="SunOS" ]]; then
TERM=xterm
alias whoami='echo "$LOGNAME"'
fi
ENDSOLARIS
echo "Created new .solaris"
###########################################################
cat << 'ENDFOLLOW' >> bin/follow
#!/usr/bin/perl
use Getopt::Long qw(:config pass_through);
my %colors =
(
'red' => '41;37;1m',
'green' => '42;37;1m',
'yellow' => '43;30;1m',
'blue' => '44;37;1m',
'magenta' => '45;37;1m',
'cyan' => '46;30;1m',
'white' => '47;30;1m'
);
my @highlights;
GetOptions ("highlight=s" => \@highlights);
my $tailargs;
for(my $c = 0; $c < scalar(@ARGV); $c++)
{
$tailargs .= ' ' . $ARGV[$c];
}
my $highlightargs;
for(my $c = 0; $c < scalar(@highlights); $c++)
{
my @data = split(/:/, $highlights[$c], 2);
my $color = $colors{$data[0]};
my $search = $data[1];
$highlightargs .= "s/(^.*$search.*\$)/\\033\\[$color\$1\\033[0m/gi; ";
}
my $cmd = 'tail ' . $tailargs . " | ruby -p -e '" . $highlightargs . "'";
#print 'executing: ' . $cmd . "\r\n";
system($cmd);
ENDFOLLOW
echo "Created new bin/follow"
###########################################################
cat << 'ENDPROTECTSESSION' >> bin/protect_session
#!/bin/sh
# kill the process for this session
PID=`ps | grep protect_session | sed 's/^ *\(.*\) *$/\1/' | cut -d' ' -f 1`
# we can't use pgrep above because it returns more than just the current session's PIDs
# but we can use it below:
# TODO: find a cross-platform alternative to pgrep. it's not installed by default on OS X, and maybe not BSDs or Solaris.
# which and whereis give wildly inconsistent return values across these platforms, so they are not much use in detecting pgrep.
# it might be better to just create a long version like for PID.
PIDS=`pgrep protect_session`
if [[ -n $PID ]]; then
echo "Killing process $PID."
kill $PID
fi
# offer to remove all other instances of protect_session
if [[ -n $PIDS ]]; then
echo "If you have other sessions being maintained unnecessarily, you can close them now."
killall -iq protect_session
fi
ENDPROTECTSESSION
echo "Created new bin/protect_session"
###########################################################
cat << 'ENDPROFILE' >> .profile
TERM=xterm
export PATH=$PATH:/home/dbutler/bin:/www/TII/turbine.lib/native/bin/
SCREEN=$(which screen 2> /dev/null)
if [[ -n $SCREEN ]]; then
# we've got it installed in the path...
screen -s bash -R -A
else
echo "Searching for screen in the background..."
sleep 1
find / -name screen 2> /dev/null &
bash # we just switch to bash without screen
fi
ENDPROFILE
echo "Created new .profile"
###########################################################
cat << 'ENDACKRC' >> .ackrc
--group
--color-filename=BLUE
--color-match=RED
--follow
--all-types
--nobinary
--context=7
# this doesn't work. blech!
# --pager="'less -r'"
# use when only listing filenames:
--print0
ENDACKRC
echo "Created new .ackrc"
###########################################################
######### bin ###########
mkdir -p ~/bin
# install ack
# TODO: and code to accomodate machines without wget either (perl?)
# both curl and wget have strange ideas of what constitutes STDERR:
if [[ -n $(curl -L http://www.apple.com 2> /dev/null) ]]; then
curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 ~/bin/ack
elif [[ -n $(wget -S http://www.apple.com 2> /dev/null) ]]; then
wget http://betterthangrep.com/ack-standalone 1> /dev/null && mv ack-standalone ~/bin/ack && chmod 0755 ~/bin/ack
echo "Created new ~/bin/ack"
else
echo "You can install ack by downloading "
echo " http://betterthangrep.com/ack-standalone"
echo "and entering:"
echo " mv ack-standalone ~/bin/ack"
echo " chmod 0755 ~/bin/ack"
fi
###########################################################
cat << 'ENDREBUILD' > ~/bin/rebuild
#!/bin/bash
wget http://gist.github.com/raw/259348/build_home.sh
chmod u+x build_home.sh
./build_home.sh
rm ./build_home.sh
ENDREBUILD
echo "Created new ~/bin/rebuild"
###########################################################
chmod u+x bin/rebuild
###########################################################
# clean up
# bash 3.2.25:
for file in .keep_alive .aliases .functions .prompt .variables .local .vimrc .solaris .profile .bashrc .bash_profile .ackrc .screenrc .inputrc; do
# bash 4???:
# for file (.keep_alive .aliases .functions .prompt .variables .local); do
if [[ -z `diff $file $file.$DATE 2> /dev/null` ]]; then
rm -f $file.$DATE
else
echo "$file.$DATE has changes you may want to merge into $file:"
echo " echo; echo $file.$DATE" >> ".diff.$DATE"
echo " diff $file $file.$DATE" | tee -a ".diff.$DATE"
echo " echo; echo '************************'; sleep 1; echo" >> ".diff.$DATE"
DIFFS='true'
fi
done
if [[ $DIFFS = 'true' ]]; then
echo "To compare all modified files, type"
echo " source .diff.$DATE"
fi
# TODO: add a counter, and create aliases in the loop like
# alias diff1='diff .screenrc .screenrc.20100729'
# alias diff2=...
###########################################################
echo "Done building your home. Here are your current dot files:"
sleep 1
ls -d1 .*
# here's a faster way to apply this to lots of servers at once; copy this entire line to each one:
# wget http://gist.github.com/raw/259348/build_home.sh && chmod u+x build_home.sh &&./build_home.sh && source .bash_profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment