-
Notifications
You must be signed in to change notification settings - Fork 11
/
install.sh
169 lines (144 loc) · 4.78 KB
/
install.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
#!/bin/bash
export OWNER=wakatara
export REPO=harsh
export SUCCESS_CMD="$REPO --version"
export BINLOCATION="/usr/local/bin"
version=$(curl -sI https://github.com/$OWNER/$REPO/releases/latest | grep -i "location:" | awk -F"/" '{ printf "%s", $NF }' | tr -d '\r')
if [ ! $version ]; then
echo "Failed while attempting to install $REPO. Please manually install:"
echo ""
echo "1. Open your web browser and go to https://github.com/$OWNER/$REPO/releases"
echo "2. Download the latest release for your platform. Call it '$REPO'."
echo "3. chmod +x ./$REPO"
echo "4. mv ./$REPO $BINLOCATION"
exit 1
fi
hasCli() {
hasCurl=$(which curl)
if [ "$?" = "1" ]; then
echo "You need curl to use this script."
exit 1
fi
}
getPackage() {
uname=$(uname)
userid=$(id -u)
suffix=""
case $uname in
"Darwin")
# suffix=".tar.gz"
arch=$(uname -m)
case $arch in
"x86_64")
suffix="Darwin_x86_64.tar.gz"
;;
esac
case $arch in
"aarch64")
suffix="Darwin_arm64.tar.gz"
;;
esac
;;
"Linux")
arch=$(uname -m)
echo $arch
case $arch in
"x86_64")
suffix="Linux_x86_64.tar.gz"
;;
esac
case $arch in
"i386")
suffix="Linux_i386.tar.gz"
;;
esac
case $arch in
"aarch64")
suffix="Linux_arm64.tar.gz"
;;
esac
case $arch in
"armv6l" | "armv7l")
suffix="Linux_armv6.tar.gz"
;;
esac
;;
esac
targetFile="/tmp/${REPO}_${suffix}"
if [ "$userid" != "0" ]; then
targetFile="$(pwd)/${REPO}_${suffix}"
fi
if [ -e "$targetFile" ]; then
rm "$targetFile"
fi
url="https://github.com/$OWNER/$REPO/releases/download/$version/${REPO}_${suffix}"
echo "Downloading package $url as $targetFile"
curl -sSLf $url --output "$targetFile"
if [ $? -ne 0 ]; then
echo "Download Failed!"
exit 1
else
extractFolder=$(pwd)
echo "Download Complete, extracting $targetFile to $extractFolder ..."
tar -xzf "$targetFile" -C "$extractFolder"
fi
if [ $? -ne 0 ]; then
echo "\nFailed to expand archve: $targetFile"
exit 1
else
# Remove the LICENSE and README
echo "OK"
rm "$(pwd)/LICENSE"
rm "$(pwd)/README.md"
# Get the parent dir of the 'bin' folder holding the binary
# targetFile=$(echo "$targetFile" | sed "s+/${REPO}${suffix}++g")
# suffix=$(echo $suffix | sed 's/.tgz//g')
# installFile="${targetFile}/${REPO}/harsh"
installFile="$(pwd)/harsh"
chmod +x "$installFile"
# Calculate SHA
# https://github.com/wakatara/harsh/releases/download/v0.8.12/checksums.txt
shaurl="https://github.com/$OWNER/$REPO/releases/download/$version/checksums.txt"
shacheck="$(curl -sSLf $shaurl | grep ${REPO}_${suffix})"
SHA256="$(echo $shacheck | awk '{print $1}')"
echo "SHA256 fetched from release: $SHA256"
# NOTE to other maintainers
# There needs to be two spaces between the SHA and the file in the echo statement
# for shasum to compare the checksums
echo "$SHA256 $targetFile" | shasum -a 256 -c -s
# Don't need the tar.gz any more so delete it
rm "$targetFile"
if [ $? -ne 0 ]; then
echo "SHA mismatch! This means there must be a problem with the download"
exit 1
else
if [ ! -w "$BINLOCATION" ]; then
echo
echo "============================================================"
echo " The script was run as a user who is unable to write"
echo " to $BINLOCATION. To complete the installation the"
echo " following commands may need to be run manually."
echo "============================================================"
echo
echo " sudo mv $installFile $BINLOCATION/$REPO"
echo
./${REPO} --version
else
echo
echo "SHA 256 integrity check on release ${version} succesful"
echo "Running with sufficient permissions to attempt to move $REPO to $BINLOCATION"
mv "$installFile" $BINLOCATION/$REPO
if [ "$?" = "0" ]; then
echo "New version of $REPO installed to $BINLOCATION"
fi
if [ -e "$installFile" ]; then
rm "$installFile"
fi
echo "Checking successful install: running 'harsh --version'"
${SUCCESS_CMD}
fi
fi
fi
}
hasCli
getPackage