-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLibrary.sh
executable file
·339 lines (295 loc) · 7.85 KB
/
getLibrary.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
331
332
333
334
335
336
337
338
339
#!/bin/bash
# SCRIPT: getLibrary.sh
# AUTHOR: Udo Schochtert
# E-MAIL: [email protected]
#
# PLATFORM: Linux
# Mac
#
# PURPOSE: Script shell to automatically download, extract & symlink a specific version of libraries like ZF, doctrine, etc.
# Supported libraries:
# + Zend Framework
# + Doctrine 2
#
# IMPORTANT!!!
# Make sure that the directory from which the script is executed is writable for the user which is executing the script! Otherwise the download will fail because writing permissions are missing.
#
# Target library path:
# /usr/local/lib/<library>/stable/
#
# USAGE:
# ./getLibrary.sh <library> <version>
# <library>
# zf - Zend Framework
# doc - Doctrine 2
# <version>
# check the website of the library for the available versions
###############################
# Variables #
###############################
### PATHS ###
# default library path
PATHBASE="/usr/local/lib/"
### OTHER ###
# supported libs variable (used in error messages)
LIBSSUPPORTED="Zend Framework (zf), Doctrine2 (doc)"
SCRIPTNAME="getLibrary"
### MESSAGES ###
MESSAGE="$SCRIPTNAME - MESSAGE"
WARNING="$SCRIPTNAME - WARNING"
ERROR="$SCRIPTNAME - ERROR"
###############################
# Functions #
###############################
#
# Purpose: Enable debug mode
#
function fctDebug(){
echo "***** Debug Mode Enabled *****"
echo "Archive: $ARCHIVE"
echo "Source: $SOURCE"
echo $PATHDEST
echo "Working Dir: " & pwd
echo "***** Debug Mode Disabled (Script stopped) *****"
exit 0
}
#
# Purpose: Transform string to lower
#
function fctString2Lower(){
# Check if parameter #1 is zero length -> no parameter passed
if [ -z "$1" ]
then
echo "$ERROR: No parameter passed. Stopping script..."
exit 1
fi
# Check if parameter #2 was passed -> not allowd -> only parameter #1 allowd
if [ "$2" ]
then
echo "$ERROR: Only 1 parameter allowed (Example: fctString2Lower $1). Stopping script..."
exit 1
fi
# convert string to lower case
echo $1 | tr [:upper:] [:lower:]
exit 0
}
#################################
# Main Script Logic Starts Here #
#################################
# detect platform
PLATFORM="other"
OS=$(uname)
# determine OS
case $OS in
Linux)
# library name
PLATFORM="linux"
;;
Darwin)
# library name
PLATFORM="darwin"
;;
*)
# unknown OS
echo "$ERROR: Unknown Operating System. Stopping script..."
exit 1
esac
# check if the user (which launched this script) belongs to an administrator group (e.g. root, admin, etc.)
# notes:
# admin groups are OS dependent
# '>/dev/null' redirects the output so user do not see it
case $OS in
Linux)
if ! groups | grep -co 'root' >/dev/null
then
# user does not belong to group root -> stop script
echo "$ERROR: You do not belong to group 'root'. Please log in as root and relaunch script. Stopping script..."
exit 1
fi
;;
Darwin)
if ! groups | grep -co 'admin' >/dev/null
then
# user does not belong to group admin -> stop script
echo "$ERROR: You do not belong to group 'admin'. Please log in as root and relaunch script. Stopping script..."
exit 1
fi
;;
esac
# check if default library path exists
if [ ! -d $PATHBASE ]
then
# lib path does not exist
echo "$ERROR: Directory $PATHBASE does not exist. Stopping script..."
echo " Please create the directory path manually."
echo " Restart the script."
exit 1
fi
# check if 1st argument was passed
if [ -z $1 ]
then
echo "$ERROR: Please provide the library name (e.g. $LIBSSUPPORTED). Stopping script..."
exit 1
else
LIB=$(fctString2Lower $1)
fi
# verify user requested library
case $LIB in
zend | zf)
# library name
LIB="ZF"
;;
doctrine | doctrine2 | doc | doc2)
# library name
LIB="Doctrine2"
;;
*)
echo "$ERROR: requested library '$LIB' not supported. Only the libraries $LIBSSUPPORTED are supported at the moment. Stopping script..."
exit 1
esac
# check if 2nd argument was passed
if [ -z $2 ]
then
echo "$ERROR: Please provide the version number. Stopping script..."
exit 1
else
# library version (2nd parameter passed)
VERSION=$2
fi
# final path in function of the requested library
# ToDo: write function fctSetPath to set the path. Consider alpha, beta, stable versions.
PATHDEST="$PATHBASE$LIB/stable/"
# check if the path exists
if [ ! -d $PATHDEST ]
then
# path does not exist
echo "$MESSAGE: Directory $PATHDEST does not exist. Creating directory..."
mkdir -pv $PATHDEST
fi
# change directory (to use directories relative to this dir path)
cd $PATHDEST
# debugging: uncomment the following line if you are unsure in which directory the script is...
#fctDebug
# check if version directory already exists
if [ -d $VERSION ]
then
echo "$WARNING: The version directory $VERSION already exists."
#define options as array
declare -a options
#set first empty position with new value
options[${#options[*]}]="remove directory $VERSION";
options[${#options[*]}]="quit";
#expand to quoted elements:
select opt in "${options[@]}"; do
case ${opt} in
${options[0]})
CURRENT_DIR=$(pwd)
#echo $CURRENT_DIR
rm -r $VERSION
echo "$MESSAGE: Directory $CURRENT_DIR/$VERSION (including subdirectories) deleted."
break
;;
(quit)
echo "$MESSAGE: Stopping script."
exit 1
;;
(*)
echo "$WARNING: You entered a non-valid option ${opt}"; ;;
esac;
done
fi
case $LIB in
ZF)
# archive name
ARCHIVENAME="ZendFramework-$VERSION"
# archive extension
ARCHIVEEXT=".tar.gz"
# archive full (name & extension)
ARCHIVE=$ARCHIVENAME$ARCHIVEEXT
# source
SOURCE="http://framework.zend.com/releases/ZendFramework-$VERSION/$ARCHIVE"
;;
Doctrine2)
ARCHIVENAME="DoctrineORM-$VERSION-full"
ARCHIVEEXT=".tar.gz"
ARCHIVE=$ARCHIVENAME$ARCHIVEEXT
SOURCE="http://www.doctrine-project.org/downloads/$ARCHIVE"
;;
esac
# check if archive already exists
if [ -e "$ARCHIVE" ]
then
echo "$WARNING: The archive $ARCHIVE already exists."
#define options as array
declare -a options
#set first empty position with new value
options[${#options[*]}]="remove archive";
options[${#options[*]}]="quit";
#expand to quoted elements:
select opt in "${options[@]}"; do
case ${opt} in
${options[0]})
rm $ARCHIVE
echo "$MESSAGE: Archive $ARCHIVE deleted."
break
;;
(quit)
echo "$MESSAGE: Stopping script."
exit 1
;;
(*)
echo "$WARNING: You entered a non-valid option ${opt}"; ;;
esac;
done
else
echo "$MESSAGE: Download archive from: $SOURCE"
# download archive
wget $SOURCE
# $? is <> 0 if previous command produced an error
if [ "$?" -ne "0" ]
then
echo "$ERROR (wget): The url $SOURCE produced an error. Probably the submitted parameter $2 (library version) is not correct."
exit 1
fi
fi
#untar the archive
tar -xzf $ARCHIVE
# remove archive
rm $ARCHIVE
# rename untared directory
case $LIB in
ZF)
mv $ARCHIVENAME $VERSION
;;
Doctrine2)
mv doctrine-orm $VERSION
# move Symfony folder
mv $VERSION/Doctrine/Symfony $VERSION/
;;
esac
# ask user if he wants to change the symbolic link of the latest stable to this version
cat << EOF
Do you want to set the symbolic link of the lastest stable to this version? [y/n]
EOF
read USERINPUT
# transform user input to lowercase
#USERINPUT='echo $USERINPUT | tr A-Z a-z'
#echo "$USERINPUT"
# symlink name
SYMLINKNAME="latest"
# check user input
case $USERINPUT in
yes | y | YES | Y| Yes)
# check if symbolic link "latest_test" exists
if [ -L $SYMLINKNAME ]
then
# remove symlink
rm $SYMLINKNAME
fi
# set symlink
ln -s $VERSION $SYMLINKNAME
;;
esac
echo "$MESSAGE: Script $0 terminated successfully. :)"
exit 0
# end of script