Ctrl K
ComunidadGuías…

install.sh

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
#!/usr/bin/env bash
set -e

usage() {
    cat <<EOF
Usage: $0 [OPTIONS]

Options:
  -p, --pre-release  Download latest pre-release instead of stable
  -h, --help         Show this help message
EOF
    exit 0
}

PRE_RELEASE=false
while [[ $# -gt 0 ]]; do
    case "$1" in
        -p|--pre-release) PRE_RELEASE=true ;;
        -h|--help) usage ;;
        *) echo "Unknown option: $1"; usage ;;
    esac
    shift
done

echo "========================================"
echo "RSPlayer Installer"
echo "========================================"

# Use sudo only if not already root
if [ "$(id -u)" -eq 0 ]; then
    SUDO=""
else
    SUDO="sudo"
fi

device_arch=$(uname -m)
echo "[INFO] Device architecture: $device_arch"

# Map device architecture to package suffix per distribution
if [ "$device_arch" = "x86_64" ]; then
    deb_arch_suffix="amd64"
    rpm_arch_suffix="x86_64"
    arch_arch_suffix="x86_64"
elif [ "$device_arch" = "aarch64" ]; then
    deb_arch_suffix="arm64"
    rpm_arch_suffix="aarch64"
    arch_arch_suffix="aarch64"
elif [ "$device_arch" = "armv7l" ]; then
    deb_arch_suffix="armhfv7"
    rpm_arch_suffix="armv7hl"
    arch_arch_suffix="armv7h"
elif [ "$device_arch" = "armv6l" ]; then
    deb_arch_suffix="armhfv6"
    rpm_arch_suffix="armv6hl"
    arch_arch_suffix="armv6h"
elif [ "$device_arch" = "riscv64" ]; then
    deb_arch_suffix="riscv64"
    rpm_arch_suffix="riscv64"
    arch_arch_suffix="riscv64"
else
    deb_arch_suffix=$device_arch
    rpm_arch_suffix=$device_arch
    arch_arch_suffix=$device_arch
fi

echo "[INFO] Architecture suffixes: deb=$deb_arch_suffix rpm=$rpm_arch_suffix arch=$arch_arch_suffix"

# Detect distribution
echo "[INFO] Detecting distribution..."
if [ -f /etc/os-release ]; then
    . /etc/os-release
    echo "[INFO] OS: $PRETTY_NAME (ID=$ID)"
    case $ID in
        debian|ubuntu|raspbian)
            pkg_type="deb"
            pkg_suffix="$deb_arch_suffix"
            pkg_ext="deb"
            ;;
        fedora|rhel|centos|opensuse*)
            pkg_type="rpm"
            pkg_suffix="$rpm_arch_suffix"
            pkg_ext="rpm"
            ;;
        arch|archarm|manjaro)
            pkg_type="arch"
            pkg_suffix="$arch_arch_suffix"
            pkg_ext="pkg.tar.zst"
            ;;
        *)
            echo "[WARN] Unknown distribution '$ID', defaulting to deb"
            pkg_type="deb"
            pkg_suffix="$deb_arch_suffix"
            pkg_ext="deb"
            ;;
    esac
else
    echo "[WARN] /etc/os-release not found, assuming Debian-based"
    pkg_type="deb"
    pkg_suffix="$deb_arch_suffix"
    pkg_ext="deb"
fi

echo "[INFO] Selected package type: $pkg_type (suffix=$pkg_suffix, ext=$pkg_ext)"

REPO_URL="https://ljufa.github.io/rsplayer-pkg"

# Add the RSPlayer apt repo and install from it. Every step is chained so any
# failure returns non-zero and the caller falls back to a direct download.
setup_apt_repo() {
    echo "[INFO] Adding RSPlayer apt repository ($REPO_URL/deb)"
    curl -fsSL "$REPO_URL/rsplayer.gpg" -o /tmp/rsplayer-keyring.gpg &&
        $SUDO install -D -m 644 /tmp/rsplayer-keyring.gpg /usr/share/keyrings/rsplayer.gpg &&
        rm -f /tmp/rsplayer-keyring.gpg &&
        echo "deb [signed-by=/usr/share/keyrings/rsplayer.gpg] $REPO_URL/deb stable main" | $SUDO tee /etc/apt/sources.list.d/rsplayer.list >/dev/null &&
        $SUDO apt-get update &&
        $SUDO apt-get install -y rsplayer
}

setup_dnf_repo() {
    echo "[INFO] Adding RSPlayer dnf repository ($REPO_URL/rpm)"
    curl -fsSL "$REPO_URL/rpm/rsplayer.repo" -o /tmp/rsplayer.repo &&
        $SUDO install -m 644 /tmp/rsplayer.repo /etc/yum.repos.d/rsplayer.repo &&
        rm -f /tmp/rsplayer.repo &&
        $SUDO dnf install -y rsplayer
}

# Prefer the package repository: after the one-time setup, updates arrive via
# regular apt/dnf upgrade. Pre-release installs and Arch/unknown distros use
# the direct-download path below (also the fallback if the repo fails).
repo_install_done=false
if [ "$PRE_RELEASE" = false ]; then
    case $pkg_type in
        deb)
            if setup_apt_repo; then
                repo_install_done=true
            else
                echo "[WARN] Repo-based install failed, removing repo entry and falling back to direct package download"
                $SUDO rm -f /etc/apt/sources.list.d/rsplayer.list
            fi
            ;;
        rpm)
            if setup_dnf_repo; then
                repo_install_done=true
            else
                echo "[WARN] Repo-based install failed, removing repo entry and falling back to direct package download"
                $SUDO rm -f /etc/yum.repos.d/rsplayer.repo
            fi
            ;;
    esac
fi

# Function to attempt download
try_download() {
    local type=$1
    local suffix=$2
    local ext=$3
    local file="rsplayer_${suffix}.${ext}"
    # Server asset names: rsplayer_<ver>_<suffix>.deb/.rpm, Arch: rsplayer-bin-<ver>-<rel>-<carch>.pkg.tar.zst
    local pattern="/rsplayer_[^/]*_${suffix}\.${ext}$"
    if [ "$type" = "arch" ]; then
        pattern="/rsplayer-bin-[^/]*-${suffix}\.pkg\.tar\.zst$"
    fi

    if [ "$PRE_RELEASE" = true ]; then
        echo "[INFO] Querying GitHub API for latest pre-release..."
        URL=$(curl -s "https://api.github.com/repos/ljufa/rsplayer/releases?per_page=1" | grep browser_download_url | cut -d '"' -f 4 | grep -E "$pattern" | head -n 1)
    else
        echo "[INFO] Querying GitHub API for latest stable release..."
        URL=$(curl -s "https://api.github.com/repos/ljufa/rsplayer/releases/latest" | grep browser_download_url | cut -d '"' -f 4 | grep -E "$pattern" | head -n 1)
    fi
    if [ -z "$URL" ]; then
        echo "[WARN] No $type package found for suffix=$suffix ext=$ext"
        return 1
    fi
    echo "[INFO] Downloading: $URL"
    if curl -L -o "$file" "$URL"; then
        echo "[INFO] Download OK: $file ($(du -h "$file" | cut -f1))"
        chmod 644 "$file"
        pkg_type="$type"
        pkg_suffix="$suffix"
        pkg_ext="$ext"
        pkg_file_name="$file"
        return 0
    else
        echo "[ERROR] Download failed"
        rm -f "$file"
        return 1
    fi
}

# Sets overwrite_args to --overwrite for each file of package $1 that is already
# on disk without a pacman owner (left there by an earlier tarball install).
overwrite_unowned() {
    overwrite_args=()
    local path
    while read -r path; do
        if [ -f "$path" ] && ! pacman -Qo "$path" >/dev/null 2>&1; then
            overwrite_args+=(--overwrite "$path")
        fi
    done < <(pacman -Qlpq "$1")
}

# Direct-download install (pre-releases, Arch, unknown distros, repo fallback)
if [ "$repo_install_done" = false ]; then

# Try primary package type
echo "[INFO] Attempting primary package type: $pkg_type"
if ! try_download "$pkg_type" "$pkg_suffix" "$pkg_ext"; then
    if [ "$pkg_type" = "arch" ]; then
        echo "[ERROR] No Arch package found for $device_arch in this release."
        exit 1
    fi
    echo "[WARN] Primary package type $pkg_type not available, falling back to DEB"
    if ! try_download "deb" "$deb_arch_suffix" "deb"; then
        echo "[ERROR] No suitable package found for architecture $device_arch"
        exit 1
    fi
fi

echo "[INFO] Installing $pkg_type package: $pkg_file_name"
case $pkg_type in
    deb)
        echo "[INFO] Installing deb with apt-get (resolves dependencies automatically)"
        $SUDO apt-get update
        $SUDO apt-get install "./${pkg_file_name}" || {
            echo "[INFO] Retrying with dpkg + apt-get -f install to resolve dependencies"
            $SUDO dpkg -i --force-overwrite "./${pkg_file_name}" || true
            $SUDO apt-get install -f
        }
        ;;
    rpm)
        echo "[INFO] Installing rpm with dnf (resolves dependencies automatically)"
        $SUDO dnf install "./${pkg_file_name}"
        ;;
    arch)
        # An earlier installer version extracted a tarball to / instead. Remove
        # its unit and polkit rule (the package ships them under /usr), and let
        # pacman take over the other files it left behind.
        for f in /etc/systemd/system/rsplayer.service /etc/polkit-1/rules.d/99-rsplayer.rules; do
            # sudo: /etc/polkit-1/rules.d is not readable by regular users
            if $SUDO test -f "$f" && ! pacman -Qo "$f" >/dev/null 2>&1; then
                echo "[INFO] Removing $f from a previous tarball install"
                $SUDO rm -f "$f"
            fi
        done
        overwrite_unowned "./${pkg_file_name}"
        echo "[INFO] Installing with pacman (resolves dependencies, sets up the service)"
        $SUDO pacman -U "${overwrite_args[@]}" "./${pkg_file_name}"
        ;;
esac

rm "${pkg_file_name}"

fi # repo_install_done

# Ensure /opt/rsplayer is owned by rsplayer regardless of package type
echo "[INFO] Ensuring /opt/rsplayer ownership..."
if getent passwd rsplayer >/dev/null 2>&1; then
    $SUDO chown -R rsplayer /opt/rsplayer
    echo "[INFO] /opt/rsplayer owned by rsplayer:rsplayer"
else
    echo "[WARN] rsplayer user not found, skipping chown"
fi

# Work out the web UI address to show the user (port from the packaged env file)
ui_port=$(grep -s '^PORT=' /opt/rsplayer/env | cut -d= -f2 | tr -d '"' || true)
ui_port=${ui_port:-80}
ui_host=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i = 1; i < NF; i++) if ($i == "src") { print $(i + 1); exit }}' || true)
[ -n "$ui_host" ] || ui_host=$(hostname -I 2>/dev/null | awk '{print $1}' || true)
ui_host=${ui_host:-localhost}
if [ "$ui_port" = "80" ]; then
    ui_url="http://${ui_host}"
else
    ui_url="http://${ui_host}:${ui_port}"
fi

echo "========================================"
echo "[INFO] Installation complete!"
echo "[INFO] Package type: $pkg_type"
echo "[INFO] Architecture: $device_arch ($pkg_suffix)"
echo "========================================"
if systemctl is-active --quiet rsplayer 2>/dev/null; then
    echo "  RSPlayer is running. Open it in your browser:"
    echo ""
    echo "      $ui_url"
else
    echo "[WARN] The rsplayer service is not running yet. Start it with:"
    echo "      sudo systemctl start rsplayer"
    echo "  then open it in your browser:"
    echo ""
    echo "      $ui_url"
fi
echo ""
echo "  Useful commands:"
echo "      systemctl status rsplayer"
echo "      journalctl -u rsplayer -f -n 50"
echo "========================================"