Files
sbp/install.sh
T
2026-11-01 06:51:56 +08:00

42 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
repo="${SBP_REPO:-typist/sbp}"
branch="${SBP_BRANCH:-main}"
app_dir="${SBP_APP_DIR:-$HOME/.local/share/sbp/app}"
bin_dir="${SBP_BIN:-$HOME/.local/bin}"
url="${SBP_BASE:-https://gitea.typist.cc}/${repo}/archive/${branch}.tar.gz"
command -v curl >/dev/null 2>&1 || { echo "error: curl is required" >&2; exit 1; }
command -v tar >/dev/null 2>&1 || { echo "error: tar is required" >&2; exit 1; }
command -v python3 >/dev/null 2>&1 || { echo "error: python3 is required" >&2; exit 1; }
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
curl -fsSL "$url" | tar -xz -C "$tmp"
src="$(find "$tmp" -mindepth 1 -maxdepth 1 -type d | head -n 1)"
[ -n "$src" ] && [ -f "$src/sbp" ] || { echo "error: unexpected archive layout from $url" >&2; exit 1; }
[ -f "$src/src/sbp/cli.py" ] || { echo "error: package files missing from $url" >&2; exit 1; }
mkdir -p "$(dirname "$app_dir")" "$bin_dir"
rm -rf "$app_dir.new"
mv "$src" "$app_dir.new"
chmod 755 "$app_dir.new/sbp"
if [ -e "$app_dir" ]; then
[ -f "$app_dir/src/sbp/cli.py" ] || { echo "error: refusing to replace $app_dir" >&2; exit 1; }
rm -rf "$app_dir"
fi
mv "$app_dir.new" "$app_dir"
ln -sfn "$app_dir/sbp" "$bin_dir/sbp"
echo "installed $bin_dir/sbp"
case ":$PATH:" in
*":$bin_dir:"*) ;;
*) echo "note: add $bin_dir to PATH, for example: export PATH=\"$bin_dir:\$PATH\"" ;;
esac
if [ "${1:-}" = "--run" ]; then
exec "$bin_dir/sbp" install "${@:2}"
fi