-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·196 lines (171 loc) · 6.26 KB
/
install.sh
File metadata and controls
executable file
·196 lines (171 loc) · 6.26 KB
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
#!/bin/bash
# Script to install Formlander from GitHub releases
# Run as: curl -fsSL https://formlander.com/install | sudo bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Configuration
GITHUB_REPO="karloscodes/formlander"
INSTALL_DIR="/usr/local/bin"
run_installer() {
# Verify running as root
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}Error: This script requires root privileges. Use 'sudo su' and then re-run the installation command.${NC}"
exit 1
fi
# Detect architecture
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH="amd64"
;;
aarch64|arm64)
ARCH="arm64"
;;
*)
echo -e "${RED}Unsupported architecture: $ARCH. Only amd64 and arm64 are supported.${NC}"
exit 1
;;
esac
BINARY_PATH="$INSTALL_DIR/formlander"
TEMP_FILE="/tmp/formlander-$ARCH"
# Install dependencies if needed
NEED_UPDATE=false
if ! command -v jq >/dev/null 2>&1; then
NEED_UPDATE=true
fi
if ! command -v file >/dev/null 2>&1; then
NEED_UPDATE=true
fi
if [ "$NEED_UPDATE" = true ]; then
apt-get update -qq > /dev/null 2>&1
fi
if ! command -v jq >/dev/null 2>&1; then
apt-get install -y -qq jq > /dev/null 2>&1 || {
echo -e "${RED}Error: Failed to install jq. This script requires jq to parse GitHub API responses.${NC}"
exit 1
}
fi
if ! command -v file >/dev/null 2>&1; then
apt-get install -y -qq file > /dev/null 2>&1 || {
echo -e "${RED}Error: Failed to install 'file'. Binary verification will be skipped.${NC}"
}
fi
# Fetch the latest release information
echo "Fetching latest release information..."
RELEASE_INFO=$(curl -fsSL "https://api.github.com/repos/$GITHUB_REPO/releases/latest")
# Check for rate limit or other API errors
if echo "$RELEASE_INFO" | grep -q "API rate limit exceeded"; then
echo -e "${RED}Error: GitHub API rate limit exceeded. Please try again later.${NC}"
exit 1
fi
if echo "$RELEASE_INFO" | grep -q "Not Found"; then
echo -e "${RED}Error: No releases found in $GITHUB_REPO.${NC}"
exit 1
fi
# Extract the latest version
LATEST_VERSION=$(echo "$RELEASE_INFO" | jq -r '.tag_name' | sed 's/^v//')
if [ -z "$LATEST_VERSION" ]; then
echo -e "${RED}Error: Could not determine latest version.${NC}"
exit 1
fi
echo "Latest version: $LATEST_VERSION"
# Look for the correct asset
ASSET_NAME="formlander-linux-$ARCH"
if ! echo "$RELEASE_INFO" | jq -r '.assets[].name' | grep -q "$ASSET_NAME"; then
echo -e "${RED}Error: No binary found for $ARCH in release v$LATEST_VERSION.${NC}"
echo "Available assets:"
echo "$RELEASE_INFO" | jq -r '.assets[].name'
exit 1
fi
# Construct download URLs
BINARY_URL="https://github.com/$GITHUB_REPO/releases/download/v$LATEST_VERSION/$ASSET_NAME"
CHECKSUMS_URL="https://github.com/$GITHUB_REPO/releases/download/v$LATEST_VERSION/checksums.txt"
echo "Download URL: $BINARY_URL"
# Download the binary
echo "Downloading Formlander v$LATEST_VERSION for $ARCH..."
curl -L --fail --progress-bar -o "$TEMP_FILE" "$BINARY_URL" || {
echo -e "${RED}Error: Failed to download binary.${NC}"
rm -f "$TEMP_FILE"
exit 1
}
# Verify the download
if [ ! -s "$TEMP_FILE" ]; then
echo -e "${RED}Error: Downloaded file is empty.${NC}"
rm -f "$TEMP_FILE"
exit 1
fi
# Verify SHA256 checksum
echo "Verifying SHA256 checksum..."
CHECKSUMS_FILE="/tmp/formlander-checksums.txt"
if curl -fsSL -o "$CHECKSUMS_FILE" "$CHECKSUMS_URL" 2>/dev/null; then
EXPECTED_HASH=$(grep "$ASSET_NAME" "$CHECKSUMS_FILE" | awk '{print $1}')
if [ -n "$EXPECTED_HASH" ]; then
ACTUAL_HASH=$(sha256sum "$TEMP_FILE" | awk '{print $1}')
if [ "$ACTUAL_HASH" != "$EXPECTED_HASH" ]; then
echo -e "${RED}Error: SHA256 checksum mismatch!${NC}"
echo " Expected: $EXPECTED_HASH"
echo " Got: $ACTUAL_HASH"
rm -f "$TEMP_FILE" "$CHECKSUMS_FILE"
exit 1
fi
echo -e "${GREEN}Checksum verified.${NC}"
else
echo "Warning: No checksum found for $ASSET_NAME, skipping verification."
fi
rm -f "$CHECKSUMS_FILE"
else
echo "Warning: Could not download checksums file, skipping verification."
fi
# Check file type
if command -v file >/dev/null 2>&1; then
FILE_TYPE=$(file -b "$TEMP_FILE" | cut -d',' -f1-2)
echo "Verifying file: $FILE_TYPE"
if ! echo "$FILE_TYPE" | grep -q "ELF"; then
echo -e "${RED}Error: Downloaded file is not a valid binary.${NC}"
rm -f "$TEMP_FILE"
exit 1
fi
fi
# Install the binary
echo "Installing to $BINARY_PATH..."
mv "$TEMP_FILE" "$BINARY_PATH" && chmod +x "$BINARY_PATH" || {
echo -e "${RED}Error: Failed to install binary.${NC}"
rm -f "$TEMP_FILE"
exit 1
}
# Run the installer interactively
echo -e "${GREEN}Running Formlander installer...${NC}"
if "$BINARY_PATH" install; then
echo -e "${GREEN}Installation complete!${NC}"
else
INSTALL_EXIT_CODE=$?
echo -e "${RED}Installation failed with exit code $INSTALL_EXIT_CODE.${NC}"
exit $INSTALL_EXIT_CODE
fi
}
# Handle piped execution (curl | sudo bash)
# When piped, stdin is not a TTY, so we need to re-exec with /dev/tty
if [ ! -t 0 ]; then
echo "Detected piped execution. Creating temporary installer for interactive mode..."
TEMP_SCRIPT=$(mktemp /tmp/formlander-install-XXXXXX.sh)
# Export the function and variables to a temp script
{
echo '#!/bin/bash'
echo "RED='$RED'"
echo "GREEN='$GREEN'"
echo "NC='$NC'"
echo "GITHUB_REPO='$GITHUB_REPO'"
echo "INSTALL_DIR='$INSTALL_DIR'"
declare -f run_installer
echo 'run_installer'
} > "$TEMP_SCRIPT"
chmod +x "$TEMP_SCRIPT"
bash "$TEMP_SCRIPT" < /dev/tty
EXIT_CODE=$?
rm -f "$TEMP_SCRIPT"
exit $EXIT_CODE
fi
# Run the installer
run_installer