-
Notifications
You must be signed in to change notification settings - Fork 30
/
utils.sh
executable file
·238 lines (195 loc) · 6.42 KB
/
utils.sh
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
#!/bin/bash
###########################################################
# utilitary functions
###########################################################
function log {
if [ ! -n "$1" ]; then
log "log() requires text string as its argument"
return 1;
fi
echo "`date '+%D %T'` - $1" 1>&2
}
function ask {
if [ ! -n "$1" ]; then
log "ask() requires a question to ask"
return 1;
fi
read -p "$1 " ANSWER
echo "$ANSWER"
}
function upgrade_system {
log "Upgrading System..."
apt-get update
apt-get -y install aptitude
aptitude -y full-upgrade
}
install_essentials() {
log "Installing Essentials..."
aptitude -y install build-essential zlib1g-dev libpcre3-dev libssl-dev libcurl4-openssl-dev libreadline5-dev libreadline6-dev libreadline-dev libxml2-dev libxslt1-dev libmysqlclient-dev openssh-server git-core wget vim less bash-completion curl rsync locate imagemagick libyaml-dev libffi-dev
}
enable_root_color_prompt() {
sed -i -e 's/^#PS1=/PS1=/' /root/.bashrc
}
function lower {
echo $1 | tr '[:upper:]' '[:lower:]'
}
function update_locale_en_US_UTF_8 {
log "Updating locale to en_US.UTF-8..."
dpkg-reconfigure locales
update-locale LANG=en_US.UTF-8
}
better_bash_prompt()
{
local USE_DETAILED=$1
for bashfile in /root/.bashrc /etc/skel/.bashrc ; do
cat <<'EOF' >$bashfile
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(1) for more options
#export HISTCONTROL=ignoredups
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
color_test=$(ls --color 3>&1 2>&1 >/dev/null)
eval "`dircolors -b 2>/dev/null`"
if [ -z "$color_test" ] ; then
#no errors, alias to ls --color
eval "`dircolors -b`"
alias ls='ls --color=auto'
alias dir='ls --color=auto --format=vertical'
alias vdir='ls --color=auto --format=long'
else
#--color flag doesn't work, use -G (we're probably on OSX)
alias ls='ls -G'
fi
fi
# some more ls aliases
alias ll='ls -al'
#alias la='ls -A'
#alias l='ls -CF'
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
#fi
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
#if [ -f ~/.bash_aliases ]; then
# . ~/.bash_aliases
#fi
# git is a bitch -- it pipes the output of git diff into less
# unless PAGER variable is set to cat. Shouldn't cat be
# the default??? WHAT. THE. HELL.
PAGER=cat
export PAGER
color_red='\[\033[01;31m\]'
color_orange='\[\033[00;33m\]'
color_green='\[\033[00;32m\]'
color_blue='\[\033[01;34m\]'
color_purple='\[\033[01;35m\]'
color_cyan='\[\033[01;36m\]'
color_white='\[\033[01;37m\]'
color_default='\[\033[00m\]'
root=$(groups | egrep "root")
admin=$(groups | egrep "wheel|admin")
color_user=$color_green
if [ -n "$root" ] ; then
color_user=$color_red
elif [ -n "$admin" ] ; then
color_user=$color_orange
fi
########################################################################
# VCS part mostly shamelessly ripped off from acdha's bash prompt: #
# http://github.com/acdha/unix_tools/blob/master/etc/bash_profile #
########################################################################
# Utility function so we can test for things like .git/.hg without firing
# up a separate process
__git_branch()
{
local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]; then
local r
local b
if [ -d "$g/../.dotest" ]
then
r="|AM/REBASE"
b="$(git symbolic-ref HEAD 2>/dev/null)"
elif [ -f "$g/.dotest-merge/interactive" ]
then
r="|REBASE-i"
b="$(cat $g/.dotest-merge/head-name)"
elif [ -d "$g/.dotest-merge" ]
then
r="|REBASE-m"
b="$(cat $g/.dotest-merge/head-name)"
elif [ -f "$g/MERGE_HEAD" ]
then
r="|MERGING"
b="$(git symbolic-ref HEAD 2>/dev/null)"
else
if [ -f $g/BISECT_LOG ]
then
r="|BISECTING"
fi
if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
then
b="$(cut -c1-7 $g/HEAD)..."
fi
fi
if [ -n "$1" ]; then
printf "$1" "${b##refs/heads/}$r"
else
printf "%s" "${b##refs/heads/}$r"
fi
fi
}
__vcs_prompt_part()
{
name=""
local git_branch=$(__git_branch)
local hg_branch=$(hg branch 2>/dev/null)
if [ -d .svn ] ; then
name="svn" ;
elif [ -d RCS ] ; then
echo "RCS" ;
elif [ -n "$git_branch" ] ; then
name="git, $git_branch" ;
elif [ -n "$hg_branch" ] ; then
name="hg, $hg_branch"
else
name=""
fi
if [ -n "$name" ] ; then
echo -e '-(\033[01;35m'$name'\033[00m)' #purple
else
echo ""
fi
}
detailed='${debian_chroot:+($debian_chroot)}'$color_default'\n('$color_user'\u@\h'$color_default')-('$color_cyan'\d \@'$color_default')$(__vcs_prompt_part)\n'$color_default'('$color_blue'\w'$color_default')\$ '
short='${debian_chroot:+($debian_chroot)}'$color_user'\u@\h'$color_default':'$color_blue'\w'$color_default'$ '
PS1=$short
EOF
if [ "$USE_DETAILED" = "1" ] ; then
sed -i -e 's/^PS1=\$short[\t ]*$/PS1=$detailed/' $bashfile
fi
done
}
function better_stuff
{
aptitude install -y unzip wget vim less imagemagick sudo whois dnsutils
better_bash_prompt
}