forked from dnschneid/crouton
/
croutonversion
executable file
·144 lines (125 loc) · 4.2 KB
/
croutonversion
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
#!/bin/sh -e
# Copyright (c) 2014 The crouton Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
VERSION='unknown'
RELEASE='unknown'
ARCH='unknown'
APPLICATION="${0##*/}"
CHANGES=''
DOWNLOAD=''
DEST="$HOME/Downloads/crouton"
UPDATES=''
BRANCH="${VERSION##*~}"
BRANCH="${BRANCH%%:*}"
COMMIT="${VERSION##*:}"
CHANGESURL="https://github.com/dnschneid/crouton/compare/$COMMIT..."
CROUTONURL='http://goo.gl/cZ2PL'
USAGE="$APPLICATION [options]
Reports the version of crouton installed, checks if updates are available,
and/or launches a changelog.
If no options are specified, outputs crouton version information to STDOUT.
Options:
-a Prints the architecture of this chroot to stdout.
-c Launches a browser to view the list of changes.
If -u is specified, only launches if a newer version exists.
-d Downloads the latest version of crouton to the location specified.
If -u is specified, only downloads if the version is newer.
-f FILE Changes the destination of the downloaded crouton.
Default: $DEST
-h Prints out usage.
-r Prints the release of this chroot to stdout.
-u Checks for updates, and prints out the updated version number."
# Function to exit with exit code $1, spitting out message $@ to stderr
error() {
local ecode="$1"
shift
echo "$*" 1>&2
exit "$ecode"
}
# Process arguments
while getopts 'acdf:ru' f; do
case "$f" in
a) echo "$ARCH"; exit 0;;
c) CHANGES='y';;
d) DOWNLOAD='y';;
f) DEST="$OPTARG";;
r) echo "$RELEASE"; exit 0;;
u) UPDATES='y';;
\?) error 2 "$USAGE";;
esac
done
# No extra parameters
if [ ! "$#" = "$((OPTIND-1))" ]; then
error 2 "$USAGE"
fi
# Print out version if nothing else specified
if [ -z "$CHANGES$DOWNLOAD$UPDATES" ]; then
echo "crouton: version $VERSION"
echo "release: $RELEASE"
echo "architecture: $ARCH"
targetfile='/etc/crouton/targets'
if [ -r "$targetfile" ]; then
echo "targets: `sed 's/^,//' "$targetfile"`"
fi
hostrel='/var/host/lsb-release'
if [ -r "$hostrel" ]; then
host="`awk -F= '/_RELEASE_DESCRIPTION=/{print $2}' "$hostrel"`"
fi
echo "host: version ${host:-unknown}"
exit 0
fi
# Print out version to stderr for info
echo "crouton: version $VERSION" 1>&2
latest=''
tmpdir=''
if [ -n "$UPDATES$DOWNLOAD" ]; then
tmpdir="`mktemp -d --tmpdir=/tmp crouton.XXX`"
trap "rm -rf --one-file-system '$tmpdir'" INT HUP 0
echo "Retrieving latest version of crouton" 1>&2
if ! wget "$CROUTONURL" -O "$tmpdir/crouton" 2>"$tmpdir/log"; then
cat "$tmpdir/log" 1>&2
echo "Failed to retrieve latest version of crouton" 1>&2
exit 1
fi
latest="`awk -F"'" '/^VERSION=/{print $2;exit}' "$tmpdir/crouton"`"
fi
# Print out latest version number if requested
if [ -n "$UPDATES" ]; then
echo "latest: version $latest"
elif [ -n "$latest" ]; then
echo "latest: version $latest" 1>&2
fi
# Save latest version if requested
if [ -n "$DOWNLOAD" -a ! "$VERSION" = "$latest" ]; then
echo "Saving latest version to $DEST" 1>&2
mv -f "$tmpdir/crouton" "$DEST"
fi
# Launch changelog if requested
if [ -n "$CHANGES" -a ! "$VERSION" = "$latest" ]; then
# Check if changelogs are available from this version / to this branch
if ! wget "$CHANGESURL$BRANCH" -O/dev/full 2>&1 | grep -q '404 Not Found'; then
CHANGESURL="$CHANGESURL$BRANCH"
elif ! wget "${CHANGESURL}master" -O/dev/full 2>&1 | grep -q '404 Not Found'; then
CHANGESURL="${CHANGESURL}master"
else
# Fall back on the main commit log
CHANGESURL='https://github.com/dnschneid/crouton/commits/'
fi
# One of these will probably work...
for x in exo-open gnome-open kde-open xdg-open \
sensible-browser x-www-browser www-browser; do
if hash "$x" 2>/dev/null; then
browser="$x"
break
fi
done
if [ -z "$browser" ]; then
error 2 "No browser found to view $CHANGESURL"
fi
# Launch the webpage
if ! "$browser" "$CHANGESURL"; then
error 1 "Failed to launch browser to view $CHANGESURL"
fi
fi
exit 0