-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappInstaller-v1.0
192 lines (165 loc) · 7.77 KB
/
appInstaller-v1.0
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
#!/bin/bash
################################################################
# BASIC SCRIPT CONFIGURATION
################################################################
# Declare app name as shown in finder and dont add .app to the end)
applicationName=""
# Declare the latest version hosted in CDN
latestVersion=""
# Declare the Mosyle CDN variable/CDN URL of the package file
cdn=""
# Declare the SHA-256 hash of the package file
sha256=""
# Declare path of app
appFilePath="/Applications/$applicationName.app"
# Array of files to be deleted/uninstalled. Use this application to find residual files: https://freemacsoft.net/appcleaner/
filesToDelete=(
"/Applications/$applicationName"
)
##########################################################################################################################################
######################################################### ADVANCED OPTIONS BELOW #########################################################
##########################################################################################################################################
################################################################
# DIRECTORIES
################################################################
# Declare tmp folder
temporaryFolderPath="/tmp"
# Declare name of the downloaded installer
temporaryFileName="/${applicationName}-$(date +%s).pkg"
# Declare the directory for the parent log folder
logDirectory="/private/var/log/.apaIT"
# Declare the name of the log file
logFileName="${applicationName}_${latestVersion}_updater.log"
# Declare the path for the plist that contains the string that represents the version number
installedVersion=$(defaults read "$appFilePath/Contents/Info.plist" CFBundleShortVersionString 2>/dev/null || echo "null")
################################################################
# COMMANDS
################################################################
# Curl command used throught the script. https://ss64.com/osx/curl.html
callCurl="$( curl -s -S -L -k "$cdn" -o "${temporaryFolderPath}""${temporaryFileName}" ) 2>&1"
# Means by which SHA256 is determined. https://ss64.com/osx/shasum.html
shaCheck="$( shasum -a 256 "${temporaryFolderPath}""${temporaryFileName}" | awk '{print $1}' )"
################################################################
# FUNCTIONS
################################################################
# Function to iniciate the log
logStart() {
local datestamp
datestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "Session started @ $datestamp" | tee -a "$logDirectory/$logFileName"
}
# Function to update the log
logMessage() {
local timestamp
timestamp=$(date +"%H:%M:%S")
echo "[$timestamp] $1" | tee -a "$logDirectory/$logFileName"
}
# Function to handle end of log
logEnd() {
echo "----------" >> "$logDirectory/$logFileName"
}
# Function to handle script errors
handleError() {
local error_message=$1
logMessage "ERROR: $error_message"
logEnd
exit 1
}
# Function to handle successful completion of the script
handleSuccess() {
logMessage "Installation of $applicationName v$latestVersion complete."
logEnd
exit 0
}
# Function to delete files, given an array of paths
deleteFiles() {
local timestamp
timestamp=$(date +"%H:%M:%S")
local filesToDelete=("$@")
for file in "${filesToDelete[@]}"; do
if [[ -e "$file" ]]; then
rm -rf "$file"
echo "[$timestamp] Deleted file: $file" >> "$logDirectory/$logFileName"
fi
done
}
################################################################
# SCRIPT START
################################################################
# Check if the log directory exists or create it
[[ -d "$logDirectory" ]] || mkdir -p "$logDirectory"
logStart
if [[ -e "$appFilePath" ]]; then
if [[ "$installedVersion" = "$latestVersion" ]]; then
logMessage "$applicationName ($installedVersion) is installed. This device is up to date."
logEnd
exit 0
elif [[ "$installedVersion" = "null" ]]; then
logMessage "Device is reporting OS version: $(sw_vers -productVersion)"
logMessage "$applicationName appears to be installed but is malformed/corrupted. The \$installedVersion variable can not be found. Reinstallation is needed."
logMessage "Beginning download..."
if curl_output="$callCurl 2>&1"; then
logMessage "Download complete. Checking SHA-256..."
if [[ "$sha256" == "$shaCheck" ]]; then
logMessage "SHA-256 is valid. Uninstalling old version ($installedVersion)."
deleteFiles "${filesToDelete[@]}"
logMessage "Uninstallation successful. Beginning install of old version ($latestVersion)."
installer -pkg "${temporaryFolderPath}${temporaryFileName}" -target / | while read -r line; do logMessage "$line" ; done
handleSuccess
else
logMessage "ERROR: SHA-256 does not match. Exiting..."
logMessage "Expected SHA-256: $sha256"
logMessage "Downloaded SHA-256: $($shaCheck | awk '{print $1}')"
handleError "SHA-256 verification failed."
fi
else
handleError "Curl command failed. > $curl_output"
fi
elif [[ "$installedVersion" < "$latestVersion" ]]; then
logMessage "Device is reporting OS version: $(sw_vers -productVersion)"
logMessage "$applicationName is installed but NOT up to date. Installed version: $installedVersion, CDN Version: $latestVersion."
logMessage "Beginning download..."
if curl_output="$callCurl 2>&1"; then
logMessage "Download complete. Checking SHA-256..."
if [[ "$sha256" == "$shaCheck" ]]; then
logMessage "SHA-256 is valid. Uninstalling old version ($installedVersion)."
deleteFiles "${filesToDelete[@]}"
logMessage "Uninstallation successful. Beginning install of old version ($latestVersion)."
installer -pkg "${temporaryFolderPath}${temporaryFileName}" -target / | while read -r line; do logMessage "$line" ; done
handleSuccess
else
logMessage "Expected SHA-256: $sha256"
logMessage "Downloaded SHA-256: $($shaCheck | awk '{print $1}')"
handleError "SHA-256 verification failed."
fi
else
handleError "Curl command failed. > $curl_output"
fi
elif [[ "$installedVersion" > "$latestVersion" ]]; then
logMessage "Device is reporting OS version: $(sw_vers -productVersion)"
logMessage "WEIRD!!! Installed version (v$installedVersion) is newer than CDN version (v$latestVersion)."
handleSuccess
else
logMessage "Device is reporting OS version: $(sw_vers -productVersion)"
handleError "Unable to determine installed version: ($installedVersion)"
fi
else
logMessage "$applicationName is not installed."
logMessage "Device is reporting OS version: $(sw_vers -productVersion)"
logMessage "Beginning download..."
if curl_output="$callCurl 2>&1"; then
logMessage "Download complete. Checking SHA-256..."
if [[ "$sha256" == "$shaCheck" ]]; then
logMessage "SHA-256 is valid. Beginning install."
installer -pkg "${temporaryFolderPath}${temporaryFileName}" -target / | while read -r line; do logMessage "$line" ; done
handleSuccess
else
logMessage "ERROR: SHA-256 does not match. Exiting..."
logMessage "Expected SHA-256: $sha256"
logMessage "Downloaded SHA-256: $($shaCheck | awk '{print $1}')"
handleError "SHA-256 verification failed."
fi
else
handleError "Curl command failed. > $curl_output"
fi
fi