-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcs.sh
516 lines (422 loc) · 14.8 KB
/
vcs.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/bin/bash
#### ABOUT: VCS application that allows for multiple users to work on a project
#### NAME: Daniel J McGuinness
#### DATE: 15/10/16
while [ "$choice" != 0 ]
do
clear
cat << MENU
-------------------------------------
Main Menu
-------------------------------------
1. Start New Project
2. Select Project
0. Quit
-------------------------------------
MENU
read choice
case "$choice" in
1)
#prompt user to choose name for folder
echo -n "Choose a name for your new project: "
read projectname
echo "$STR"
#test to see if directory already exists
if [ ! -d projects/"$projectname" ];
then
#create group for project
sudo groupadd "$projectname"
#assign current user to group
sudo usermod -a -G "$projectname" $USER
#create project directory + subdirectories
mkdir -p projects/"$projectname"/archive
mkdir projects/"$projectname"/log
touch ./projects/"$projectname"/log/log
#assign to group
sudo chown -R $USER:$projectname projects/"$projectname"
#change access permissions to user only
chmod 770 projects/"$projectname"
#inform user that project has been created
echo "project successfully created"
else
#inform user that project already exists
echo "project could not be created: project with this name already exists"
fi
echo "press any key to continue"
read
;;
2)
clear
echo "avaliable projects: "
echo "$STR"
ls --format=single-column ./projects
echo "$STR"
echo -n "enter the project you would like to view: "
read projectname
echo "$STR"
#test to see if project exists
if [ -d ./projects/"$projectname" ] && [ ! "$projectname" == "" ];
then
#display project menu
projectchoice=1
while [ "$projectchoice" != 0 ]
do
clear
echo "-------------------------------------"
echo "$projectname Menu"
echo "-------------------------------------"
echo "1. Create File"
echo "2. Remove File"
echo "3. Edit File"
echo "4. View Change Log"
echo "5. Finish & Zip Project"
echo "6. Change Access Permissions"
echo "7. Delete Project"
echo "8. Archive Project"
echo "9. Restore from Archive"
echo "0. Return to Main Menu"
echo "-------------------------------------"
echo "file list: "
echo "-------------------------------------"
ls --format=single-column ./projects/$projectname
echo "-------------------------------------"
read projectchoice
case "$projectchoice" in
1) #ADD FILE
echo "Enter the name of the new file: "
read filename
if [ ! -f ./projects/"$projectname"/"$filename" ];
then
#create file in given project
touch ./projects/"$projectname"/"$filename"
#set permission to current user only
chmod 700 ./projects/"$projectname"/"$filename"
#ask users for input
echo "enter text for $filename: (Ctrl + D to end input)"
cat > ./projects/"$projectname"/"$filename"
#allow group users to access file
chmod 770 ./projects/"$projectname"/"$filename"
#add creation info to log
d=$(date -u)
echo "File Created: $filename - Date: $d - User: $USER" >> ./projects/"$projectname"/log/log
#ask users to enter a comment for the log
echo "do you wish to add a comment to the log?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "do you wish to add a comment to the log?(y/n)"
read userchoice
echo "$userchoice"
done
if [ "$userchoice" == "y" ];
then
clear
echo "Enter commment below:(Ctrl + D to end input)"
echo "$USER comment:">> ./projects/"$projectname"/log/log
cat >> ./projects/"$projectname"/log/log
fi
echo "" >> ./projects/"$projectname"/log/log
else
echo "the file $filename already exists"
fi
#ensure text can be read by user
echo "press any key to continue"
read
;;
2) #DELETE FILE
echo "Avaliable files:"
ls --format=single-column ./projects/"$projectname"
echo "Which file would you like to delete: "
read filename
#confirm user wishes to delete file
if [ -f ./projects/"$projectname"/"$filename" ];
then
echo "Are you sure you wish to delete $filename ?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "Are you sure you wish to delete $filename ?(y/n)"
read userchoice
done
#process user's choice
if [ "$userchoice" == "y" ];
then
rm -f ./projects/"$projectname"/"$filename"
d=$(date -u)
echo "File Deleted: $filename - Date: $d - User: $USER" >> ./projects/"$projectname"/log/log
echo "$STR"
echo "do you wish to add a comment?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "do you wish to add a comment?(y/n)"
read userchoice
echo "$userchoice"
done
if [ "$userchoice" == "y" ];
then
clear
echo "Enter commment below:(Ctrl + D to end input)"
echo "$USER comment:">> ./projects/"$projectname"/log/log
cat >> ./projects/"$projectname"/log/log
fi
echo "" >> ./projects/"$projectname"/log/log
else
echo "file was not deleted"
fi
else
echo "This file doesn't exist"
fi
#ensure text can be read by user
echo "press any key to continue"
read
;;
3) #EDIT FILE
echo "Avaliable files:"
ls --format=single-column ./projects/"$projectname"
echo "Which file would you like to edit: "
read filename
if [ -f ./projects/"$projectname"/"$filename" ];
then
clear
echo "Current contents of $filename"
echo "$STR"
cat ./projects/"$projectname"/"$filename"
echo "$STR"
echo "Are you sure you wish to edit $filename ?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "Are you sure you wish to edit $filename ?(y/n)"
read userchoice
done
#process user's choice
if [ "$userchoice" == "y" ];
then
#set access to only current user while editing
chmod 700 ./projects/"$projectname"/"$filename"
echo "Editing: (Ctrl + D to end input)"
#start edit
cat > ./projects/"$projectname"/"$filename"
#return access to group
chmod 770 ./projects/"$projectname"/"$filename"
#add edit info to log
d=$(date -u)
echo "File Edited: $filename - Date: $d - User: $USER" >> ./projects/"$projectname"/log/log
#ask users to enter a comment for the log
echo "do you wish to add a comment to the log?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "do you wish to add a comment to the log?(y/n)"
read userchoice
echo "$userchoice"
done
if [ "$userchoice" == "y" ];
then
clear
echo "Enter commment below:(Ctrl + D to end input)"
echo "$USER comment:">> ./projects/"$projectname"/log/log
cat >> ./projects/"$projectname"/log/log
fi
echo "" >> ./projects/"$projectname"/log/log
else
echo "file was not changed"
fi
else
echo "This file doesn't exist"
fi
echo "$STR"
echo "press any key to continue"
read
;;
4) #VIEW CHANGE LOG
clear
cat ./projects/"$projectname"/log/log
echo "$STR"
echo "press any key to continue"
read
;;
5) #FINISH & ZIP PROJECT
#assign output temp name
cp -r ./projects/"$projectname" ./temp
#reduce file size to essentials
rm -rf ./temp/archive
rm -rf ./temp/log
#zip project
tar cvzf "$projectname"Final.tar.gz temp
#remove copy of project
rm -rf ./temp
#move final project to 'finished' folder
mkdir ./finished
mv ./"$projectname"Final.tar.gz ./finished/"$projectname"Final.tar.gz
echo "archive successful"
echo "press any key to continue"
read
;;
6) #CHANGE ACCESS PERMISSIONS
while [ "$accesschoice" != 0 ]
do
clear
echo "-------------------------------------"
echo "$projectname Access Permissions"
echo "-------------------------------------"
echo "1. Add User to Group"
echo "2. Remove User from Group"
echo "0. Return to project overview"
echo "-------------------------------------"
read accesschoice
case "$accesschoice" in
1) #ADD USER TO GROUP
clear
echo "All Local System Users:"
ls --format=single-column /home
echo "$STR"
echo "Which user would you like to add?"
read adduser
echo "$STR"
echo "Are you sure you wish to add this user?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "Are you sure you wish to add $adduser ?(y/n)"
read userchoice
echo "$userchoice"
done
if [ "$userchoice" == "y" ];
then
#add user to group
sudo usermod -a -G "$projectname" $adduser
#inform that user has been added to project
echo "$adduser added to $projectname"
else
echo "user not added"
fi
echo "press any key to continue"
read
;;
2) #REMOVE USER FROM GROUP
clear
echo "All Local System Users:"
ls --format=single-column /home
echo "$STR"
echo "Which user would you like to delete?"
read removeuser
if [ $removeuser != $USER ];
then
echo "$STR"
echo "Are you sure you wish to add this user?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "Are you sure you wish to add $adduser ?(y/n)"
read userchoice
echo "$userchoice"
done
if [ "$userchoice" == "y" ];
then
#inform that user has been removed
echo "$removeuser added to $projectname"
#remove user
gpasswd -d "$removeuser" "$projectname"
else
echo "user not be removed"
fi
else
echo "Cannot remove yourself from group"
fi
echo "$STR"
echo "press any key to continue"
read
;;
esac
done
;;
7) #DELETE PROJECT
echo "Are you sure you wish to delete this project?(y/n)"
read userchoice
#loop until user gives valid response
while [ "$userchoice" != "y" ] && [ "$userchoice" != "n" ]
do
echo "Are you sure you wish to delete $filename ?(y/n)"
read userchoice
echo "$userchoice"
done
if [ "$userchoice" == "y" ];
then
#delete the project directory along with all
#the files and sub directories held within it.
rm -rf ./projects/"$projectname"
#delete group associated with project
sudo groupdel "$projectname"
#ensure user is returned to main menu
projectchoice=0
#inform user that project has been deleted
echo "project deleted"
else
echo "project not deleted"
fi
echo "$STR"
echo "press any key to continue"
read
;;
8) #ARCHIVE PROJECT
#assign output temp name
cp -r ./projects/"$projectname" ./"$projectname"
#remove archive to prevent too many folders
rm -rf ./"$projectname"/archive
#zip project
tar cvzf "$projectname".tar.gz "$projectname"
d=$(date +%Y%m%d%H%M)
mv ./"$projectname".tar.gz ./projects/"$projectname"/archive/"$d".tar.gz
#remove temp
rm -rf ./"$projectname"
#inform user
echo "project archived!"
echo "$STR"
echo "press any key to continue"
read
;;
9) #RESTORE FROM ARCHIVE
echo "Avaliable back-ups"
ls --format=single-column ./projects/"$projectname"/archive
echo "$STR"
echo "Enter which backup you would like to restore: "
read restorename
if [ -f ./projects/"$projectname"/archive/"$restorename" ] && [ ! "$restorename" == "" ];
then
#copy files to be moved to restore
cp ./projects/"$projectname"/archive/"$restorename" ./"$restorename"
cp -rf ./projects/"$projectname"/archive ./archivetemp
#extract file
tar -xvzf "$restorename"
#tidy up main folder
mv ./archivetemp ./"$projectname"/archive
rm -rf ./"$restorename"
#replace project
rm -rf ./projects/"$projectname"
mv ./"$projectname" ./projects/$projectname
else
echo "input is invalid"
fi
echo "press any key to continue"
read
;;
esac
done
else
echo "project not found, returning to main menu"
echo "$STR"
echo "press any key to continue"
read
fi
;;
esac
done