-
Notifications
You must be signed in to change notification settings - Fork 10
/
MacOS_Updater
executable file
·713 lines (612 loc) · 21.4 KB
/
MacOS_Updater
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#!/bin/sh
#
# By Gengik84, rewritten by PMheart
#
# Based on Pike R. Alpha's script and crazybirdy's modifications.
#
# Many thanks to PikeRAlpha, crazybirdy and PMheart!
#
# uncomment to enable debug logging
# set -x
# Change additional shell optional behavior (expand unmatched names to a null string).
shopt -s nullglob
#
# VARIABLES
#
#----------------------------------------------------------------------------------------------------
#
# script version
#
gScriptVersion="1.0.9"
#
# Beta version (ONLY the latest Beta)
# could be empty when there is no Beta available. (consider 10.14.6)
#
gBetaVersion="10.14.4"
#
# stable version (ONLY the latest stable)
#
gStableVersion="10.14.3"
#
# Current macOS version returned from sw_vers.
#
gProductVersion="$(sw_vers -productVersion)"
#
# Current macOS major version.
# For example: given gProductVersion 10.14.3, gProductMajorVersion will be 14.
#
gProductMajorVersion="${gProductVersion:3:2}"
#
# Current macOS minor version.
# For example: given gProductVersion 10.14.3, gProductMinorVersion will be 3.
#
gProductMinorVersion="${gProductVersion:6:2}"
# NOTE: It will be null when no minor version available. Appending 0.
[[ -z "${gProductMinorVersion}" ]] && gProductMinorVersion=0
#
# Beta Major version
# For example: given gBetaVersion 10.14.4, gBetaMajorVersion will be 14.
#
gBetaMajorVersion="${gBetaVersion:3:2}"
#
# Beta Minor version
# For example: given gBetaVersion 10.14.4, gBetaMinorVersion will be 4.
#
gBetaMinorVersion="${gBetaVersion:6:2}"
# NOTE: It will be null when no minor version available. Appending 0.
[[ -z "${gBetaMinorVersion}" ]] && gBetaMinorVersion=0
#
# Stable Major version
# For example: given gStableVersion 10.14.4, gStableMajorVersion will be 14.
#
gStableMajorVersion="${gStableVersion:3:2}"
#
# Stable Minor version
# For example: given gStableVersion 10.14.3, gStableMinorVersion will be 3.
#
gStableMinorVersion="${gStableVersion:6:2}"
# NOTE: It will be null when no minor version available. Appending 0.
[[ -z "${gStableMinorVersion}" ]] && gStableMinorVersion=0
#
# Package Mode:
# Delta - a more lightweight package, should only be used on the most recent macOS.
# Combo - a reasonably full package, could be used on all major releases, used by default.
#
# When to use Delta (pseudo code):
# gProductMajorVersion == gBetaMajorVersion &&
# gProductMinorVersion == gBetaMinorVersion [e.g update from 10.14.4 b1 to 10.14.4 b3] ||
# gProductMinorVersion == (gBetaMinorVersion - 1) [e.g update from 10.14.3 to 10.14.4 b3]
#===============================================================================================================
# gProductMajorVersion == gStableMajorVersion &&
# gProductMinorVersion == gStableMinorVersion [e.g update from 10.14.3 to 10.14.3 supplemental update] ||
# gProductMinorVersion == (gStableMinorVersion - 1) [e.g update from 10.14.2 to 10.14.3]
#
# When to use Combo:
# Of course, scenarios other than ones using Delta...
#
gPackageMode="Combo"
#----------------------------------------------------------------------------------------------------
#
# GLOBAL VARS
# WILL BE UPDATED BY CERTAIN FUNCTIONS
#
#----------------------------------------------------------------------------------------------------
# selected channel for determining URL
# will be updated by selectChannel()
gChannel=""
# selected catalog URL (see channels and catalogs above)
# will be updated by selectChannel()
gCatalog=""
# selected system version
gVersion=""
# target destination
gVolume=""
# package keys
# will be updated by parseCatalog()
gUrlIndex=""
gKeyNum=""
gKeyIndex=""
gKeySalt=""
# the exact url
gUrl=""
# distribution file
gDist=""
# initialisation of a variable (our target folder).
gTmpDirectory="/tmp"
gWorkDir=""
# name of target installer package
gInstallerPackage="installer.pkg"
# backup dir
gUserPath="/Users/`users`/Desktop"
#----------------------------------------------------------------------------------------------------
#
# CONSTANTS
# NO CHANGE NEEDED HERE
#
#----------------------------------------------------------------------------------------------------
# possible channels
gChannels=(
# dev beta (pub beta has been dropped)
"Beta"
# normal
"Normal Channel"
)
# possible catalog URLs (corresponding to channels above)
gCatalogs=(
# beta
"https://swscan.apple.com/content/catalogs/others/index-10.14seed-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog.gz"
# normal
"https://swscan.apple.com/content/catalogs/others/index-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog.gz"
)
# colors
RED="\e[31m"
GREEN="\e[32m"
DARK_YELLOW="\e[33m"
BLUE="\e[34m"
PURPLE="\e[35m"
CYAN="\e[36m"
OFF="\e[0m"
#----------------------------------------------------------------------------------------------------
function STY_LINE() {
printf '%s\n\n' '-------------------------------------------------------------------------------'
}
function setPackageMode() {
if [[ "${gChannel}" == 'Beta' ]]; then
let "gBetaMinorVersionMinusOne = gBetaMinorVersion - 1"
if [[ "${gProductMajorVersion}" -eq "${gBetaMajorVersion}" && \
"${gProductMinorVersion}" -eq "${gBetaMinorVersion}" || \
"${gProductMinorVersion}" -eq "${gBetaMinorVersionMinusOne}" ]]; then
printf "\nUsing ${GREEN}Delta${OFF} package (Beta).\n\n"
gPackageMode='Delta'
else
printf "\nUsing ${DARK_YELLOW}Combo${OFF} package (Beta).\n\n"
fi
else
let "gStableMinorVersionMinusOne = gStableMinorVersion - 1"
if [[ "${gProductMajorVersion}" -eq "${gStableMajorVersion}" && \
"${gProductMinorVersion}" -eq "${gStableMinorVersion}" || \
"${gProductMinorVersion}" -eq "${gStableMinorVersionMinusOne}" ]]; then
printf "\nUsing ${GREEN}Delta${OFF} package (Stable).\n\n"
gPackageMode='Delta'
else
printf "\nUsing ${DARK_YELLOW}Combo${OFF} package (Stable).\n\n"
fi
fi
}
function updateScript() {
local remoteAddr="https://raw.githubusercontent.com/Gengik84/MacOS_Updater/master/MacOS_Updater"
local tmpScriptPath="/tmp/MacOS_Updater"
# download remote script
rm -f "${tmpScriptPath}" && curl -s "${remoteAddr}" -o "${tmpScriptPath}" || exit 1
local remoteVer=$(cat ${tmpScriptPath} | grep '^gScriptVersion=' | tr -cd '.0-9')
if [[ "${gScriptVersion}" != "${remoteVer}" ]]; then
clear
printf "\tLocal Script Version: ${RED}${gScriptVersion}${OFF}\t\tUpdate available: ${GREEN}v${remoteVer}${OFF}\n"
local confirmScptUpd
read -p "Do you want update to the latest version? (y/n) " confirmScptUpd
case "${confirmScptUpd}" in
y|Y )
cp "${tmpScriptPath}" "$0"
STY_LINE
printf "${BLUE}Running the new script...${OFF}"
local args="$@"
"$0" "${args}"
# do not run the current (old) script again
exit 0
;;
esac
fi
}
function downloadFile() {
local link="$1"
local out="$2"
local attempt=0
local maxTry=3
for ((; attempt < maxTry; attempt++ )); do
curl "${link}" -o "${out}"
if [[ $? -ne 0 ]]; then
# failed, remove the "corrupted" file and try again
rm -rf "${out}"
else
# success
return 0
fi
sleep 0.5
done
printf "Failed to download ${RED}${out}${OFF} after ${attempt} times trying!\n"
exit 1
}
function registerScript() {
local prefDir="/Users/`users`/.MacOS_Updater"
local prefFile="${prefDir}/pref.conf"
[[ ! -d "${prefDir}" ]] && mkdir "${prefDir}"
if [[ -f "${prefFile}" ]]; then
source "${prefFile}"
else
clear
echo 'It seems to be the first time you are using this script,'
echo 'for convenience, you may want to register it,'
echo 'in this case you will no more be prompted to enter the password'
echo 'until having been moved to elsewhere.'
echo 'Note that it might also be a security risk when registering.'
echo
echo 'Well, it is totally up to you to register or not,'
echo 'and you will be no more asked you after this time.'
echo
while true; do
local confirmRegScpt
read -p "Would you like to? (y/n) " confirmRegScpt
case "${confirmRegScpt}" in
y|Y )
echo 'wantRegScpt=1' > "${prefFile}"
break
;;
n|N )
echo 'wantRegScpt=0' > "${prefFile}"
break
;;
* )
echo 'Try again...'
;;
esac
done
fi
if [[ "${wantRegScpt}" -eq 1 ]]; then
local regFile="/etc/sudoers.d/MacOS_Updater"
local context="$(users) ALL=(ALL) NOPASSWD: ${0}"
if [[ ! -f "${regFile}" || \
"$(cat ${regFile})" != "${context}" ]]; then
rm -f "${regFile}"
echo "${context}" | sudo tee "${regFile}" &> /dev/null
fi
fi
}
function selectChannel() {
local i=0
while true; do
# list all possible channels
printf "${CYAN}SELECT A CHANNEL${OFF}\n"
for c in "${gChannels[@]}"; do
printf "[ ${GREEN}${i}${OFF} ] ${PURPLE}${c}${OFF}\n"
let i++
done
local channelNum
read -p ">> " channelNum
# insane choice?!
if [[ -z "${channelNum}" || ! "${channelNum}" =~ ^[0-9]+$ || "${channelNum}" -lt 0 || "${channelNum}" -ge $i ]]; then
printf "${RED}Wrong choice!${OFF}\n\n"
# restore i to 0 for another traversal
i=0
continue
fi
# update vars
gChannel="${gChannels[channelNum]}"
gCatalog="${gCatalogs[channelNum]}"
# ask for confirmation
printf "Your choice: [ ${GREEN}${channelNum}${OFF} ] ${PURPLE}${gChannel}${OFF}\n"
while true; do
local confirmChannel
read -p ">> CONFIRM? (Y/N) " confirmChannel
case "${confirmChannel}" in
"" )
printf 'Please do type Y if you are sure.\n'
continue
;;
y|Y )
# done, terminate the function
return
;;
n|N )
printf 'goodbye\n'
exit 0
;;
* )
printf 'Try again...\n\n'
# restore i to 0 for another traversal
i=0
# go to the last loop
break
;;
esac
done
done
}
function parseCatalog() {
selectChannel
# show version
if [[ "${gChannel}" == "Beta" && ! -z "${gBetaVersion}" ]]; then
gVersion="${gBetaVersion}"
elif [[ "${gChannel}" == "Beta" && -z "${gBetaVersion}" ]]; then
printf "${RED}Warning:${OFF} using Beta channel but there is no release at the moment!\n"
printf "Redirecting to ${PURPLE}Normal Channel ${OFF}and using version ${GREEN}${gStableVersion}${OFF}.\n"
# manually put normal channel and stable version here for redirection
gChannel="${gChannels[1]}"
gCatalog="${gCatalogs[1]}"
gVersion="${gStableVersion}"
sleep 2
elif [[ "${gChannel}" != "Beta" ]]; then
gVersion="${gStableVersion}"
fi
# check if it is fine to go with Delta
setPackageMode
# download catalog
cd "${gTmpDirectory}"
[[ -f update-catalogs ]] && rm -f update-catalogs
downloadFile "${gCatalog}" "update-catalogs.gz" && gunzip update-catalogs.gz || exit 1
# update vars
if [[ "${gPackageMode}" == 'Combo' ]]; then
gUrlIndex="$(cat /tmp/update-catalogs | \
grep macOSUpdCombo${gVersion} | grep pkg | grep -v RecoveryHDUpdate | grep -v Patch | grep -v integrityData | \
sed 's/<string>//' | sed 's/<\/string>//' | \
awk '{print $1;}' | sed 's/^.*downloads//')"
else
gUrlIndex="$(cat /tmp/update-catalogs | \
grep macOSUpd${gVersion} | grep pkg | grep -v RecoveryHDUpdate | grep -v Patch | grep -v integrityData | \
sed 's/<string>//' | sed 's/<\/string>//' | \
awk '{print $1;}' | sed 's/^.*downloads//')"
fi
[[ -z "${gUrlIndex}" ]] && printf "ERROR: ${RED}URL Index not found!${OFF}\n" && exit 1
gKeyNum="$( echo ${gUrlIndex} | cut -d/ -f2,3)"
gKeyIndex="$(echo ${gUrlIndex} | cut -d/ -f4,4)"
gKeySalt="$( echo ${gUrlIndex} | cut -d/ -f5,5)"
gUrl="https://swdist.apple.com/content/downloads/${gKeyNum}/${gKeyIndex}/${gKeySalt}/"
gDist="${gKeyIndex}.English.dist"
# create our working dir
gWorkDir="${gTmpDirectory}/${gKeyIndex}"
[[ ! -d "${gWorkDir}" ]] && mkdir "${gWorkDir}"
cd "${gWorkDir}"
# download dist
rm -f "${gDist}"
printf "Downloading: ${BLUE}${gDist}${OFF}...\n"
downloadFile "${gUrl}${gDist}" "${gDist}"
# show build
local awkNR=0
[[ "${gChannel}" == "Beta" && ! -z "${gBetaVersion}" ]] && awkNR=13 || awkNR=12
build="$(cat ${gDist} | \
sed 's/<string>//' | sed 's/<\/string>//' | \
awk '{print $1;}' | awk NR==${awkNR})"
confirmDownload
}
function confirmDownload() {
while true; do
printf "${RED}${gVersion} (${build})${OFF} will be downloaded, confirm? (y/n) "
local confirmVer
read -ea confirmVer
case "${confirmVer}" in
"" )
printf 'Please do type Y if you are sure.\n'
continue
;;
y|Y )
# done, terminate the function
return
;;
n|N )
rm -rf "${gWorkDir}"
printf 'goodbye\n'
exit 0
;;
* )
printf 'Try again...\n\n'
continue
;;
esac
done
}
function downloadPackage() {
# now we are inside "${gWorkDir}" thanks to the prior parseCatalog()
# packages to be downloaded
if [[ "${gPackageMode}" == 'Combo' ]]; then
local targetFiles=(
"macOSUpdCombo${gVersion}.pkg"
"macOSUpdCombo${gVersion}.RecoveryHDUpdate.pkg"
"macOSBrain.pkg"
"SecureBoot.pkg"
"EmbeddedOSFirmware.pkg"
"FirmwareUpdate.pkg"
"FullBundleUpdate.pkg"
)
else
local targetFiles=(
"macOSUpd${gVersion}.pkg"
"macOSUpd${gVersion}.RecoveryHDUpdate.pkg"
"macOSBrain.pkg"
"SecureBoot.pkg"
"EmbeddedOSFirmware.pkg"
"FirmwareUpdate.pkg"
"FullBundleUpdate.pkg"
)
fi
# download them
clear
for filename in "${targetFiles[@]}"; do
if [[ ! -e "${filename}" ]]; then
printf "Downloading: ${BLUE}${filename}${OFF}...\n"
downloadFile "${gUrl}${filename}" "${filename}"
elif [[ -z "$(pkgutil --check-signature ${filename} | grep -i 'signed Apple Software')" ]]; then
printf "File: ${filename} is ${RED}CORRUPTED${OFF}, redownloading...\n"
rm -rf "${filename}"
downloadFile "${gUrl}${filename}" "${filename}"
else
printf "File: ${GREEN}${filename}${OFF} is already there, ${DARK_YELLOW}skipping download.${OFF}\n"
fi
STY_LINE
done
}
function selectDestination() {
cd /Volumes
local i=0
local targetVolumes=(*)
while true; do
printf "${CYAN}SELECT A DESTINATION${OFF}\n"
for vol in "${targetVolumes[@]}"; do
printf "[ ${GREEN}${i}${OFF} ] ${CYAN}${vol}${OFF}\n"
let i++
done
# ask to select a target volume
local volNum
read -p ">> " volNum
# insane choice?!
if [[ -z "${volNum}" || ! "${volNum}" =~ ^[0-9]+$ || "${volNum}" -lt 0 || "${volNum}" -ge $i ]]; then
printf "${RED}Wrong choice!${OFF}\n\n"
# restore i to 0 for another traversal
i=0
continue
fi
local volume="${targetVolumes[volNum]}"
gVolume="/Volumes/${volume}"
printf "Your choice: [ ${GREEN}${volNum}${OFF} ] ${CYAN}${volume}${OFF}\n"
while true; do
local confirmVol
read -p ">> CONFIRM? (y/n) " confirmVol
case "${confirmVol}" in
"" )
printf 'Please do type Y if you are sure.\n'
continue
;;
y|Y )
# done, terminate the function
return
;;
* )
printf 'Try again...\n\n'
# restore i to 0 for another traversal
i=0
# go to the last loop
break
;;
esac
done
done
# create the enrollment plist
local seedEnrollmentPlist="${vol}/Users/Shared/.SeedEnrollment.plist"
# write enrollement plist when missing (seed program options: CustomerSeed, DeveloperSeed)
if [[ ! -f "${seedEnrollmentPlist}" ]]; then
echo '<?xml version="1.0" encoding="UTF-8"?>' > "${seedEnrollmentPlist}"
echo '<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >> "${seedEnrollmentPlist}"
echo '<plist version="1.0">' >> "${seedEnrollmentPlist}"
echo ' <dict>' >> "${seedEnrollmentPlist}"
echo ' <key>SeedProgram</key>' >> "${seedEnrollmentPlist}"
if [[ "${gChannel}" == "Beta" ]]; then
echo ' <string>DeveloperSeed</string>' >> "${seedEnrollmentPlist}"
else
echo ' <string>CustomerSeed</string>' >> "${seedEnrollmentPlist}"
fi
echo ' </dict>' >> "${seedEnrollmentPlist}"
echo '</plist>' >> "${seedEnrollmentPlist}"
fi
}
function updateRecovery() {
printf "${CYAN}UPDATING RECOVERY${OFF}\n"
cd "${gTmpDirectory}"
rm -rf "updRec" && mkdir "updRec" && cd "updRec"
# download dm for updating Recovery
curl -O "https://raw.githubusercontent.com/Gengik84/MacOS_Updater/master/dm" && chmod +x dm || exit 1
local dmBin="${gTmpDirectory}/updRec/dm"
if [[ "${gPackageMode}" == 'Combo' ]]; then
local recPkg="${gWorkDir}/macOSUpdCombo${gVersion}.RecoveryHDUpdate.pkg"
else
local recPkg="${gWorkDir}/macOSUpd${gVersion}.RecoveryHDUpdate.pkg"
fi
[[ ! -f "${recPkg}" ]] && printf "ERROR: ${RED}${recPkg} NOT found!${OFF}\n" && exit 1
local tmpRecUpdDir="/tmp/RecoveryHDUpdate"
rm -rf "${tmpRecUpdDir}" && pkgutil --expand-full "${recPkg}" "${tmpRecUpdDir}"
local tmpDmg="${tmpRecUpdDir}/RecoveryHDMeta.dmg"
# attempt to create a temporary mount point
local tmpMountPoint="$(/usr/bin/mktemp -d)"
# mount tmpDmg there
/usr/bin/hdiutil attach -nobrowse "${tmpDmg}" -mountpoint "${tmpMountPoint}"
# probe target volume
local fsType="$(diskutil info "${gVolume}" | awk '$1 == "Type" { print $NF }')"
# start updating
if [[ "${fsType}" == "apfs" ]]; then
"${dmBin}" ensureRecoveryBooter "${gVolume}" -base "${tmpMountPoint}/BaseSystem.dmg" "${tmpMountPoint}/BaseSystem.chunklist" -diag "${tmpMountPoint}/AppleDiagnostics.dmg" "${tmpMountPoint}/AppleDiagnostics.chunklist" -diagmachineblacklist 0 -installbootfromtarget 0 -slurpappleboot 0 -delappleboot 0 -addkernelcoredump 0
else
"${dmBin}" ensureRecoveryPartition "${gVolume}" "${tmpMountPoint}/BaseSystem.dmg" "${tmpMountPoint}/BaseSystem.chunklist" "${tmpMountPoint}/AppleDiagnostics.dmg" "${tmpMountPoint}/AppleDiagnostics.chunklist" 0 0 0
fi
# done
/usr/bin/hdiutil eject "${tmpMountPoint}"
rm -rf "${tmpMountPoint}"
rm -rf "${tmpRecUpdDir}"
}
function updatePrimarySystem() {
printf "${CYAN}UPDATING PRIMARY SYSTEM${OFF}\n"
cd "${gWorkDir}"
# remove these lines to avoid installer error
sed -e '/volume-check/d' "${gDist}" > new1.txt
sed -e '/installation-check/d' new1.txt > new2.txt
sed -e '/RecoveryHDUpdate/d' new2.txt > new3.txt
sed -e '/system-image/d' new3.txt > new4.txt
# remove macOS${gVersion}Patch.pkg since it will cause installation failure
sed -e '/Patch/d' new4.txt > new5.txt
cp new5.txt "${gDist}"
rm new?.txt
# build pkg
productbuild --distribution "${gWorkDir}/${gDist}" --package-path "${gWorkDir}" "${gWorkDir}/${gInstallerPackage}"
# perform the update by launching the installer
[[ -f "${gWorkDir}/${gInstallerPackage}" ]] && /usr/sbin/installer -pkg "${gWorkDir}/${gInstallerPackage}" -target "${gVolume}"
}
function updateSystem() {
# update Recovery first
updateRecovery
# then primary system
updatePrimarySystem
}
function ask4Backup() {
local confirmBackup
read -p "Do you want to backup installer.pkg? (y/N) " confirmBackup
case "${confirmBackup}" in
y|Y )
printf "${CYAN}BACKING UP${OFF}\n"
hdiutil create -format UDZO -srcfolder "${gWorkDir}/${gInstallerPackage}" "${gUserPath}/Installer_${gVersion}_${build}"
;;
esac
}
function endProgram() {
printf "${CYAN}CLEANING UP${OFF}\n"
rm -rf "${gWorkDir}"
rm -rf "${gTmpDirectory}/update-catalogs"
rm -rf "${gTmpDirectory}/updRec"
STY_LINE
printf "${GREEN}RESTART REQUIRED${OFF}\n"
local confirmReboot
read -p "Do you want to reboot now? (y/N) " confirmReboot
case "${confirmReboot}" in
y|Y )
reboot
;;
esac
printf "${RED}ALL DONE${OFF}\nPlease reboot manually.\n"
}
function main() {
updateScript "$@"
# try to register the script
registerScript
# direct update from a pre-built pkg
case "$1" in
-i|--installer )
selectDestination
local installerPKG
read -p "Drag installer.pkg on terminal windows: " installerPKG
[[ ! -f "${installerPKG}" ]] && echo "${installerPKG} not found!" && exit 1
printf "${CYAN}UPDATING PRIMARY SYSTEM${OFF}\n"
/usr/sbin/installer -pkg "${installerPKG}" -target "${gVolume}"
endProgram
# done
exit 0
;;
esac
parseCatalog
downloadPackage
selectDestination
updateSystem
ask4Backup
endProgram
}
clear
if [[ $EUID -ne 0 ]]; then
echo "$(basename "$0") must be run as ROOT!"
sudo "$0" "$@"
else
main "$@"
fi