#!/usr/bin/env bash set -euo pipefail # One-command installer for rTorrent + pyTorrent on Arch Linux. # Arch uses current repository packages by default. Source build is opt-in. if [[ "${EUID}" -ne 0 ]]; then echo "Run as root: sudo $0" >&2 exit 1 fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(cd "${SCRIPT_DIR}/../.." && pwd)" RTORRENT_USER="${RTORRENT_USER:-rtorrent}" RTORRENT_HOME="${RTORRENT_HOME:-/home/${RTORRENT_USER}}" RTORRENT_BASE_DIR="${RTORRENT_BASE_DIR:-/opt/rtorrent_build}" RTORRENT_SCGI_PORT="${RTORRENT_SCGI_PORT:-5000}" RTORRENT_TORRENT_PORT="${RTORRENT_TORRENT_PORT:-51300}" RTORRENT_REF="${RTORRENT_REF:-v0.16.11}" LIBTORRENT_REF="${LIBTORRENT_REF:-v0.16.11}" PYTORRENT_APP_DIR="${PYTORRENT_APP_DIR:-/opt/pytorrent}" PYTORRENT_PORT="${PYTORRENT_PORT:-8090}" PYTORRENT_BASE_URL="${PYTORRENT_BASE_URL:-http://127.0.0.1:${PYTORRENT_PORT}}" PYTORRENT_PROFILE_NAME="${PYTORRENT_PROFILE_NAME:-Local rTorrent}" PYTORRENT_API_TOKEN="${PYTORRENT_API_TOKEN:-}" PYTORRENT_SERVICE_NAME="${PYTORRENT_SERVICE_NAME:-pytorrent}" RTORRENT_SCGI_BACKEND="${RTORRENT_SCGI_BACKEND:-tcp}" RTORRENT_SCGI_SOCKET="${RTORRENT_SCGI_SOCKET:-/run/rtorrent/rtorrent.sock}" RTORRENT_SCGI_PROXY_LISTEN="${RTORRENT_SCGI_PROXY_LISTEN:-127.0.0.1:5050}" RTORRENT_SCGI_PROXY_TOKEN="${RTORRENT_SCGI_PROXY_TOKEN:-}" PYTORRENT_RTORRENT_SCGI_URL="${PYTORRENT_RTORRENT_SCGI_URL:-scgi://127.0.0.1:${RTORRENT_SCGI_PORT}}" RTORRENT_BUILD_FROM_SOURCE="${RTORRENT_BUILD_FROM_SOURCE:-0}" RTORRENT_FORCE_CONFIG="${RTORRENT_FORCE_CONFIG:-1}" RTORRENT_EXTRA_ARGS=() while [[ $# -gt 0 ]]; do case "$1" in --build-rtorrent|--build-from-source|--compile-rtorrent) RTORRENT_BUILD_FROM_SOURCE=1 shift ;; --with-xmlrpc-c) RTORRENT_BUILD_FROM_SOURCE=1 RTORRENT_EXTRA_ARGS+=(--with-xmlrpc-c) shift ;; --scgi-unix-socket) RTORRENT_SCGI_BACKEND=unix shift ;; --rtorrent-socket) RTORRENT_SCGI_BACKEND=unix RTORRENT_SCGI_SOCKET="$2" shift 2 ;; *) echo "Unknown option: $1" >&2 echo "Supported options: --build-rtorrent, --with-xmlrpc-c, --scgi-unix-socket, --rtorrent-socket PATH" >&2 exit 1 ;; esac done if [[ "${RTORRENT_WITH_XMLRPC_C:-0}" == "1" ]]; then RTORRENT_BUILD_FROM_SOURCE=1 RTORRENT_EXTRA_ARGS+=(--with-xmlrpc-c) fi if [[ "${RTORRENT_SCGI_BACKEND}" == "unix" ]]; then if [[ -z "${RTORRENT_SCGI_PROXY_TOKEN}" ]]; then RTORRENT_SCGI_PROXY_TOKEN="$(python - <<'PYTOKEN' import secrets print(secrets.token_urlsafe(32)) PYTOKEN )" fi PYTORRENT_RTORRENT_SCGI_URL="scgi://${RTORRENT_SCGI_PROXY_LISTEN}/proxy/${RTORRENT_SCGI_PROXY_TOKEN}" elif [[ "${RTORRENT_SCGI_BACKEND}" != "tcp" ]]; then echo "Invalid RTORRENT_SCGI_BACKEND: ${RTORRENT_SCGI_BACKEND}" >&2 exit 1 fi command -v pacman >/dev/null 2>&1 || { echo "pacman is required on Arch Linux." >&2; exit 1; } pacman -Sy --noconfirm --needed ca-certificates curl tar gzip sudo python python-pip git rsync rtorrent ensure_rtorrent_user() { if ! getent group "${RTORRENT_USER}" >/dev/null; then groupadd --system "${RTORRENT_USER}" fi if ! id "${RTORRENT_USER}" >/dev/null 2>&1; then local shell="/usr/bin/nologin" [[ -x "${shell}" ]] || shell="/sbin/nologin" useradd --system --gid "${RTORRENT_USER}" --home-dir "${RTORRENT_HOME}" --create-home --shell "${shell}" "${RTORRENT_USER}" fi mkdir -p "${RTORRENT_HOME}/downloads" "${RTORRENT_HOME}/.session" "${RTORRENT_HOME}/watch" chown -R "${RTORRENT_USER}:${RTORRENT_USER}" "${RTORRENT_HOME}" } write_rtorrent_config() { local config="${RTORRENT_HOME}/.rtorrent.rc" if [[ -f "${config}" && "${RTORRENT_FORCE_CONFIG}" != "1" ]]; then echo "Keeping existing config: ${config}" return fi cat > "${config}" < /etc/systemd/system/rtorrent@.service <