Merge pull request #34 from kamihouse/master
[m6w6/gitweb-theme] / setup
1 #!/bin/bash
2
3 ##########################################################
4 # Configuration
5 ##########################################################
6 #
7 # Here you can set the default installation directory
8 #
9 DEFAULT_TARGET="/usr/share/gitweb"
10
11
12
13
14 ##########################################################
15 # Functions
16 ##########################################################
17 #
18 # (no need to modify, unless you want to)
19 #
20 install()
21 {
22 set_target
23 set_cmd_args
24
25 if [[ $(check_for_symlinks) == $TRUE && $(check_for_backups) == $TRUE ]]
26 then
27 log ""
28 log "[ERROR] - Symlinks and backups found in target, nothing to do."
29 log "[ERROR] - (Is the theme already installed?)"
30 exit 1
31 else
32
33 #Backing up
34 if [[ $INTERACTIVE == $TRUE ]]
35 then
36 log ""
37 if [[ $(confirm "Backing up original files, continue?") == $FALSE ]]
38 then
39 echo ""
40 exit 1
41 else
42 echo ""
43 fi
44 fi
45
46 if [[ $(check_for_backups) == $TRUE ]]
47 then
48 log "[NOTICE] - Backups present, skipping..."
49 else
50 log ""
51 log "Backing up..."
52 log ""
53 for FILE in "${THEME_FILES[@]}"
54 do
55 if [[ -e "$TARGET/$FILE.bak" ]]
56 then
57 log "Skipping $FILE.bak, file exists..."
58 else
59 mv $MV_ARGS "$TARGET/$FILE" "$TARGET/$FILE.bak"
60 fi
61 done
62 log ""
63 log "...done"
64 fi
65
66 #Symlinking
67 if [[ $INTERACTIVE == $TRUE ]]
68 then
69 log ""
70 if [[ $(confirm "Linking theme files, continue?") == $FALSE ]]
71 then
72 echo ""
73 exit 1
74 else
75 echo ""
76 fi
77 fi
78
79 if [[ $(check_for_symlinks) == $TRUE ]]
80 then
81 log "[NOTICE] - Symlinks present, skipping..."
82 else
83 log ""
84 log "Linking..."
85 log ""
86 for FILE in "${THEME_FILES[@]}"
87 do
88 if [[ -h "$TARGET/$FILE" ]]
89 then
90 log "Skipping $FILE, symlink exists..."
91 else
92 ln $LN_ARGS "$THEME/$FILE" "$TARGET/$FILE"
93 fi
94 done
95 log ""
96 log "...done"
97 fi
98 fi
99
100 log ""
101 log "[NOTICE] - Installation complete!"
102 }
103
104 remove()
105 {
106 set_target
107 set_cmd_args
108
109 if [[ $(check_for_symlinks "missing") == $TRUE && $(check_for_backups "missing") == $TRUE ]]
110 then
111 log ""
112 log "[ERROR] - No symlinks or backups found in target, Nothing to do."
113 log "[ERROR] - (Is the theme already removed?)"
114 exit 1
115 else
116
117 #Deleting
118 if [[ $INTERACTIVE == $TRUE ]]
119 then
120 log ""
121 if [[ $(confirm "Deleting symlinks, continue?") == $FALSE ]]
122 then
123 echo ""
124 exit 1
125 else
126 echo ""
127 fi
128 fi
129
130 if [[ $(check_for_symlinks) == $TRUE ]]
131 then
132 log ""
133 log "Deleting symlinks..."
134 log ""
135 for FILE in "${THEME_FILES[@]}"
136 do
137 if [[ -h "$TARGET/$FILE" ]]
138 then
139 rm $RM_ARGS "$TARGET/$FILE"
140 else
141 log "Skipping $FILE, not found..."
142 fi
143 done
144 log ""
145 log "...done"
146 else
147 log "[NOTICE] - Symlinks not found, skipping..."
148 fi
149
150 #Restoring
151 if [[ $INTERACTIVE == $TRUE ]]
152 then
153 log ""
154 if [[ $(confirm "Restoring original files, continue?") == $FALSE ]]
155 then
156 echo ""
157 exit 1
158 else
159 echo ""
160 fi
161 fi
162
163 if [[ $(check_for_backups) == $TRUE ]]
164 then
165 log ""
166 log "Restoring..."
167 log ""
168 for FILE in "${THEME_FILES[@]}"
169 do
170 if [[ -e "$TARGET/$FILE.bak" ]]
171 then
172 mv $MV_ARGS "$TARGET/$FILE.bak" "$TARGET/$FILE"
173 else
174 log "Skipping $FILE.bak, not found..."
175 fi
176 done
177 log ""
178 log "...done"
179 else
180 log "[NOTICE] - Backups not found, skipping..."
181 fi
182 fi
183
184 log ""
185 log "[NOTICE] - Removal complete!"
186 }
187
188 set_target()
189 {
190 if [[ $TARGET && $(check_for_static $TARGET) == $TRUE ]]
191 then
192 log "[NOTICE] - Using specified target path: '$TARGET'"
193
194 if [[ $INTERACTIVE == $TRUE ]]; then
195 if [[ $(confirm "Is this correct?") == $FALSE ]]; then
196 echo ""
197 exit 1
198 else
199 echo ""
200 fi
201 fi
202
203 TARGET="$TARGET/static"
204 else
205 if [[ $(check_for_static $DEFAULT_TARGET) == $TRUE ]]
206 then
207 log "[NOTICE] - Target not set, using default path: '$DEFAULT_TARGET'"
208
209 if [[ $INTERACTIVE == $TRUE ]]
210 then
211 log ""
212 if [[ $(confirm "Is this ok?") == $FALSE ]]
213 then
214 echo ""
215 exit 1
216 else
217 echo ""
218 fi
219 fi
220
221 TARGET="$DEFAULT_TARGET/static"
222 else
223 log ""
224 log "[ERROR] - Couldn't find folder 'static/' in the target path: '$TARGET'"
225 log "[ERROR] - (Are you sure this folder contains gitweb?)"
226 exit 1
227 fi
228 fi
229 }
230
231 set_cmd_args()
232 {
233 MV_ARGS=""
234 LN_ARGS="-s"
235 RM_ARGS=""
236
237 if [[ $VERBOSE == $TRUE ]];
238 then
239 MV_ARGS="-v"
240 LN_ARGS="-v -s"
241 RM_ARGS="-v"
242 fi
243
244 if [[ $INTERACTIVE == $TRUE ]];
245 then
246 MV_ARGS="-i"
247 LN_ARGS="-i -s"
248 RM_ARGS="-i"
249 fi
250
251 if [[ $INTERACTIVE == $TRUE && $VERBOSE == $TRUE ]];
252 then
253 MV_ARGS="-i -v"
254 LN_ARGS="-i -v -s"
255 RM_ARGS="-i -v"
256 fi
257 }
258
259 check_for_static()
260 {
261 local RETURN=$FALSE
262
263 if [[ -d "$1/static" ]]
264 then
265 RETURN=$TRUE
266 fi
267
268 echo $RETURN
269 }
270
271 #If all present, true
272 #If passed "missing", and all missing, true
273 check_for_symlinks()
274 {
275 local RETURN=$TRUE
276
277 if [[ $1 == "missing" ]]
278 then
279 local COUNT=0
280
281 for FILE in "${THEME_FILES[@]}"
282 do
283 if [[ ! -h "$TARGET/$FILE" ]]
284 then
285 COUNT=$(expr $COUNT + 1)
286 fi
287 done
288
289 if [[ $COUNT == "${THEME_FILES[@]}" ]]
290 then
291 RETURN=$TRUE
292 else
293 RETURN=$FALSE
294 fi
295 else
296 for FILE in "${THEME_FILES[@]}"
297 do
298 if [[ ! -h "$TARGET/$FILE" ]]
299 then
300 RETURN=$FALSE
301 fi
302 done
303 fi
304
305 echo $RETURN
306 }
307
308 #If all present, true
309 #If passed "missing", and all missing, true
310 check_for_backups()
311 {
312 local RETURN=$TRUE
313
314 if [[ $1 == "missing" ]]
315 then
316 local COUNT=0
317
318 for FILE in "${THEME_FILES[@]}"
319 do
320 if [[ ! -e "$TARGET/$FILE.bak" ]]
321 then
322 COUNT=$(expr $COUNT + 1)
323 fi
324 done
325
326 if [[ $COUNT == "${THEME_FILES[@]}" ]]
327 then
328 RETURN=$TRUE
329 else
330 RETURN=$FALSE
331 fi
332 else
333 for FILE in "${THEME_FILES[@]}"
334 do
335 if [[ ! -e "$TARGET/$FILE.bak" ]]
336 then
337 RETURN=$FALSE
338 fi
339 done
340 fi
341
342 echo $RETURN
343 }
344
345 confirm()
346 {
347 read -n 1 -p "$0: $1 [y] | [n] : " REPLY
348
349 case $REPLY in
350 y|Y)
351 echo $TRUE
352 ;;
353 n|N)
354 echo $FALSE
355 ;;
356 *)
357 echo "$0: [ERROR] - You must answer [y] | [n]"
358 confirm "$1"
359 ;;
360 esac
361 }
362
363 log()
364 {
365 if [[ $VERBOSE == $TRUE ]]
366 then
367 echo "$0: $1"
368 fi
369 }
370
371 usage()
372 {
373 cat << EOF
374 Usage: $0 [-v|-i] [-t <TARGET>|--target <TARGET>] [--install|--remove|--repair]
375 Or: $0 [-V|-h|--version|--help]
376
377 This script will create symlinks to your gitweb install for themeing.
378 The default location is '/usr/share/gitweb' unless set via -t or --target.
379
380 OPTIONS:
381 -v, --verbose Verbose output
382 -i, --interactive Pauses for confirmation at each step
383 -t, --target Where to create the symlinks, gitweb installation path
384 -h, --help Shows this usage message
385 -V, --version Displays version information
386 --install Adds '.bak' to original files and creates symlinks to theme files
387 --remove Deletes themed symlinks and restores the original files.
388 --repair Removes all theme files, then reinstalls
389 EOF
390 }
391
392
393
394 ##########################################################
395 # Let's Go!
396 ##########################################################
397 #
398 PROG_NAME=$0
399 PROG_VERSION="0.9.0"
400 AUTHOR="Kevin Hill <http://github.com/kevinkhill>"
401
402 readonly TRUE=0
403 readonly FALSE=1
404
405 TARGET=
406 THEME=$(pwd)
407 INTERACTIVE=$FALSE
408 VERBOSE=$FALSE
409
410 THEME_FILES=( "gitweb.css" "git-favicon.png" "git-logo.png" )
411
412 SHORT_OPTS="Vhvit:"
413 LONG_OPTS="version,help,verbose,interactive,target:,install,remove,repair"
414 OPTS=$(getopt -o "$SHORT_OPTS" -l "$LONG_OPTS" --name "$0" -- "$@")
415
416 if [ $? != 0 ]; then
417 exit 1
418 fi
419
420 eval set -- "$OPTS"
421 unset OPTS
422
423 if [ $# -eq 0 ]; then
424 echo "$0: [ERROR] - no options specified" >&2
425 usage
426 fi
427
428 while [ $# -gt 0 ]; do
429 case $1 in
430 -h|--help|-\?)
431 usage
432 exit 0
433 ;;
434 -v|--verbose)
435 VERBOSE=$TRUE
436 ;;
437 -i|--interactive)
438 INTERACTIVE=$TRUE
439 ;;
440 -t|--target)
441 TARGET=$2
442 shift
443 ;;
444 -V|--version)
445 echo "Gitweb Theme Installer Script - v$PROG_VERSION"
446 echo " Author: $AUTHOR"
447 ;;
448 --install)
449 install
450 ;;
451 --remove)
452 remove
453 ;;
454 --repair)
455 remove
456 install
457 ;;
458 --)
459 shift
460 break
461 ;;
462 -*)
463 echo "$0: [ERROR] - unrecognized option $1" >&2
464 shift
465 ;;
466 *)
467 break
468 ;;
469 esac
470 shift
471 done
472
473 exit 0