-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAssemble.sh
executable file
·167 lines (134 loc) · 4.36 KB
/
Assemble.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
#!/bin/zsh
# shellcheck shell=bash
script_dir=$(dirname "${0:A}")
projectfolder=$(dirname "$script_dir")
help () {
echo
echo "The following options are available:"
echo
echo " -j --json Builds Jamf Pro Custom Schema.json file"
echo " -s --separate Builds separate CIS Benchmark Script from the fragements"
echo " -h --help Displays this message or details on a specific verb"
echo
echo "EXAMPLES"
echo " ./Assemble.sh"
echo " Builds CIS Benchmark Script from the fragements"
echo
echo " ./Assemble.sh -j"
echo " Builds Jamf Pro Custom Schema.json file"
echo
echo " ./Assemble.sh -s"
echo " Builds separate CIS Benchmark Script from the fragements"
echo
exit
}
buildScript () {
# destination
endPath="${projectfolder}/Build"
mkdir -p "${endPath}"
endResult="${endPath}/CISBenchmarkScript.sh"
# add shebang
echo "#!/bin/zsh" > "${endResult}"
# add shellcheck bypass for zsh
echo "# shellcheck shell=bash" >> "${endResult}"
echo >> "${endResult}"
# add version and date
version=$(cat "${projectfolder}/Fragments/Version.txt")
versiondate=$(date +%F)
echo "VERSION=\"$version\"" >> "${endResult}"
echo "VERSIONDATE=\"$versiondate\"" >> "${endResult}"
echo >> "${endResult}"
# add header
tail -n +4 "${projectfolder}/Fragments/Header.sh" >> "${endResult}"
# sort the filenames numerically
setopt NUMERIC_GLOB_SORT
# loop over fragments
for filePath in "${projectfolder}/Fragments/OrgScores/"OrgScore*.sh; do
# fragment name
fileName=$(basename "${filePath}")
echo "Add ${fileName} to script"
# add script
tail -n +8 "${filePath}" >> "${endResult}"
echo >> "${endResult}"
done
# add footer
tail -n +4 "${projectfolder}/Fragments/Footer.sh" >> "${endResult}"
# make script executable
chmod +x "${endResult}"
}
# build seperate Scripts
buildSeperateScript () {
# loop over fragments
for filePath in "${projectfolder}/Fragments/OrgScores/"OrgScore*.sh; do
# fragment name
fileName=$(basename "${filePath}")
# destination
endPath="${projectfolder}/Build/Scripts"
mkdir -p "${endPath}"
endResult="${endPath}/${fileName}"
# add shebang
echo "#!/bin/zsh" > "${endResult}"
echo >> "${endResult}"
# add version and date
version=$(cat "${projectfolder}/Fragments/Version.txt")
versiondate=$(date +%F)
echo "VERSION=\"$version\"" >> "${endResult}"
echo "VERSIONDATE=\"$versiondate\"" >> "${endResult}"
echo >> "${endResult}"
# add header
tail --lines=+4 "${projectfolder}/Fragments/Header.sh" >> "${endResult}"
# add script
tail -n +8 "${filePath}" >> "${endResult}"
echo >> "${endResult}"
# add footer
tail -n +4 "${projectfolder}/Fragments/Footer.sh" >> "${endResult}"
# make script executable
chmod +x "${endResult}"
echo "${fileName} created"
done
}
# build Jamf Pro Custom Schema.json file
createJamfJSON () {
# destination
endResultJSON="${projectfolder}/Build/Jamf Pro Custom Schema.json"
# add header
cat "${projectfolder}/Fragments/Header.json" > "${endResultJSON}"
# loop over fragments
for filePath in "${projectfolder}/Fragments/OrgScores/"OrgScore*.sh; do
# fragment name
# fileName=$(basename ${filePath})
# variables
orgScore=$(awk -F '"' '/^orgScore=/ {print $2}' "${filePath}")
audit=$(awk -F '"' '/^audit=/ {print $2}' "${filePath}")
# add orgScores
cat >> "${endResultJSON}" << EOF
"${orgScore}": {
"type": "boolean",
"title": "${audit}",
"description": "This boolean is true or false.",
"default": "false"
},
EOF
done
# remove the last line to close the list
sed -i '' -e '$ d' "${endResultJSON}"
# add closer
echo " }" >> "${endResultJSON}"
echo " }" >> "${endResultJSON}"
echo "}" >> "${endResultJSON}"
echo "Jamf Pro Custom Schema.json created"
}
case $1 in
-s | --separate)
buildSeperateScript
;;
-h | --help)
help
;;
-j | --json)
createJamfJSON
;;
*)
buildScript
;;
esac