Rewrite install_bsd.sh for POSIX sh compliance

This commit is contained in:
peoplemiau1
2026-05-29 08:27:23 +03:00
parent daf465368f
commit 80a6d574a3

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/bin/sh
set -Eeuo pipefail set -eu
TMPDIR="/tmp/dcr-install" TMPDIR="/tmp/dcr-install"
INSTALL_PATH="$HOME/.local/share/dcr" INSTALL_PATH="$HOME/.local/share/dcr"
@@ -16,14 +16,11 @@ BLUE='\033[0;34m'
NC='\033[0m' NC='\033[0m'
mkdir -p "$(dirname "$LOGFILE")" mkdir -p "$(dirname "$LOGFILE")"
exec > >(tee -a "$LOGFILE") 2>&1
log() { echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1"; } log() { printf "${BLUE}[%s]${NC} %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" | tee -a "$LOGFILE"; }
success() { echo -e "${GREEN}✔ $1${NC}"; } success() { printf "${GREEN}✔ %s${NC}\n" "$1" | tee -a "$LOGFILE"; }
warn() { echo -e "${YELLOW}⚠ $1${NC}"; } warn() { printf "${YELLOW}⚠ %s${NC}\n" "$1" | tee -a "$LOGFILE"; }
error() { echo -e "${RED}✖ $1${NC}"; } error() { printf "${RED}✖ %s${NC}\n" "$1" | tee -a "$LOGFILE" >&2; }
trap 'error "Error on line $LINENO"; exit 1' ERR
INSTALL_MODE="" INSTALL_MODE=""
CHANNEL="" CHANNEL=""
@@ -51,7 +48,7 @@ detect_target() {
check_common_dependencies() { check_common_dependencies() {
command -v curl >/dev/null 2>&1 || { error "curl is not installed"; exit 1; } command -v curl >/dev/null 2>&1 || { error "curl is not installed"; exit 1; }
if [[ "$CHANNEL" == "dev" ]]; then if [ "$CHANNEL" = "dev" ]; then
if ! command -v python3 >/dev/null 2>&1 && ! command -v jq >/dev/null 2>&1; then if ! command -v python3 >/dev/null 2>&1 && ! command -v jq >/dev/null 2>&1; then
error "For dev installations, either 'python3' or 'jq' must be installed to parse GitHub API response." error "For dev installations, either 'python3' or 'jq' must be installed to parse GitHub API response."
exit 1 exit 1
@@ -68,7 +65,8 @@ select_channel() {
echo "Choose channel:" echo "Choose channel:"
echo " 1) Latest stable release (default)" echo " 1) Latest stable release (default)"
echo " 2) Latest dev (pre-release)" echo " 2) Latest dev (pre-release)"
read -r -p "Enter 1 or 2 [1]: " choice printf "Enter 1 or 2 [1]: "
read -r choice
case "${choice:-1}" in case "${choice:-1}" in
1) CHANNEL="stable" ;; 1) CHANNEL="stable" ;;
@@ -81,7 +79,8 @@ select_install_mode() {
echo "Choose installation mode:" echo "Choose installation mode:"
echo " 1) Download prebuilt binary from GitHub Release (recommended)" echo " 1) Download prebuilt binary from GitHub Release (recommended)"
echo " 2) Build from git" echo " 2) Build from git"
read -r -p "Enter 1 or 2 [1]: " choice printf "Enter 1 or 2 [1]: "
read -r choice
case "${choice:-1}" in case "${choice:-1}" in
1) INSTALL_MODE="release" ;; 1) INSTALL_MODE="release" ;;
@@ -91,7 +90,7 @@ select_install_mode() {
} }
fetch_release_json() { fetch_release_json() {
if [[ "$CHANNEL" == "dev" ]]; then if [ "$CHANNEL" = "dev" ]; then
log "Looking for latest dev (pre-release)..." log "Looking for latest dev (pre-release)..."
local json result local json result
json="$(curl -fsSL "$GITHUB_API_ALL")" json="$(curl -fsSL "$GITHUB_API_ALL")"
@@ -110,7 +109,7 @@ EOF
)" )"
fi fi
if [[ -z "$result" || "$result" == "{}" || "$result" == "null" ]]; then if [ -z "$result" ] || [ "$result" = "{}" ] || [ "$result" = "null" ]; then
error "No dev (pre-release) found on GitHub" error "No dev (pre-release) found on GitHub"
exit 1 exit 1
fi fi
@@ -127,7 +126,7 @@ download_binary() {
tag="$(printf '%s\n' "$release_json" | \ tag="$(printf '%s\n' "$release_json" | \
sed -n 's/.*"tag_name": "\([^"]*\)".*/\1/p' | head -n1)" sed -n 's/.*"tag_name": "\([^"]*\)".*/\1/p' | head -n1)"
if [[ -z "$tag" ]]; then if [ -z "$tag" ]; then
error "Failed to determine release version" error "Failed to determine release version"
exit 1 exit 1
fi fi
@@ -140,7 +139,7 @@ download_binary() {
download_url="$(printf '%s\n' "$release_json" | \ download_url="$(printf '%s\n' "$release_json" | \
sed -n "s#.*\"browser_download_url\": \"\([^\"]*/${asset_name}\)\".*#\1#p" | head -n1)" sed -n "s#.*\"browser_download_url\": \"\([^\"]*/${asset_name}\)\".*#\1#p" | head -n1)"
if [[ -z "$download_url" ]]; then if [ -z "$download_url" ]; then
error "Asset ${asset_name} not found in release ${tag}" error "Asset ${asset_name} not found in release ${tag}"
exit 1 exit 1
fi fi
@@ -155,7 +154,7 @@ prepare_sources() {
log "Fetching sources..." log "Fetching sources..."
rm -rf "$TMPDIR" rm -rf "$TMPDIR"
git clone --depth 1 "$REPO_URL" "$TMPDIR" git clone --depth 1 "$REPO_URL" "$TMPDIR"
if [[ "$CHANNEL" == "dev" ]]; then if [ "$CHANNEL" = "dev" ]; then
git clone --depth 1 --branch dev "$REPO_URL" "$TMPDIR" 2>/dev/null || \ git clone --depth 1 --branch dev "$REPO_URL" "$TMPDIR" 2>/dev/null || \
git clone --depth 1 "$REPO_URL" "$TMPDIR" git clone --depth 1 "$REPO_URL" "$TMPDIR"
fi fi
@@ -204,7 +203,7 @@ main() {
select_install_mode select_install_mode
cleanup cleanup
if [[ "$INSTALL_MODE" == "build" ]]; then if [ "$INSTALL_MODE" = "build" ]; then
check_build_dependencies check_build_dependencies
prepare_sources prepare_sources
build_binary build_binary