Skip to content

Commit 81ec9a6

Browse files
committed
Update qi.bash
1 parent bfba14f commit 81ec9a6

1 file changed

Lines changed: 113 additions & 24 deletions

File tree

qi.bash

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ print_generic() { print_output "" "$1"; }
120120
print_build_info() {
121121
local json_data="$1"
122122
local libtorrent_ver="$2"
123-
print_info "Latest Build Information:"
123+
# Extra key-value pairs appended after JSON fields, passed as "key value" arguments
124+
shift 2
125+
local -a extra_fields=("$@")
126+
print_info "Install Configuration:"
124127

125128
local key_to_remove
126129
if [[ $libtorrent_ver == "v2" ]]; then
@@ -156,6 +159,16 @@ print_build_info() {
156159
printf '%b\n' "$COLOR_INFO $aligned_text $COLOR_RESET"
157160
done <<< "$(printf '%s' "$json_data")"
158161
fi
162+
163+
# Print any extra key-value pairs in the same aligned format
164+
local i
165+
for ((i = 0; i < ${#extra_fields[@]}; i += 2)); do
166+
local ekey="${extra_fields[i]}"
167+
local evalue="${extra_fields[i+1]}"
168+
local aligned_text
169+
aligned_text=$(printf "%-18s %s" "${ekey}:" "$evalue")
170+
printf '%b\n' "$COLOR_INFO $aligned_text $COLOR_RESET"
171+
done
159172
}
160173

161174
# Detect architecture and map to binary name
@@ -406,11 +419,9 @@ parse_release_info() {
406419

407420
if command -v jq > /dev/null 2>&1; then
408421
qbt_ver=$(printf '%s' "$response" | jq -r '.qbittorrent')
409-
revision=$(printf '%s' "$response" | jq -r '.revision // ""')
410422
libt_ver=$(printf '%s' "$response" | jq -r --arg KEY "$libt_key" '.[$KEY]')
411423
else
412424
qbt_ver=$(printf '%s' "$response" | sed -rn 's/.*"qbittorrent": "([^"]*)".*/\1/p')
413-
revision=$(printf '%s' "$response" | sed -rn 's/.*"revision": "([^"]*)".*/\1/p')
414425
libt_ver=$(printf '%s' "$response" | sed -rn "s/.*\"${libt_key}\": \"([^\"]*)\".*/\1/p")
415426
fi
416427

@@ -421,12 +432,61 @@ parse_release_info() {
421432
exit 1
422433
fi
423434

424-
if [[ -z $revision ]]; then
425-
print_warning "Failed to parse revision from API response"
435+
# Output release tag only; revision must be fetched from the tag-specific dep JSON
436+
printf '%s' "release-${qbt_ver}_v${libt_ver}"
437+
}
438+
439+
# Module-level variable: the service unit identified during the stop probe
440+
_qbt_active_service=""
441+
442+
# Probe for a known qbittorrent user service, stop it if running, restart after install.
443+
# Sets _qbt_active_service to the first unit that exists (active or not).
444+
# Probes: qbittorrent-nox.service qbt-nox.service qbt.service
445+
manage_user_service() {
446+
local action="$1" # stop | start
447+
local unit="${2:-}"
448+
449+
# systemctl --user requires a running user bus; skip if unavailable
450+
if ! command -v systemctl > /dev/null 2>&1; then
451+
if [[ $action == "stop" ]]; then
452+
print_info "Service check skipped: systemctl not available"
453+
fi
454+
return 1
426455
fi
427456

428-
# Output release tag and revision
429-
printf '%s %s' "release-${qbt_ver}_v${libt_ver}" "${revision:-}"
457+
if [[ $action == "stop" ]]; then
458+
local candidates=("qbittorrent-nox.service" "qbt-nox.service" "qbt.service")
459+
for svc in "${candidates[@]}"; do
460+
# Check existence via 'cat' - works for enabled, disabled, or stopped units
461+
if systemctl --user cat "$svc" > /dev/null 2>&1; then
462+
print_info "Found user service: $svc"
463+
_qbt_active_service="$svc"
464+
# Only stop it if currently running
465+
if systemctl --user is-active --quiet "$svc" 2> /dev/null; then
466+
print_action "Stopping user service: $svc"
467+
if systemctl --user stop "$svc" 2> /dev/null; then
468+
print_success "Stopped: $svc"
469+
else
470+
print_warning "Failed to stop $svc - continuing anyway"
471+
fi
472+
else
473+
print_info "Service $svc is not running - will restart after install"
474+
fi
475+
return 0
476+
fi
477+
done
478+
print_info "No qbittorrent user service found - skipping service management"
479+
return 1
480+
fi
481+
482+
if [[ $action == "start" ]] && [[ -n $unit ]]; then
483+
print_action "Restarting user service: $unit"
484+
if systemctl --user start "$unit" 2> /dev/null; then
485+
print_success "Restarted: $unit"
486+
else
487+
print_warning "Failed to restart $unit"
488+
fi
489+
fi
430490
}
431491

432492
# Download file
@@ -471,29 +531,46 @@ main() {
471531
local libtorrent_ver="${LIBTORRENT_VERSION:-v2}"
472532
local install_path="$HOME/bin/qbittorrent-nox"
473533

474-
# Fetch dependency info
534+
# Fetch latest dep JSON to build the correct release tag
475535
local dep_api_url="https://github.com/userdocs/qbittorrent-nox-static/releases/latest/download/dependency-version.json"
476536
local dep_json
477537
dep_json=$(fetch_url "$dep_api_url")
478538
if [[ -z $dep_json ]]; then
479539
handle_error 1 "fetch_url $dep_api_url" "Failed to fetch dependency information"
480540
fi
481541

482-
# Print build info for the user
483-
print_build_info "$dep_json" "$libtorrent_ver"
542+
# Build release tag from the latest dep JSON
543+
local release_tag
544+
release_tag=$(parse_release_info "$dep_json" "$libtorrent_ver")
484545

485-
# Get release and download
486-
local release_tag revision
487-
# Capture release tag and revision from parse_release_info
488-
read release_tag revision <<< "$(parse_release_info "$dep_json" "$libtorrent_ver")"
546+
# Fetch the dep JSON for that specific tag to get the correct revision and build info
547+
local tag_dep_url="https://github.com/userdocs/qbittorrent-nox-static/releases/download/${release_tag}/dependency-version.json"
548+
local tag_dep_json
549+
tag_dep_json=$(fetch_url "$tag_dep_url")
550+
if [[ -z $tag_dep_json ]]; then
551+
handle_error 1 "fetch_url $tag_dep_url" "Failed to fetch tag-specific dependency information"
552+
fi
553+
554+
# Get revision from the tag-specific JSON
555+
local revision
556+
if command -v jq > /dev/null 2>&1; then
557+
revision=$(printf '%s' "$tag_dep_json" | jq -r '.revision // ""')
558+
else
559+
revision=$(printf '%s' "$tag_dep_json" | sed -rn 's/.*"revision": "([^"]*)".*/\1/p')
560+
fi
489561

490-
print_info "Architecture: $arch"
491-
print_info "LibTorrent version: $libtorrent_ver"
492-
# Output revision if available
493-
if [[ -n $revision ]]; then
494-
print_info "Build revision: $revision"
562+
if [[ -z $revision ]]; then
563+
print_warning "Failed to parse revision from tag-specific dependency JSON"
495564
fi
496-
print_info "Attestation verification: $(check_gh_cli && printf '%s' "enabled" || printf '%s' "disabled (gh cli not found)")"
565+
566+
local attestation_status
567+
attestation_status=$(check_gh_cli && printf '%s' "enabled" || printf '%s' "disabled (gh cli not found)")
568+
569+
# Print merged install configuration table
570+
print_build_info "$tag_dep_json" "$libtorrent_ver" \
571+
"architecture" "$arch" \
572+
"libtorrent" "$libtorrent_ver" \
573+
"attestation" "$attestation_status"
497574

498575
# Prepare installation directory
499576
local install_dir="$HOME/bin"
@@ -507,6 +584,10 @@ main() {
507584
handle_error $? "mkdir -p $install_dir" "Failed to create installation directory"
508585
}
509586

587+
# Stop any running user service before replacing the binary
588+
_qbt_active_service=""
589+
manage_user_service stop || true
590+
510591
# Get release and download
511592
local url
512593
url=$(create_download_url "$arch" "$release_tag")
@@ -534,10 +615,7 @@ main() {
534615
test_exit_code=$?
535616

536617
if [[ $test_exit_code -eq 0 ]]; then
537-
local version_info
538-
version_info=$(printf '%s' "$test_output" | head -1)
539618
print_success "Binary test passed"
540-
print_info "Version: $version_info"
541619
else
542620
binary_failed=true
543621
print_warning "Binary test failed"
@@ -552,6 +630,15 @@ main() {
552630
fi
553631
fi
554632

633+
# Restart the service only if both integrity and binary tests passed
634+
if [[ -n $_qbt_active_service ]]; then
635+
if [[ $integrity_failed == false && $binary_failed == false ]]; then
636+
manage_user_service start "$_qbt_active_service"
637+
else
638+
print_warning "Skipping service restart due to failed verification"
639+
fi
640+
fi
641+
555642
# PATH check
556643
if [[ ":$PATH:" != *":$HOME/bin:"* ]]; then
557644
print_warning '$HOME/bin is not in your PATH'
@@ -571,7 +658,9 @@ main() {
571658
else
572659
print_success "Installation completed successfully!"
573660
fi
574-
print_info "Run with: qbittorrent-nox"
661+
if [[ -z $_qbt_active_service ]]; then
662+
print_info "Run with: qbittorrent-nox"
663+
fi
575664
}
576665

577666
# Simple argument parsing: loop through all args

0 commit comments

Comments
 (0)