-
Notifications
You must be signed in to change notification settings - Fork 0
/
!Script-Template.sh
executable file
·235 lines (184 loc) · 11.7 KB
/
!Script-Template.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
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
#!/bin/sh
####################################################################################################
# #
# ABOUT THIS PROGRAM #
# #
# NAME #
# Template Name #
# #
# SYNOPSIS #
# sudo scriptname.sh #
# #
####################################################################################################
# #
# HISTORY #
# Version: 1.0 #
# #
# - 1.0 Martyn Watts, DD.MM.YYYY Template #
# #
####################################################################################################
####################################################################################################
# Program Variables #
####################################################################################################
# The version of this script is used in the logs and will help with script debugging
scriptver="1.0"
# The url of the webpage to scrape for the latest version number and download link.
releaseNotesUrl='https://vendorweb.com/product_history/release_notes'
# Installation Method, are we using a DMG, PKG or ZIP to install the app?
installMethod="PKG"
# This is the name of the file we download this might be a dmg, a pkg or a zip.
dnldfile='Application Name.pkg'
# The name of the application once it is installed without the .app extension
appName='Installed Application Name'
# Do we want to force quit the existing application before updating
forceQuit='Y'
# The 2 Log files we create as were progressing through the script
# logfile is the main log which has each step and any errors sent to it
# deplog is used to update the depnotify application with statuses and increments the progress bar
logfile="/Library/Logs/${appName}InstallScript.log"
deplog="/var/tmp/depnotify.log"
####################################################################################################
# Script Startup #
####################################################################################################
# Create log files if they don't exist
if [[ ! -e ${logfile} ]]; then
/bin/echo "---- New Log ----" >> $logfile
fi
if [[ ! -e ${deplog} ]]; then
/bin/echo "---- New Log ----" >> $deplog
fi
# Open the log files if the openconsole parameter has been passed to the script
if [[ $@ == "openconsole" ]]; then
open ${logfile}
open ${deplog}
fi
# Log the script version and date
/bin/echo "`date`: Running script version: ${scriptver}" >> ${logfile}
####################################################################################################
# Gathering the latest version number #
####################################################################################################
# To get just the latest version number from the Release Notes URL
/bin/echo "`date`: Getting latest version number" >> ${logfile}
/bin/echo "Getting latest version number"
latestver=$(curl -s ${releaseNotesUrl} | grep 'find all lines with this word' | grep -vim 1 'find the first instance that excludes this word' | cut -f2 -d'"' | cut -f2 -d"-" | sed -e 's/\.[^.]*$//')
####################################################################################################
# Gathering the download url #
####################################################################################################
# To get the latest download link from the Release Notes URL
/bin/echo "`date`: Getting download url" >> ${logfile}
/bin/echo "Getting download url"
url=$(curl -s ${releaseNotesUrl} | grep 'download' | grep -vim 1 'beta' | cut -f2 -d'"')
# Display the latest version available in the logs
/bin/echo "`date`: Latest version number is: ${latestver}" >> ${logfile}
/bin/echo "Latest version number is: ${latestver}"
####################################################################################################
# Check to see if app already installed and get the version if it is #
####################################################################################################
# Is the app already installed?
if [[ -e "/Applications/${appName}.app" ]]; then
# Read the version number from the info.plist
currentinstalledver=`/usr/bin/defaults read "/Applications/${appName}.app/Contents/Info" CFBundleShortVersionString`
# Display the current installed version number in the logs
/bin/echo "Current installed version is: $currentinstalledver"
/bin/echo "`date`: Current installed version is: $currentinstalledver" >> ${logfile}
# Check to see if the latest available version is the same as the installed version
if [[ ${latestver} = ${currentinstalledver} ]]; then
/bin/echo "${appName} is current. Exiting"
/bin/echo "`date`: ${appName} is current. Exiting" >> ${logfile}
/bin/echo "Status: ${appName} is current. Exiting" >> ${deplog}
exit 0
fi
else
# update the logs to indicate that app is not already installed
currentinstalledver="none"
/bin/echo "${appName} is not installed"
/bin/echo "`date`: ${appName} is not installed" >> ${logfile}
fi
####################################################################################################
# Compare the two versions, if they are different or the App is not present then download and #
# install the new version. #
####################################################################################################
# Check to see if the versions do not match
if [[ "${currentinstalledver}" != "${latestver}" ]]; then
# Display current and available versions in the logs
/bin/echo "`date`: Current ${appName} version: ${currentinstalledver}" >> ${logfile}
/bin/echo "Current ${appName} version: ${currentinstalledver}"
/bin/echo "`date`: Available ${appName} version: ${latestver}" >> ${logfile}
/bin/echo "Available ${appName} version: ${latestver}"
# Download the file
/bin/echo "`date`: Downloading newer version." >> ${logfile}
/bin/echo "Downloading newer version."
/usr/bin/curl -s -o /tmp/${dnldfile} ${url}
# Force quit the application
if [[ "${forceQuit}" = "Y" ]]; then
killall ${appName}
fi
####################################################################################################
# Choose the correct installation method. #
####################################################################################################
if [[ $installMethod = "DMG"]]; then
# Mount the DMG
/bin/echo "`date`: Mounting installer disk image." >> ${logfile}
/usr/bin/hdiutil attach /tmp/${dnldfile} -nobrowse -quiet
# Copy the file to the Applications folder - you may need to change the mounted volume name
/bin/echo "`date`: Installing..." >> ${logfile}
ditto -rsrc "/Volumes/${appName}/${appName}.app" "/Applications/${appName}.app"
/bin/sleep 10
# Unmount the DMG
/bin/echo "`date`: Unmounting installer disk image." >> ${logfile}
/usr/bin/hdiutil detach $(/bin/df | /usr/bin/grep "${appName}" | awk '{print $1}') -quiet
/bin/sleep 10
else if [[ $installMethod = "PKG"]]; then
# Open the PKG with installer
cd /tmp
/usr/sbin/installer -pkg ${dnldfile} -target /
else
# Expand the archive
cd /tmp
unzip $dnlndfile
# Move the file to the Applications folder
mv "{appName}.app" "/Applications/${appName}.app"
/bin/sleep 10
fi
# Clean up after the installation
/bin/sleep 5
/bin/echo "`date`: Deleting the downloaded file." >> ${logfile}
/bin/rm /tmp/${dnldfile}
####################################################################################################
# Check to see if the update was successful. #
####################################################################################################
# Get the version of the app from the info.plist
newlyinstalledver=`/usr/bin/defaults read "/Applications/${appName}.app/Contents/Info" CFBundleShortVersionString`
# Compare the installed version against the latest version
if [[ "${latestver}" = "${newlyinstalledver}" ]]; then
/bin/echo "`date`: SUCCESS: ${appName} has been updated to version ${newlyinstalledver}" >> ${logfile}
/bin/echo "SUCCESS: ${appName} has been updated to version ${newlyinstalledver}"
# If dockutil is installed remove existing dock icon and add new one
if [[ -e "/usr/local/bin/dockutil" ]]; then
/bin/echo "`date`: Removing Existing Dock Icon." >> ${logfile}
/bin/echo "Removing Existing Dock Icon."
/usr/local/bin/dockutil --remove "${appName}" --allhomes >> ${logfile}
/bin/sleep 5
/bin/echo "`date`: Creating New Dock Icon." >> ${logfile}
/bin/echo "Creating New Dock Icon."
/usr/local/bin/dockutil --add "/Applications/${appName}.app" --position 1 --allhomes >> ${logfile}
/bin/sleep 3
fi
# Update log with Failed install and exit with exit code 1
else
/bin/echo "`date`: ERROR: ${appName} update unsuccessful, version remains at ${currentinstalledver}." >> ${logfile}
/bin/echo "ERROR: ${appName} update unsuccessful, version remains at ${currentinstalledver}."
/bin/echo "---- End of script ----" >> ${logfile}
exit 1
fi
# App is up to date already so we log it.
else
/bin/echo "`date`: ${appName} is already up to date, running ${currentinstalledver}." >> ${logfile}
/bin/echo "${appName} is already up to date, running ${currentinstalledver}."
fi
# Update log so show end of script
/bin/echo "---- End of script ----" >> ${logfile}
# Update DEP Notify to increment the progress bar - when used with multiple scripts
/bin/echo "Command: DeterminateManualStep: 1" >> ${deplog}
#Exit script with exit code 0
exit 0