-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.sh
More file actions
194 lines (156 loc) · 3.78 KB
/
script.sh
File metadata and controls
194 lines (156 loc) · 3.78 KB
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
#!/bin/bash
DEFAULTPATH='etc/scriptbuilder/default'
STRUCTUREFILE='etc/scriptbuilder/default/structure'
#Causes the script to exit if a variable doesn't exist.
set -o nounset
#Allocating flags for debug and verbose modes.
vValue=0
xValue=0
#Checks if the script is currently in verbose mode, then the script will print every action performed.
vFlag() {
if [ "${vValue}" -eq 1 ]
then
echo "${1}"
fi
}
#Checks if the script is currently in debug mode, then the script will display which changes would be made without actually modifying or creating files.
xFlag() {
if [ "${xValue}" -eq 1 ]
then
return 1
else
return 0
fi
}
#Task created in order to validate whether a file/folder exists or not.
checkExistence() {
vFlag "Validating if the folder exists already..."
if [ -e "${1}" ]
then
return 1
else
return 0
fi
}
#Task created in order to validate whether the directory is owned by the current user or not.
checkFileOwnership() {
vFlag "Validating if the user owns the current directory and can write into it..."
if [ -O "${1}" ]
then
return 1
else
return 0
fi
}
#Validates if the project folder exists and the current user is allowed to write in the current directory
validateBeforeStarting() {
checkExistence "${1}/${2}"
isExisting="$?"
checkFileOwnership "${1}"
isOwnedByUser="$?"
if [ "${isExisting}" -eq 1 ]
then
echo "Process can't be run because the given directory does exist."
exit
fi
if [ "${isOwnedByUser}" -eq 0 ]
then
echo "Process can't be run because you don't have permission to take any actions with the file."
exit
fi
}
#Default creation of projects task
defaultCreation() {
xFlag
isDebugged="$?"
file="${STRUCTUREFILE}"
if [ "$isDebugged" -eq 1 ]
then
echo "Project ${projectName} would be created."
while read line
do
echo "${line} file would be created."
done < $file
else
vFlag "Creating project directory called ${projectName}."
mkdir "/home/anuar/${projectName}"
while read line
do
vFlag "Creating ${line} directory."
mkdir "${projectDirectory}/${line}"
done < $file
vFlag "Project structure of ${projectName} created succesfully."
fi
}
#Sets new file of project structure
sFlag() {
vFlag "Setting new structure according to the given file in arguments..."
xFlag
isDebugged="$?"
if [ "$isDebugged" -eq 1 ]
then
echo "${1} would be the new structure file."
else
cp "$1" "${DEFAULTPATH}"
fi
}
#Sets new content folder structure
dFlag() {
vFlag "Setting new contents directory according to the given directory in arguments..."
xFlag
isDebugged="$?"
if [ "$isDebugged" -eq 1 ]
then
echo "Contents folder would be replaced."
else
sudo rm -rf "${DEFAULTPATH}/contents/"
sudo cp -a "$1/" "${DEFAULTPATH}/"
fi
}
#Compares a given directory with the project structure in order to look for missing files
cFlag() {
file='${STRUCTUREFILE}'
while read line
do
vFlag "Checking if ${line} is missing..."
if [ ! -e "${1}/${line}" ]
then
echo "--- ${line} directory is missing in ${1} ---"
fi
done < $file
exit
}
#Prints the content project structure into a given file
pFlag() {
xFlag
isDebugged="$?"
cd "${DEFAULTPATH}/contents"
if [ "$isDebugged" -eq 1 ]
then
echo "The following structure would be printed into a structure file"
ls
else
ls > "/home/anuar/$1"
fi
exit
}
#Receives arguments provided by user in command prompt
while getopts 'xcdpsv' flag
do
case "${flag}" in
c) cFlag "${2}";;
d) dFlag "${2}";;
p) pFlag "${2}";;
s) sFlag "${2}";;
v) vValue=1
echo "Verbose mode active" ;;
x) xValue=1
echo "Debugged mode active" ;;
*) echo "Please, enter a valid flag."
esac
done
echo "Enter the name of the project: "
read projectName
projectDirectory="/home/anuar/${projectName}"
validateBeforeStarting "/home/anuar" "${projectName}"
defaultCreation