1#!/bin/bash
2
3export PATH=/bin:/usr/bin:/sbin:/usr/sbin
4shopt -s expand_aliases
5
6if [ "$UID" -ne 0 ]; then
7	echo '*** Auto-updater must be run as root.'
8	exit 1
9fi
10
11scriptPath="`dirname "$0"`/`basename "$0"`"
12if [ ! -s "$scriptPath" ]; then
13	scriptPath="$0"
14	if [ ! -s "$scriptPath" ]; then
15		echo "*** Auto-updater cannot determine its own path; $scriptPath is not readable."
16		exit 2
17	fi
18fi
19
20endMarkerIndex=`grep -a -b -E '^################' "$scriptPath" | head -c 16 | cut -d : -f 1`
21if [ "$endMarkerIndex" -le 100 ]; then
22	echo 'Internal error: unable to find end of script / start of binary data marker.'
23	exit 2
24fi
25blobStart=`expr $endMarkerIndex + 17`
26if [ "$blobStart" -le "$endMarkerIndex" ]; then
27	echo 'Internal error: unable to find end of script / start of binary data marker.'
28	exit 2
29fi
30
31rm -f /tmp/ZeroTierOne-update.pkg
32tail -c +$blobStart "$scriptPath" >/tmp/ZeroTierOne-update.pkg
33chmod 0600 /tmp/ZeroTierOne-update.pkg
34
35if [ -s /tmp/ZeroTierOne-update.pkg ]; then
36	rm -f '/Library/Application Support/ZeroTier/One/latest-update.exe' '/Library/Application Support/ZeroTier/One/latest-update.json' /tmp/ZeroTierOne-update.log
37	installer -verbose -pkg /tmp/ZeroTierOne-update.pkg -target / >/tmp/ZeroTierOne-update.log 2>&1
38	rm -f /tmp/ZeroTierOne-update.pkg
39	exit 0
40else
41	echo '*** Error self-unpacking update!'
42	exit 3
43fi
44
45# Do not remove the last line or add a carriage return to it! The installer
46# looks for an unterminated line beginning with 16 #'s in itself to find
47# the binary blob data, which is appended after it.
48
49################