1 | #!/bin/bash␊ |
2 | ␊ |
3 | main ()␊ |
4 | {␊ |
5 | ␉tempDir="${destinationDrive}/.Chameleon/"␊ |
6 | ␉echo "DEBUG: tempDir is ${tempDir}"␊ |
7 | ␊ |
8 | ␊ |
9 | ␉if [ ! -d "${tempDir}" ]; then␊ |
10 | ␉␉echo "the temporary directory does not exist so no boot options have been selected"␊ |
11 | ␉else␊ |
12 | ␉␉# check to see if there are any boot options in the temporary directory␊ |
13 | ␊ |
14 | ␉␉cd "${tempDir}"␊ |
15 | ␉␉Dirlist=$(ls | grep "@")␊ |
16 | ␊ |
17 | ␉␉if [ "${Dirlist}" ]; then␊ |
18 | ␊ |
19 | ␉␉␉echo "==============================================="␊ |
20 | ␉␉␉echo "set com.apple.Boot.plist variables"␊ |
21 | ␉␉␉echo "**********************************"␊ |
22 | ␊ |
23 | ␉␉␉bootplist="${tempDir}Extra/com.apple.Boot.plist"␊ |
24 | ␉␉␉echo "DEBUG: bootplist = ${bootplist}"␉␊ |
25 | ␊ |
26 | ␉␉␉systemplist="/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"␊ |
27 | ␉␉␉echo "DEBUG: systemplist= ${systemplist}"␊ |
28 | ␊ |
29 | ␉␉␉echo "if no directory exists named ${bootplist%/*} then mkdir ${bootplist%/*}"␊ |
30 | ␉␉␉[ ! -d "${bootplist%/*}" ] && mkdir -p "${bootplist%/*}"␊ |
31 | ␊ |
32 | ␉␉␉if [ ! -f "${bootplist}" ]; then␊ |
33 | ␉␉␉␉echo "${bootplist} doesn't exist"␊ |
34 | ␉␉␉␉if [ -f "${systemplist}" ]; then␊ |
35 | ␉␉␉␉␉echo "${systemplist} doesn't exist"␊ |
36 | ␉␉␉␉␉echo "copy ${systemplist} to ${bootplist}"␊ |
37 | ␉␉␉␉␉cp "${systemplist}" "${bootplist}"␊ |
38 | ␉␉␉␉fi␊ |
39 | ␉␉␉fi␊ |
40 | ␉␉␉echo "-----------------------------------------------"␊ |
41 | ␉␉␉echo ""␊ |
42 | ␊ |
43 | ␊ |
44 | ␉␉␉echo "==============================================="␊ |
45 | ␉␉␉echo "Build Extra/com.apple.Boot.plist"␊ |
46 | ␉␉␉echo "********************************"␊ |
47 | ␊ |
48 | ␉␉␉for bootOptionFile in $Dirlist ; do␊ |
49 | ␉␉␉␉overide=$(echo $bootOptionFile | sed 's/_/ /g' | tr -d "./@" |cut -d '+' -f1)␊ |
50 | ␉␉␉␉string=$(echo $bootOptionFile | tr -d "./@" |cut -d'+' -f2)␊ |
51 | ␊ |
52 | ␉␉␉␉bootoptionshdextra[1]="<key>${overide}</key>"␊ |
53 | ␉␉␉␉bootoptionshdextra[2]="<string>${string}</string>"␊ |
54 | ␉␉␉␉#echo "DEBUG: bootoptionshdextra 1 = ${bootoptionshdextra[1]}"␊ |
55 | ␉␉␉␉#echo "DEBUG: bootoptionshdextra 2= ${bootoptionshdextra[2]}"␊ |
56 | ␊ |
57 | ␉␉␉␉echo "Attempting to add ${overide}=${string} to ${bootplist}"␊ |
58 | ␉ ␉␉␉array=("${bootoptionshdextra[@]}")␊ |
59 | ␉␉␉␉searchfilereplaceline "${bootplist}" "<key>Kernel Flags</key>" "" "" "2"␊ |
60 | ␉␉␉␉echo ""␊ |
61 | ␊ |
62 | ␉␉␉␉#echo "set owner privileges for ${bootplist}"␊ |
63 | ␉␉␉␉chown "${USER}:20" "${bootplist}"␊ |
64 | ␉␉␉done␊ |
65 | ␊ |
66 | ␉␉␉echo "-----------------------------------------------"␊ |
67 | ␉␉␉echo ""␊ |
68 | ␉␉␊ |
69 | ␊ |
70 | ␉␉␉echo "==============================================="␊ |
71 | ␉␉␉echo "Clean up and Remove Boot Option Null Files"␊ |
72 | ␉␉␉echo "******************************************"␊ |
73 | ␊ |
74 | ␉␉␉# make sure we're in the right directory␊ |
75 | ␉␉␉cd "${tempDir}"␊ |
76 | ␊ |
77 | ␉␉␉Dirlist=$(find . -type f | grep "@")␊ |
78 | ␉␉␉for bootOptionFile in ${Dirlist} ; do␊ |
79 | ␉␉␉␉echo "Removing temporary boot option file: ${bootOptionFile}"␊ |
80 | ␉␉␉␉rm $bootOptionFile␊ |
81 | ␉␉␉done␊ |
82 | ␊ |
83 | ␉␉␉echo "-----------------------------------------------"␊ |
84 | ␉␉␉echo ""␊ |
85 | ␊ |
86 | ␉␉else␊ |
87 | ␉␉␉echo "No boot options in the temporary directory."␊ |
88 | ␉␉fi␊ |
89 | ␉fi␊ |
90 | }␊ |
91 | ␊ |
92 | ␊ |
93 | searchfilereplaceline ()␊ |
94 | {␊ |
95 | ␊ |
96 | mv "${1}" "${1}.orig"␊ |
97 | ␊ |
98 | prefunctionifs="${IFS}"␊ |
99 | ␊ |
100 | unset fileinput␊ |
101 | unset fileoutput␊ |
102 | unset find␊ |
103 | unset replace␊ |
104 | unset deletelines␊ |
105 | unset deletelinesskip␊ |
106 | unset insertlinesskip␊ |
107 | ␊ |
108 | fileinput="${1}.orig"␊ |
109 | fileoutput="${1}"␊ |
110 | find="${2}"␊ |
111 | replace="${3}"␊ |
112 | deletelines="${4%,*}"␊ |
113 | insertlinesskip="${5}"␊ |
114 | ␊ |
115 | matchlinefound="0"␊ |
116 | ␊ |
117 | if [ "${#4}" = "${4#*,}" ]; then␊ |
118 | deletelinesskip="0"␊ |
119 | ␉else␊ |
120 | ␉deletelinesskip="${4#*,}"␊ |
121 | fi␊ |
122 | ␊ |
123 | IFS="\n"␊ |
124 | while read line␊ |
125 | do␊ |
126 | ␉{␊ |
127 | ␉if [ ! "${line}" = "${line/${find}/}" ]; then␊ |
128 | ␉␉{␊ |
129 | ␉␉# Trim the longest match from the end for <*␊ |
130 | ␉␉xmlelementindent="${line%%<*}"␊ |
131 | ␊ |
132 | ␉␉# Trim the longest match from the start for *<␊ |
133 | ␉␉xmlelementtemp="${line#*<}"␊ |
134 | ␉␉# Add back in stripped <␊ |
135 | ␉␉xmlelement="<${xmlelementtemp}"␊ |
136 | ␊ |
137 | ␉␉# Trim the shortest match from the start for <␊ |
138 | ␉␉xmltagtemp="${xmlelement#<}"␊ |
139 | ␉␉# Trim the longest match from the end for >*␊ |
140 | ␉␉xmltag="${xmltagtemp%%>*}"␊ |
141 | ␊ |
142 | ␉␉# Trim the shortest match from the start for *>␊ |
143 | ␉␉xmltexttemp="${xmlelement#*>}"␊ |
144 | ␉␉# Trim the longest match from the end for <*␊ |
145 | ␉␉xmltext="${xmltexttemp%%<*}"␊ |
146 | ␊ |
147 | ␉␉if [ "${replace}" ]; then␊ |
148 | ␉␉echo "DEBUG: Replace=${replace}"␊ |
149 | ␉␉␉{␊ |
150 | ␉␉␉␉echo "${xmlelementindent}<${xmltag}>${replace}</${xmltag}>" >>"${fileoutput}"␊ |
151 | ␉␉␉}␊ |
152 | ␉␉␉else␊ |
153 | ␉␉␉{␊ |
154 | ␉␉␉␉echo "${line}" >>"${fileoutput}"␊ |
155 | ␉␉␉}␊ |
156 | ␉␉fi␊ |
157 | ␉␉matchlinefound="1"␊ |
158 | ␊ |
159 | ␉␉}␊ |
160 | ␉␉else␊ |
161 | ␉␉{␊ |
162 | ␊ |
163 | ␉␉if [ "${insertlinesskip}" ] && [ "${matchlinefound}" -gt 0 ] && [ "${matchlinefound}" -le "${insertlinesskip}" ]; then␊ |
164 | ␉␉␉{␊ |
165 | ␉␉␉if [ "${matchlinefound}" = "${insertlinesskip}" ]; then␊ |
166 | ␉␉␉␉{␊ |
167 | ␉␉␉␉arraysize=0␊ |
168 | ␉␉␉␉while [ ${arraysize} -lt ${#array[@]} ];␊ |
169 | ␉␉␉␉do␊ |
170 | ␉␉␉␉␉#echo "DEBUG: ${xmlelementindent}${array[${arraysize}]}"␊ |
171 | ␉␉␉␉␉echo "${xmlelementindent}${array[${arraysize}]}" >>"${fileoutput}"␊ |
172 | ␉␉␉␉ let arraysize="${arraysize}+1"␊ |
173 | ␉␉␉␉done␊ |
174 | ␉␉␉␉}␊ |
175 | ␉␉␉fi␊ |
176 | ␉␉␉}␊ |
177 | ␉␉fi␊ |
178 | ␊ |
179 | ␉␉if [ "${deletelines}" ] && [ "${matchlinefound}" -gt 0 ] && [ "${matchlinefound}" -lt $((${deletelines}+${deletelinesskip})) ] && [ "${matchlinefound}" -ge ${deletelinesskip} ]; then␊ |
180 | ␉␉echo "DEBUG: Delete=${matchlinefound}"␊ |
181 | ␉␉␉{␊ |
182 | ␉␉␉:␊ |
183 | ␉␉␉}␊ |
184 | ␉␉␉else␊ |
185 | ␉␉␉{␊ |
186 | ␉␉␉echo "${line}" >>"${fileoutput}"␊ |
187 | ␉␉␉}␊ |
188 | ␉␉fi␊ |
189 | ␊ |
190 | ␉␉if [ "${matchlinefound}" -gt 0 ]; then␊ |
191 | ␉␉␉let matchlinefound="${matchlinefound}+1"␊ |
192 | ␉␉fi␊ |
193 | ␊ |
194 | ␉␉}␊ |
195 | ␉fi␊ |
196 | ␉}␊ |
197 | done < "${fileinput}"␊ |
198 | ␊ |
199 | IFS=${prefunctionifs}␊ |
200 | ␊ |
201 | echo " rm -f ${fileinput}"␊ |
202 | rm -f "${fileinput}"␊ |
203 | }␊ |
204 | ␊ |
205 | ␊ |
206 | echo "==============================================="␊ |
207 | echo "Enter Build Boot Options script"␊ |
208 | echo "*******************************"␊ |
209 | ␊ |
210 | # get the passed destination drive?␊ |
211 | ␊ |
212 | if [ "$#" -eq 1 ]; then␊ |
213 | ␉destinationDrive="$1"␊ |
214 | ␉echo "DEBUG: passed argument for destinationDrive = $destinationDrive"␊ |
215 | fi␊ |
216 | ␊ |
217 | # Build Boot Options␊ |
218 | main "${1}" "${2}" "${3}" "${4}"␊ |
219 | ␊ |
220 | exit 0␊ |
221 | |