Refactor a few routines out of main()

This commit is contained in:
2026-07-30 20:26:55 -04:00
parent ec3be607a0
commit 93489d2330

View File

@@ -279,7 +279,7 @@ init() {
warn "Running as root user." warn "Running as root user."
ask_ok "Continue as root user (not recommended)?" || exit 1 ask_ok "Continue as root user (not recommended)?" || exit 1
elif [[ -n $SUDO_USER ]]; then elif [[ -n $SUDO_USER ]]; then
err "Sudo detected, installJRMC should not be run with sudo but attempting to continue." err "sudo detected, installJRMC should not be run with sudo but attempting to continue."
ask_ok "Continue as user $SUDO_USER (unsupported and may result in permission issues)?" || exit 1 ask_ok "Continue as user $SUDO_USER (unsupported and may result in permission issues)?" || exit 1
USER="${SUDO_USER:-$USER}" USER="${SUDO_USER:-$USER}"
fi fi
@@ -348,7 +348,7 @@ init() {
BUILD_SWITCH=1 BUILD_SWITCH=1
LOCAL_INSTALL_SWITCH=1 LOCAL_INSTALL_SWITCH=1
fi ;; fi ;;
*) err "Auto-detecting OS, this is unreliable and --compat may be required." *) warn "Auto-detecting OS, this is unreliable and --compat may be required."
for cmd in dnf yum apt-get pacman; do for cmd in dnf yum apt-get pacman; do
if command -v "$cmd" &>/dev/null; then if command -v "$cmd" &>/dev/null; then
case "$cmd" in case "$cmd" in
@@ -410,6 +410,13 @@ init() {
PKG_QUERY=(:) PKG_QUERY=(:)
;; ;;
esac esac
# Set source repo and build/install targets
MC_REPO="${MC_REPO_USER:-${UBUNTU_CODENAME:-${VERSION_CODENAME:-$MC_REPO_HARDCODE}}}" # user>host>hardcoded
BUILD_TARGET="${BUILD_TARGET:-$ID}"
CREATEREPO_TARGET="${CREATEREPO_TARGET:-$ID}"
info "$MC_REPO $MC_ARCH --> $BUILD_TARGET $ARCH"
} }
# @description Determines the latest MC version using several methods # @description Determines the latest MC version using several methods
@@ -476,6 +483,10 @@ set_mc_vars() {
;; ;;
esac esac
fi fi
debug "MC_VERSION_SOURCE=$MC_VERSION_SOURCE"
debug "MC_REPO=$MC_REPO"
debug "MC_VERSION=$MC_VERSION"
} }
# @description Installs a package using the system package manager # @description Installs a package using the system package manager
@@ -700,8 +711,8 @@ acquire_deb() {
# If deb file already exists and is >30MB, skip download # If deb file already exists and is >30MB, skip download
if [[ -f $MC_DEB ]]; then if [[ -f $MC_DEB ]]; then
if [[ $(stat -c%s "$MC_DEB") -lt 30000000 ]]; then if [[ $(stat -c%s "$MC_DEB") -lt 40000000 ]]; then
info "Removing existing DEB under 30MB: $MC_DEB" info "Removing existing DEB under 40MB: $MC_DEB"
execute rm -f "$MC_DEB" execute rm -f "$MC_DEB"
else else
info "Using existing DEB: $MC_DEB" info "Using existing DEB: $MC_DEB"
@@ -743,14 +754,15 @@ acquire_deb() {
done done
fi fi
# Return if the download was successful # Return if the download was successful and over 40MB
[[ -f $MC_DEB ]] [[ -f $MC_DEB && $(stat -c%s "$MC_DEB") -gt 40000000 ]]
} }
# @description Translates upstream DEB dependencies for several distros # @description Translates upstream DEB dependencies for several distros
translate_packages() { translate_packages() {
debug "${FUNCNAME[0]}()" "$*" debug "${FUNCNAME[0]}()" "$*"
local deb_file="$1" local deb_file="$1"
# Use namerefs to pass arrays
# shellcheck disable=SC2178 # shellcheck disable=SC2178
declare -n requires_arr="$2" recommends_arr="$3" declare -n requires_arr="$2" recommends_arr="$3"
local -i i local -i i
@@ -929,6 +941,8 @@ build_rpm() {
return 0 return 0
fi fi
[[ -d $OUTPUT_DIR/SPECS ]] || execute mkdir -p "$OUTPUT_DIR/SPECS"
# Exclude MC stub executable <= MC31 # Exclude MC stub executable <= MC31
if [[ $MC_MVERSION -le 31 ]]; then if [[ $MC_MVERSION -le 31 ]]; then
stub="" stub=""
@@ -1020,10 +1034,20 @@ build_rpm() {
fi fi
# Run rpmbuild and verify output RPM exists # Run rpmbuild and verify output RPM exists
execute "${build_prefix[@]}" "${rpmbuild_cmd[@]}" && [[ -f $MC_RPM ]] || return 1 if { execute "${build_prefix[@]}" "${rpmbuild_cmd[@]}" && [[ -f $MC_RPM ]]; }; then
ok "Built RPM: $MC_RPM"
else
err "Failed to build RPM."
# On build failure, remove the source DEB in case it is corrupted
if [[ -f $MC_DEB ]] && ask_ok "Remove source DEB?"; then
execute rm -f "$MC_DEB" || execute sudo rm -f "$MC_DEB"
fi
return 1
fi
# Optionally sign the built RPM with the configured key # Optionally sign the built RPM with the configured key
if ((SIGN_SWITCH)); then if ((SIGN_SWITCH)); then
info "Signing RPM with key '$SIGN_KEY' as user '$SIGN_USER'."
command -v rpmsign &>/dev/null || { err "rpmsign command missing (install rpm-sign/rpm-build)."; return 1; } command -v rpmsign &>/dev/null || { err "rpmsign command missing (install rpm-sign/rpm-build)."; return 1; }
command -v gpg &>/dev/null || { err "gpg command missing."; return 1; } command -v gpg &>/dev/null || { err "gpg command missing."; return 1; }
@@ -1057,17 +1081,17 @@ build_rpm() {
info "Signing RPM: $MC_RPM" info "Signing RPM: $MC_RPM"
debug "${sign_prefix[*]} ${sign_cmd[*]}" debug "${sign_prefix[*]} ${sign_cmd[*]}"
if ! sign_output=$("${sign_prefix[@]}" "${sign_cmd[@]}" 2>&1); then if sign_output=$("${sign_prefix[@]}" "${sign_cmd[@]}" 2>&1); then
ok "Signed RPM: $MC_RPM"
else
err "RPM signing failed." err "RPM signing failed."
[[ -n $sign_output ]] && echo "$sign_output" >&2 warn "For non-interactive service runs, ensure $SIGN_USER can access an unlocked GPG key."
err "For non-interactive service runs, ensure $SIGN_USER can access an unlocked GPG key."
return 1 return 1
fi fi
((DEBUG)) && [[ -n $sign_output ]] && echo "$sign_output" [[ -n $sign_output ]] && debug "$sign_output"
fi
return 0 return 0
fi
} }
# @description Creates the Arch PKGBUILD file for Media Center # @description Creates the Arch PKGBUILD file for Media Center
@@ -1098,6 +1122,7 @@ build_pkgbuild() {
} }
EOF EOF
unset requires_arr recommends_arr unset requires_arr recommends_arr
ok "Generated Arch PKGBUILD."
} }
# @description Installs Media Center via DEB package w/ optional compatability fixes # @description Installs Media Center via DEB package w/ optional compatability fixes
@@ -1981,26 +2006,12 @@ main() {
fi fi
# Parse input, set target MC version, and create associated variables # Parse input, set target MC version, and create associated variables
init "$@" init "$@" &&
# Set targets
BUILD_TARGET="${BUILD_TARGET:-$ID}"
CREATEREPO_TARGET="${CREATEREPO_TARGET:-$ID}"
MC_REPO="${MC_REPO_USER:-${UBUNTU_CODENAME:-${VERSION_CODENAME:-$MC_REPO_HARDCODE}}}" # user>host>hardcoded
info "MC source --> target: $MC_REPO $MC_ARCH --> $BUILD_TARGET $ARCH"
get_mc_version && get_mc_version &&
set_mc_vars set_mc_vars
debug "MC_VERSION_SOURCE=$MC_VERSION_SOURCE" # Uninstall and exit if only --uninstall is passed
debug "MC_REPO=$MC_REPO" if ((UNINSTALL_SWITCH)) && uninstall &&
debug "MC_VERSION=$MC_VERSION"
((UNINSTALL_SWITCH)) && uninstall
# Exit now if only --uninstall is passed
if ((UNINSTALL_SWITCH)) &&
! ((BUILD_SWITCH || CREATEREPO_SWITCH || REPO_INSTALL_SWITCH || LOCAL_INSTALL_SWITCH)) && ! ((BUILD_SWITCH || CREATEREPO_SWITCH || REPO_INSTALL_SWITCH || LOCAL_INSTALL_SWITCH)) &&
[[ ${#SERVICES[@]} -eq 0 && ${#CONTAINERS[@]} -eq 0 ]]; then [[ ${#SERVICES[@]} -eq 0 && ${#CONTAINERS[@]} -eq 0 ]]; then
exit 0 exit 0
@@ -2012,6 +2023,7 @@ main() {
install_external_repos install_external_repos
# Install MC repositories
case $ID in case $ID in
fedora|centos) fedora|centos)
local keyurl="https://repos.bryanroessler.com/jriver/RPM-GPG-KEY-jriver.asc" local keyurl="https://repos.bryanroessler.com/jriver/RPM-GPG-KEY-jriver.asc"
@@ -2084,9 +2096,7 @@ main() {
return 1 return 1
fi fi
# Clean up legacy repo after successful install
remove_legacy_repo remove_legacy_repo
link_ssl_certs link_ssl_certs
restore_license restore_license
open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp" open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
@@ -2101,26 +2111,9 @@ main() {
if [[ $BUILD_TARGET =~ centos|fedora|suse|mandriva || $CREATEREPO_TARGET =~ centos|fedora|suse|mandriva ]]; then if [[ $BUILD_TARGET =~ centos|fedora|suse|mandriva || $CREATEREPO_TARGET =~ centos|fedora|suse|mandriva ]]; then
install_package rpm-build install_package rpm-build
[[ -d $OUTPUT_DIR/SPECS ]] || execute mkdir -p "$OUTPUT_DIR/SPECS" build_rpm requires recommends || return
if build_rpm requires recommends; then
ok "Built RPM package."
else
err "Failed to build RPM package."
# On build failure, remove the source DEB in case it is corrupted
if [[ -f $MC_DEB ]]; then
info "Removing source DEB."
if ! execute rm -f "$MC_DEB"; then
execute sudo rm -f "$MC_DEB"
fi
fi
return 1
fi
elif [[ $BUILD_TARGET =~ arch ]]; then elif [[ $BUILD_TARGET =~ arch ]]; then
if build_pkgbuild requires recommends; then build_pkgbuild requires recommends
ok "Generated Arch PKGBUILD."
else
err "Failed to generate Arch PKGBUILD."
fi
fi fi
fi fi