forked from ProfileManifests/ProfileManifests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateIndexManifests
executable file
·128 lines (103 loc) · 5.09 KB
/
updateIndexManifests
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
#!/bin/bash
if (($( sw_vers -productVersion | awk -F. '{ print $2 }' ) < 14)); then
printf "%s\n" "This script requires macOS 10.14 or higher to create correct hashes for the manifests..."
exit 1
fi
# Verify the script is running inside the ProfileManifests repository and get the repository root path
repository_path=$( git rev-parse --show-toplevel 2> /dev/null )
if !( [[ $? -eq 0 ]] && [[ $(git remote -v) == *"ProfileManifests.git"* ]] ); then
printf "%s\n" "This script must be run inside the ProfileManifests repository."
exit 1
fi
cd $repository_path
# Set the path to the index file
index_path="${repository_path}/Manifests/index"
# Set the path to the temporary index file
index_path_tmp="/private/tmp/index.$( cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 8 )"
# List all directories to index
manifest_directories=( 'ManagedPreferencesApple' 'ManifestsApple' 'ManagedPreferencesApplications' 'ManagedPreferencesDeveloper' )
# Loop through all directories
for directory_name in "${manifest_directories[@]}"; do
directory_path="${repository_path}/Manifests/${directory_name}"
if ! [[ -d ${directory_path} ]]; then
printf "%s\n" "${directory_path}: No such file or directory"
continue
fi
# Create the directory dictionary
if ! /usr/libexec/PlistBuddy -c "Add :${directory_name} dict" "${index_path_tmp}" &>/dev/null; then
printf "%s\n" "${file_name}: Failed to add dictionary with key: ${directory_name}"
exit 1
fi
# Loop through all files in the directory
for file in "${directory_path}"/*.plist; do
# Get the file name
file_name=$( basename "${file}" )
# Get the value of the pfm_version key
file_version=$( /usr/libexec/PlistBuddy -c "Print pfm_version" "${file}" 2>/dev/null )
# Verify the value returned is valid
if [[ -z ${file_version} ]] && ! /usr/libexec/PlistBuddy -c "Add pfm_version integer 1" "${file}"; then
printf "%s\n" "${file_name}: Failed to add value: 1 for key: pfm_version"
exit 1
elif ! [[ ${file_version} =~ ^[0-9]+$ ]]; then
printf "%s\n" "${file_name}: Invalid or missing value: ${file_version} for key: pfm_version"
exit 1
fi
# Get the file hash
file_hash=$( plutil -convert binary1 -o - "${file}" | md5 -q )
# Get the value of the pfm_domain key
file_domain=$( /usr/libexec/PlistBuddy -c "Print pfm_domain" "${file}" 2>/dev/null )
# Get the value of the pfm_subdomain key
file_subdomain=$( /usr/libexec/PlistBuddy -c "Print pfm_subdomain" "${file}" 2>/dev/null )
# Verify the value returned is valid
if [[ -z ${file_domain} ]]; then
printf "%s\n" "${file_name}: Invalid or missing value: ${file_domain} for key: pfm_domain"
exit 1
fi
# Add subdomain to domain if it exists
if [[ -n ${file_subdomain} ]]; then
file_domain="${file_domain}-${file_subdomain}"
fi
# Get the last modification time of the file
file_last_modified=$( /usr/libexec/PlistBuddy -c "Print pfm_last_modified" "${file}" 2>/dev/null )
# Verify the value returned is valid
if [[ -z ${file_last_modified} ]] && ! /usr/libexec/PlistBuddy -c "Add pfm_last_modified date $( LANG=C /bin/date +%a" "%b" "%d" "%T" "%Z" "%Y )" "${file}"; then
printf "%s\n" "${file_name}: Failed to add value: $( LANG=C /bin/date +%a" "%b" "%d" "%T" "%Z" "%Y ) for key: pfm_last_modified"
exit 1
fi
# Create the file dictionary
if ! /usr/libexec/PlistBuddy -c "Add :${directory_name}:${file_domain} dict" "${index_path_tmp}"; then
printf "%s\n" "${file_name}: Failed to add dictionary with key: ${directory_name}:${file_domain}"
exit 1
fi
# Add the file version
if ! /usr/libexec/PlistBuddy -c "Add :${directory_name}:${file_domain}:version integer ${file_version}" "${index_path_tmp}"; then
printf "%s\n" "${file_name}: Failed to add value: ${file_version} for key: ${directory_name}:${file_domain}:version"
exit 1
fi
# Add the file hash
if ! /usr/libexec/PlistBuddy -c "Add :${directory_name}:${file_domain}:hash string ${file_hash}" "${index_path_tmp}"; then
printf "%s\n" "${file_name}: Failed to add value: ${file_hash} for key: ${directory_name}:${file_domain}:hash"
exit 1
fi
# Add the file path
if ! /usr/libexec/PlistBuddy -c "Add :${directory_name}:${file_domain}:path string Manifests/${directory_name}/${file_name}" "${index_path_tmp}"; then
printf "%s\n" "${file_name}: Failed to add value: ${directory_name}/${file_name} for key: ${directory_name}:${file_domain}:path"
exit 1
fi
# Add the file last modified
if ! /usr/libexec/PlistBuddy -c "Add :${directory_name}:${file_domain}:modified date ${file_last_modified}" "${index_path_tmp}"; then
printf "%s\n" "${file_name}: Failed to add value: ${directory_name}/${file_name} for key: ${directory_name}:${file_domain}:modified"
exit 1
fi
done
done
# Set the date the file was generated
if ! /usr/libexec/PlistBuddy -c "Add :date date $( LANG=C /bin/date +%a" "%b" "%d" "%T" "%Z" "%Y )" "${index_path_tmp}"; then
printf "%s\n" "Failed to add value for key: date"
exit 1
fi
# Move the temporary file in place
if ! mv "${index_path_tmp}" "${index_path}"; then
printf "%s\n" "Failed to move the temporary index file to the repository"
exit 1
fi