This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·243 lines (220 loc) · 8.78 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·243 lines (220 loc) · 8.78 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
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
235
236
237
238
239
240
241
242
243
#!/usr/bin/env bash
# voipbin-install — initial installer
# Usage: curl -fsSL https://raw.githubusercontent.com/voipbin/install/main/install.sh | bash
# curl -fsSL .../install.sh | bash -s -- [--dir PATH] [--version TAG] [--auto-approve] [--dry-run]
set -euo pipefail
# ---------------------------------------------------------------------------
# Defaults
# ---------------------------------------------------------------------------
INSTALL_DIR="${HOME:-}/voipbin-install"
INSTALL_VERSION=""
AUTO_APPROVE=0
DRY_RUN=0
REPO_URL="https://github.com/voipbin/install.git"
GITHUB_API_URL="https://api.github.com/repos/voipbin/install/releases/latest"
# ---------------------------------------------------------------------------
# Argument parsing
# ---------------------------------------------------------------------------
while [[ $# -gt 0 ]]; do
case "$1" in
--dir)
[[ $# -ge 2 ]] || { echo "Error: --dir requires a PATH argument." >&2; exit 1; }
INSTALL_DIR="$2"; shift 2 ;;
--version)
[[ $# -ge 2 ]] || { echo "Error: --version requires a TAG argument." >&2; exit 1; }
INSTALL_VERSION="$2"; shift 2 ;;
--auto-approve)
AUTO_APPROVE=1; shift ;;
--dry-run)
DRY_RUN=1; shift ;;
-h|--help)
echo "Usage: install.sh [--dir PATH] [--version TAG] [--auto-approve] [--dry-run]"
echo ""
echo "Options:"
echo " --dir PATH Install directory (default: ~/voipbin-install)"
echo " --version TAG Install specific version, e.g. v0.0.2 (default: latest)"
echo " --auto-approve Skip confirmation prompt"
echo " --dry-run Show what would be done without making changes"
exit 0 ;;
*)
echo "Error: Unknown option: $1" >&2
echo "Run with --help for usage." >&2
exit 1 ;;
esac
done
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
info() { echo " $*"; }
ok() { echo "✓ $*"; }
warn() { echo "Warning: $*" >&2; }
die() { echo "Error: $*" >&2; exit 1; }
validate_tag() {
[[ "$1" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]
}
# Sort tags semantically (vX.Y.Z) and return the latest.
# Input: newline-separated list of tags.
latest_tag() {
sort -V | tail -n1
}
fetch_latest_version() {
local tag=""
# Try GitHub Releases API first
if command -v curl &>/dev/null; then
tag=$(curl -fsSL \
-H "Accept: application/vnd.github+json" \
"${GITHUB_API_RELEASE_URL:-$GITHUB_API_URL}" 2>/dev/null \
| grep '"tag_name"' \
| head -1 \
| sed 's/.*"tag_name": *"\([^"]*\)".*/\1/')
fi
if validate_tag "$tag" 2>/dev/null; then
echo "$tag"
return 0
fi
# Fallback: git ls-remote (uses full URL, no local repo needed)
if command -v git &>/dev/null; then
tag=$(git ls-remote --tags "${GITHUB_LS_REMOTE_URL:-$REPO_URL}" 2>/dev/null \
| awk '{print $2}' \
| grep '^refs/tags/v' \
| grep -v '\^{}' \
| sed 's|refs/tags/||' \
| grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' \
| latest_tag)
fi
if validate_tag "$tag" 2>/dev/null; then
echo "$tag"
return 0
fi
return 1
}
# ---------------------------------------------------------------------------
# [0] stdin / TTY detection
# ---------------------------------------------------------------------------
if [[ $DRY_RUN -eq 0 && $AUTO_APPROVE -eq 0 ]] && ! [ -t 0 ]; then
die "Non-interactive mode detected (stdin is not a TTY).
Re-run with --auto-approve to install without a prompt:
curl -fsSL .../install.sh | bash -s -- --auto-approve"
fi
# ---------------------------------------------------------------------------
# [1] --version argument validation
# ---------------------------------------------------------------------------
if [[ -n "$INSTALL_VERSION" ]]; then
if ! validate_tag "$INSTALL_VERSION"; then
die "Unexpected tag format: ${INSTALL_VERSION}. Expected vX.Y.Z."
fi
fi
# ---------------------------------------------------------------------------
# [2] $HOME validation
# ---------------------------------------------------------------------------
if [[ -z "${HOME:-}" ]] && [[ "$INSTALL_DIR" == "/voipbin-install" ]]; then
die "\$HOME is not set. Use --dir to specify an install path."
fi
# ---------------------------------------------------------------------------
# [3] Prerequisites
# ---------------------------------------------------------------------------
PREREQ_FAIL=0
check_cmd() {
local name="$1"
if ! command -v "$name" &>/dev/null; then
echo " ✗ $name — not found" >&2
PREREQ_FAIL=1
else
if [[ $DRY_RUN -eq 1 ]]; then
echo " [dry-run] Prerequisites: $name ✓"
fi
fi
}
check_cmd git
check_cmd python3
if ! python3 -m pip --version &>/dev/null 2>&1; then
echo " ✗ pip — not found (run: python3 -m ensurepip)" >&2
PREREQ_FAIL=1
fi
if [[ $PREREQ_FAIL -ne 0 ]]; then
die "Missing prerequisites. Install them and re-run."
fi
# bash version warning (non-fatal)
if [[ -n "${BASH_VERSION:-}" ]]; then
bash_major="${BASH_VERSION%%.*}"
if [[ "$bash_major" -lt 4 ]]; then
warn "bash ${BASH_VERSION} detected (bash >= 4 recommended). Continuing anyway."
fi
fi
# ---------------------------------------------------------------------------
# [4] Resolve version
# ---------------------------------------------------------------------------
if [[ -z "$INSTALL_VERSION" ]]; then
info "Fetching latest version..."
if ! INSTALL_VERSION=$(fetch_latest_version); then
die "Could not determine latest version. Check your network or use --version."
fi
info "Latest version: ${INSTALL_VERSION}"
fi
# ---------------------------------------------------------------------------
# dry-run: report and exit
# ---------------------------------------------------------------------------
if [[ $DRY_RUN -eq 1 ]]; then
echo ""
echo "[dry-run] Would install voipbin-install ${INSTALL_VERSION} to ${INSTALL_DIR}"
echo "[dry-run] git clone --depth 1 --branch ${INSTALL_VERSION} ${REPO_URL} ${INSTALL_DIR}"
echo "[dry-run] No changes made."
exit 0
fi
# ---------------------------------------------------------------------------
# [5] Install path check
# ---------------------------------------------------------------------------
if [[ -e "$INSTALL_DIR" ]]; then
die "Directory ${INSTALL_DIR} already exists.
To upgrade: cd ${INSTALL_DIR} && ./voipbin-install self-update
To reinstall: rm -rf ${INSTALL_DIR} and re-run this script."
fi
# ---------------------------------------------------------------------------
# [6] Confirmation prompt
# ---------------------------------------------------------------------------
if [[ $AUTO_APPROVE -eq 0 ]]; then
echo ""
read -r -p "Install voipbin-install ${INSTALL_VERSION} to ${INSTALL_DIR}? [y/N] " REPLY
case "$REPLY" in
[yY][eE][sS]|[yY]) ;;
*)
echo " Cancelled."
exit 0 ;;
esac
fi
echo ""
info "Installing voipbin-install ${INSTALL_VERSION} to ${INSTALL_DIR} ..."
# ---------------------------------------------------------------------------
# [7] git clone (with partial cleanup on failure)
# ---------------------------------------------------------------------------
if ! git clone --depth 1 --branch "$INSTALL_VERSION" "$REPO_URL" "$INSTALL_DIR"; then
# Cleanup partial clone
if [[ -e "$INSTALL_DIR" ]]; then
rm -rf "$INSTALL_DIR"
info "Cleaned up partial clone at ${INSTALL_DIR}."
fi
die "git clone failed. Check your network or verify the version tag exists: ${INSTALL_VERSION}"
fi
# ---------------------------------------------------------------------------
# [8] pip dependencies (warn-only)
# ---------------------------------------------------------------------------
if [[ -f "${INSTALL_DIR}/requirements.txt" ]]; then
info "Installing Python dependencies..."
if ! python3 -m pip install -r "${INSTALL_DIR}/requirements.txt" -q 2>&1; then
warn "pip install failed. You may need to install dependencies manually:"
warn " python3 -m pip install -r ${INSTALL_DIR}/requirements.txt"
fi
fi
# ---------------------------------------------------------------------------
# [9] Completion message
# ---------------------------------------------------------------------------
echo ""
ok "voipbin-install ${INSTALL_VERSION} installed to ${INSTALL_DIR}"
echo ""
echo "Next steps:"
echo " cd ${INSTALL_DIR}"
echo " ./voipbin-install init"
echo ""
echo "To use from anywhere, add to your shell profile (~/.bashrc or ~/.zshrc):"
echo " export PATH=\"${INSTALL_DIR}:\$PATH\""
echo ""