forked from tracyone/oh-my-ubuntu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoh-my-ubuntu.sh
executable file
·330 lines (293 loc) · 7.64 KB
/
oh-my-ubuntu.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
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
#!/bin/bash
# author:tracyone,[email protected]
# ./oh-my-ubuntu.sh [-f <path of ini file>] [-a all|ppa|apt|download|build] [-h]
# Core theory:git can read or write standard ini file easily.
# For example:
# read :git config section.key
# write :git config section.key value
stty -echo
shopt -s expand_aliases #enable alias in bash shell
read -p "Please input $(whoami)'s passwd: " mypasswd
alias sudo="echo "${mypasswd}" | sudo -S"
stty echo
# variable default setting {{{
LOG_FILE="omu.log"
PROMPT=1 # prompt before every install
ROOT_DIR=$(pwd) #do not change
ACTION="all"
SRC_DIR=${HOME}/Work/Source
GIT_CONFIG="./config/ubuntu_16.04.ini"
# }}}
# function definition {{{
# $1:software list to install..
function AptSingleInstall()
{
local oldifs=${IFS}
IFS=" "
for i in $1
do
sudo apt-get install $i --allow-unauthenticated -y || echo -e "apt-get install failed : $i\n" >> ${LOG_FILE}
done
IFS=${oldifs}
}
function AptInstall()
{
local oldifs=${IFS}
IFS=" "
ans=""
if [[ $1 =~ ^[\ ]*$ ]]; then
return 3
fi
if [[ PROMPT -eq 1 ]]; then
read -n1 -p "Install $1 ?(y/n)" ans
fi
if [[ $ans =~ [Yy] || PROMPT -eq 0 ]]; then
sudo apt-get install $1 --allow-unauthenticated -y || AptSingleInstall "$1"
sleep 1
else
echo -e "\n\nAbort install\n"
fi
IFS=${oldifs}
}
# $1:section.key
function AptAddRepo()
{
ans=""
if [[ PROMPT -eq 1 ]]; then
read -n1 -p "Adding ppa $i? (y/n) " ans
fi
if [[ $ans =~ [Yy] || PROMPT -eq 0 ]]; then
sudo add-apt-repository -y $1 || echo -e "apt-add-repository failed : $1\n" >> ${LOG_FILE}
sleep 1
else
echo -e "\n\nAbort install\n"
fi
}
function DebInstall()
{
local filename="tmp$(date +%Y%m%d%H%M%S).deb"
ans=""
if [[ PROMPT -eq 1 ]]; then
read -n1 -p "Wget $i? (y/n) " ans
fi
if [[ $ans =~ [Yy] || PROMPT -eq 0 ]]; then
wget -c $1 -O ${filename} || echo -e "Wget $1 failed\n" >> ${LOG_FILE}
sudo dpkg -i ${filename} || ( sudo apt-get -f install --fix-missing -y; sudo dpkg -i ${filename} \
|| echo -e "dpkg install ${filename} form $1 failed\n" >> ${LOG_FILE} )
else
echo -e "\n\nAbort install\n"
fi
}
function BuildSrc()
{
IFS=","
local -i count=0
local proj_str=""
if [[ PROMPT -eq 1 ]]; then
read -n1 -p "Build source $i? (y/n) " ans
fi
if [[ $ans =~ [Yy] || PROMPT -eq 0 ]]; then
for i in $1; do
case ${count} in
0 )
IFS=${OLD_IFS}
proj_dir=${SRC_DIR}/$(basename $i .git)
if [[ ! -d ${proj_dir} ]]; then
git clone $i ${proj_dir}/ || echo -e "git clone $i failed\n" >> ${LOG_FILE}
else
echo -e "\n\nUpdating $(basename $i .git)'s source code ...\n"
cd ${proj_dir}
git checkout -- .
git pull || echo -e "Update source $(basename $i .git) failed\n" >> ${LOG_FILE}
fi
IFS=","
;;
1 )
AptInstall $i
;;
2 )
child_shell_execute "cd ${proj_dir} && $i"
;;
*)
echo -e "Wrong ini format in build section\n" >> ${LOG_FILE}
;;
esac
let "count+=1"
done
else
echo -e "\n\nAbort install\n"
fi
cd ${ROOT_DIR}
IFS=$'\x0A'
}
function PrintInfo()
{
cat << "EOF"
==============================================================
___ __ __ _ _
/ _ \| \/ | | | |
| | | | |\/| | | | |
| |_| | | | | |_| |
\___/|_| |_|\___/
Author :tracyone at live dot cn
Project :https://github.com/tracyone/oh-my-ubuntu.git
==============================================================
EOF
}
function OmuShowHelp()
{
PrintInfo
echo -e "\n$1"
echo -e "\nUsage:`basename $0` [-f <path of ini file>] [-a all|ppa|apt|download|build] [-h]\n"
}
function ProcessOptionA()
{
ACTION=$1
case $1 in
all )
;;
ppa )
;;
apt )
;;
download )
;;
build )
;;
* )
OmuShowHelp "Unsupport action $1"
exit 3;
;;
esac
}
function configure()
{
local package_lack=""
for i in $1
do
which $i > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo -e "\nChecking for $i ..... no\n"
package_lack="$i ${package_lack}"
else
echo -e "\nChecking for $i ..... yes\n"
fi
done
if [[ ${package_lack} != "" ]]; then
echo -e "Before install ${package_lack},we update apt Source first ..... \n"
sudo apt-get update
echo -e "apt-get install ${package_lack} ..... \n"
sudo apt-get install -y ${package_lack} || return 3
fi
return 0
}
function child_shell_execute()
{
local tmp="echo ${mypasswd} |sudo -S "
bash -c "$(echo $1 | sed "s/\<sudo\>/${tmp}/g")"
}
# }}}
# Script start {{{
# arg parse and env check {{{
while getopts "f:a:h" arg #选项后面的冒号表示该选项需要参数
do
case $arg in
f )
GIT_CONFIG=${OPTARG}
;;
a )
ProcessOptionA ${OPTARG}
;;
h )
OmuShowHelp
exit 0
;;
* )
OmuShowHelp
exit 1
;;
esac
done
shift $(($OPTIND-1))
if [[ ! -z $1 ]]; then
OmuShowHelp "Unknown option:$1"
exit 3
fi
if [[ ! -f ${GIT_CONFIG} ]]; then
OmuShowHelp "Config file ${OPTARG} is not exist\n"
exit 3
fi
echo -e "\nUse Config file: ${GIT_CONFIG}\n"
echo -e "Action is : ${ACTION}\n"
export GIT_CONFIG
configure "git wget rm mkdir" || exit 3
# }}}
rm -f ${LOG_FILE}
mkdir -p ${SRC_DIR} || exit 3
# set the separator to \n
OLD_IFS="$IFS"
IFS=$'\x0A'
read -n1 -p "Install all software Without prompting?(y/n)" ans
if [[ ${ans} =~ [yY] ]]; then
PROMPT=0
else
PROMPT=1
fi
if [[ ${ACTION} == "all" || ${ACTION} == "ppa" ]]; then
ppa_list=$(git config --get-all repo.ppa)
echo -e "\n\nadding ppa ...\n"
for i in ${ppa_list}; do
if [[ $i != "" ]]; then
AptAddRepo $i
fi
done
fi
if [[ PROMPT -eq 1 ]]; then
echo -e "\n\n"
read -n1 -p "Update Source (y/n) " ans
fi
ARCH=$(git config --get-all info.arch)
if [[ $ans =~ [Yy] || PROMPT -eq 0 ]]; then
if [[ ${ARCH} == "x64" ]]; then
echo -e "\n\nAdding i386 packages support ...\n"
sudo dpkg --add-architecture i386
fi
echo -e "\n\nUpdate source ...\n"
sudo apt-get update
echo -e "\n\nUpgrade ...\n"
sudo apt-get upgrade -y
fi
if [[ ${ACTION} == "all" || ${ACTION} == "apt" ]]; then
echo -e "\n\nApt install ...\n"
apt_list=$(git config --get-all apt.packages)
for i in ${apt_list}; do
if [[ $i != "" ]]; then
AptInstall $i
fi
done
fi
if [[ ${ACTION} == "all" || ${ACTION} == "download" ]]; then
echo -e "\n\nDeb install ...\n"
deb_list=$(git config --get-all download.url)
for i in ${deb_list}; do
if [[ $i != "" ]]; then
DebInstall $i
fi
done
fi
if [[ ${ACTION} == "all" || ${ACTION} == "build" ]]; then
src_list=$(git config --get-all build.gitsrc)
echo -e "\n\nInstall software from source ...\n"
for i in ${src_list}; do
if [[ $i != "" ]]; then
BuildSrc $i
fi
done
fi
echo -e "\nAll done!!Clean ...\n"
sudo apt-get autoremove -y
sudo apt-get autoclean
sudo apt-get clean
PrintInfo
# }}}
# vim: set ft=sh fdm=marker foldlevel=0 foldmarker&: