installJRMC 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  1. #!/usr/bin/env bash
  2. # @file installJRMC
  3. # @brief Installs JRiver Media Center and associated services
  4. # @description See installJRMC --help or print_help() below for usage
  5. # Copyright (c) 2021-2025 Bryan C. Roessler
  6. # This software is released under the Apache License.
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # TODO (v2)
  10. # * Interactive (ncurses) mode
  11. # * Additional containerization (createrepo and rpmbuild)
  12. #
  13. # BUGS
  14. # * No createrepo on Mint
  15. #
  16. # NOTES
  17. # * Be careful with tabs in heredocs
  18. # * Avoid execute() for stdout
  19. #
  20. # We use indirection to match service names to their functions
  21. # shellcheck disable=SC2317
  22. shopt -s extglob
  23. declare -g SCRIPT_VERSION="1.34.0-dev"
  24. declare -g MC_VERSION="34.0.31" # do find all replace
  25. declare -g MC_REPO="bookworm" # should match the MC_VERSION
  26. declare -g BOARD_ID="89.0" # MC34
  27. declare -gi UPDATE_SWITCH=1 # set to 0 to disable automatic self-update
  28. declare -g SCRIPT_URL="https://git.bryanroessler.com/bryan/installJRMC/raw/master/installJRMC"
  29. # declare -g SCRIPT_URL="https://raw.githubusercontent.com/cryobry/installJRMC/refs/heads/master/installJRMC"
  30. declare -gi DEBUG=${DEBUG:-0} # set default debug and allow DEBUG env override (default: disabled)
  31. # @description Print help text
  32. print_help() {
  33. debug "${FUNCNAME[0]}()"
  34. cat <<-EOF
  35. SEE:
  36. README.md for more information
  37. USAGE:
  38. installJRMC [[OPTION] [VALUE]]...
  39. installJRMC defaults to --install=repo on platforms with a JRiver repository and --install=local on all others.
  40. Specifying --build, --createrepo, --service, or --uninstall disables the default install method.
  41. OPTIONS
  42. --install, -i repo|local
  43. repo: Install MC from repository, updates are handled by the system package manager
  44. local: Build and install MC locally from official source package
  45. --build[=suse|fedora|centos]
  46. Build RPM from source DEB but do not install
  47. Optionally, specify a target distro for cross-building (ex. --build=suse, note the '=')
  48. --compat
  49. Build/install MC locally without minimum dependency version requirements
  50. --mcversion VERSION
  51. Specify the MC version, ex. "$MC_VERSION" or "${MC_VERSION%%.*}" (default: latest release)
  52. --arch VERSION
  53. Specify the target MC architecture, ex. "amd64", "arm64", etc (default: host)
  54. --mcrepo REPO
  55. Specify the MC repository, ex. "bullseye", "bookworm", "noble", etc (default: auto)
  56. --outputdir PATH
  57. Generate reusable installJRMC output in this PATH (default: ./output)
  58. --restorefile MJR_FILE
  59. Restore file location for automatic license registration
  60. --betapass PASSWORD
  61. Enter beta team password for access to beta builds
  62. --service, -s SERVICE
  63. See SERVICES section below for a list of possible services to install
  64. --service-type user|system
  65. Starts services at boot (system) or at user login (user) (default: per service, see SERVICES)
  66. --container, -c CONTAINER (TODO: Under construction)
  67. See CONTAINERS section below for a list of possible services to install
  68. --createrepo[=suse|fedora|centos]
  69. Build rpm, copy to webroot, and run createrepo.
  70. Use in conjunction with --build=TARGET for crossbuilding repos
  71. Optionally, specify a target distro for non-native repo (ex. --createrepo=fedora, note the '=')
  72. --createrepo-webroot PATH
  73. Specify the webroot directory to install the repo (default: /var/www/jriver)
  74. --createrepo-user USER
  75. Specify the web server user if it differs from \$USER
  76. --no-update
  77. Disable automatic installJRMCself-update
  78. --uninstall, -u
  79. Uninstall JRiver MC, remove services, containers, and firewall rules (does not remove library files)
  80. --yes, -y, --auto
  81. Assume yes response to questions
  82. --version, -v
  83. Print installJRMC version and exit
  84. --debug, -d
  85. Print debug output
  86. --help, -h
  87. Print help dialog and exit
  88. SERVICES
  89. jriver-mediaserver (default --service-type=user)
  90. Enable and start a mediaserver systemd service (requires an existing X server)
  91. jriver-mediacenter (user)
  92. Enable and start a mediacenter systemd service (requires an existing X server)
  93. jriver-x11vnc (user)
  94. Enable and start x11vnc for the local desktop (requires an existing X server)
  95. Usually combined with jriver-mediaserver or jriver-mediacenter services
  96. --vncpass and --display are optional (see below)
  97. jriver-xvnc (system)
  98. Enable and start a new Xvnc session running JRiver Media Center
  99. --vncpass PASSWORD
  100. Set the vnc password for x11vnc/Xvnc access. If no password is set, installJRMC
  101. will either use existing password stored in \$HOME/.vnc/jrmc_passwd or else no password
  102. --display DISPLAY
  103. Display to use for x11vnc/Xvnc (default: The current display (x11vnc) or the
  104. current display incremented by 1 (Xvnc))
  105. jriver-createrepo (system)
  106. Install hourly service to build latest MC RPM and run createrepo
  107. EOF
  108. }
  109. # @description Parses user input and sets sensible defaults
  110. # @arg $@ User input
  111. parse_input() {
  112. debug "${FUNCNAME[0]}()" "$@"
  113. declare -gi BUILD_SWITCH REPO_INSTALL_SWITCH LOCAL_INSTALL_SWITCH \
  114. CONTAINER_INSTALL_SWITCH CREATEREPO_SWITCH SNAP_INSTALL_SWITCH \
  115. APPIMAGE_INSTALL_SWITCH COMPAT_SWITCH UNINSTALL_SWITCH YES_SWITCH DEBUG=0
  116. declare -g USER_MC_VERSION USER_MC_MVERSION USER_MC_RELEASE USER_MC_REPO USER_ARCH MJR_FILE \
  117. BETAPASS SERVICE_TYPE VNCPASS USER_DISPLAY BUILD_TARGET CREATEREPO_TARGET
  118. local long_opts short_opts input
  119. long_opts="install:,build::,outputdir:,mcversion:,arch:,mcrepo:,compat,"
  120. long_opts+="restorefile:,betapass:,"
  121. long_opts+="service-type:,service:,services:,"
  122. long_opts+="version,debug,verbose,help,uninstall,yes,auto,no-update,"
  123. long_opts+="createrepo::,createrepo-webroot:,createrepo-user:,"
  124. long_opts+="vncpass:,display:,container:"
  125. short_opts="+i:b::s:c:uyvdh"
  126. if input=$(getopt -o $short_opts -l $long_opts -- "$@"); then
  127. eval set -- "$input"
  128. while true; do
  129. case $1 in
  130. --install|-i) shift;
  131. case $1 in
  132. local|rpm|deb) BUILD_SWITCH=1 LOCAL_INSTALL_SWITCH=1 ;;
  133. repo|remote) REPO_INSTALL_SWITCH=1 ;;
  134. container) CONTAINER_INSTALL_SWITCH=1 ;;
  135. snap) SNAP_INSTALL_SWITCH=1 ;;
  136. appimage) APPIMAGE_INSTALL_SWITCH=1 ;;
  137. *) err "Invalid --install option passed"; exit 1 ;;
  138. esac
  139. ;;
  140. --build|-b) BUILD_SWITCH=1; shift; BUILD_TARGET="$1" ;;
  141. --outputdir) shift; OUTPUT_DIR="$1" ;;
  142. --mcversion) shift;
  143. if [[ $1 =~ ^([0-9]+)(\.[0-9]+\.[0-9]+)?(-([0-9]+))?$ ]]; then
  144. # Major version is required
  145. USER_MC_MVERSION="${BASH_REMATCH[1]}"
  146. # Set default release to 1 if not provided
  147. USER_MC_RELEASE="${BASH_REMATCH[4]:-1}"
  148. # If we get the full version, use it
  149. [[ -n ${BASH_REMATCH[2]} ]] && USER_MC_VERSION="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
  150. # Set major version defaults
  151. case "$USER_MC_MVERSION" in
  152. 34) MC_VERSION="${USER_MC_VERSION:-$MC_VERSION}" MC_REPO="bookworm" BOARD_ID="89.0" ;;
  153. 33) MC_VERSION="${USER_MC_VERSION:-33.0.72}" MC_REPO="bullseye" BOARD_ID="86.0" ;;
  154. 32) MC_VERSION="${USER_MC_VERSION:-32.0.58}" MC_REPO="bullseye" BOARD_ID="83.0" ;;
  155. 31) MC_VERSION="${USER_MC_VERSION:-31.0.83}" MC_REPO="bullseye" BOARD_ID="80.0" ;;
  156. 30) MC_VERSION="${USER_MC_VERSION:-30.0.96}" MC_REPO="buster" BOARD_ID="76.0" ;;
  157. 29) MC_VERSION="${USER_MC_VERSION:-29.0.91}" MC_REPO="buster" BOARD_ID="74.0" ;;
  158. 28) MC_VERSION="${USER_MC_VERSION:-28.0.110}" MC_REPO="buster" BOARD_ID="71.0" ;;
  159. 27) MC_VERSION="${USER_MC_VERSION:-27.0.88}" MC_REPO="buster" BOARD_ID="67.0" ;;
  160. 26) MC_VERSION="${USER_MC_VERSION:-26.0.107}" MC_REPO="jessie" BOARD_ID="64.0" ;;
  161. 25) MC_VERSION="${USER_MC_VERSION:-25.0.114}" MC_REPO="jessie" BOARD_ID="62.0" ;;
  162. 24) MC_VERSION="${USER_MC_VERSION:-24.0.78}" MC_REPO="jessie" BOARD_ID="58.0" ;;
  163. 23) MC_VERSION="${USER_MC_VERSION:-23.0.104}" MC_REPO="jessie" BOARD_ID="54.0" ;;
  164. 22) MC_VERSION="${USER_MC_VERSION:-22.0.102}" MC_REPO="jessie" BOARD_ID="51.0" ;;
  165. 21) MC_VERSION="${USER_MC_VERSION:-21.0.90}" MC_REPO="jessie" BOARD_ID="44.0" ;;
  166. 20) MC_VERSION="${USER_MC_VERSION:-20.0.131}" MC_REPO="jessie" BOARD_ID="35.0";;
  167. *) err "Bad --mcversion"; print_help; exit 1 ;;
  168. esac
  169. else
  170. err "Bad --mcversion"; print_help; exit 1
  171. fi
  172. ;;
  173. --arch) shift; USER_ARCH="$1" ;;
  174. --mcrepo) shift; USER_MC_REPO="$1" ;;
  175. --restorefile) shift; MJR_FILE="$1"; [[ -f $MJR_FILE ]] || err "Specified license $MJR_FILE missing." ;;
  176. --betapass) shift; BETAPASS="$1" ;;
  177. --service-type) shift; SERVICE_TYPE="$1" ;;
  178. --service|-s|--services) shift; SERVICES+=("$1") ;;
  179. --createrepo) shift; CREATEREPO_TARGET="$1"; BUILD_TARGET="$1"
  180. BUILD_SWITCH=1; CREATEREPO_SWITCH=1 ;;
  181. --createrepo-webroot) shift; CREATEREPO_WEBROOT="$1" ;;
  182. --createrepo-user) shift; CREATEREPO_USER="$1" ;;
  183. --vncpass) shift; VNCPASS="$1" ;;
  184. --display) shift; USER_DISPLAY="$1" ;;
  185. --compat) COMPAT_SWITCH=1; BUILD_SWITCH=1 ;;
  186. --no-update) UPDATE_SWITCH=0 ;;
  187. --container|-c) shift; CONTAINERS+=("$1") ;;
  188. --yes|-y|--auto) YES_SWITCH=1 ;;
  189. --version|-v) echo "Version: $SCRIPT_VERSION"; exit 0 ;;
  190. --debug|-d|--verbose) DEBUG=1 ;;
  191. --help|-h) print_help; exit 0 ;;
  192. --uninstall|-u) UNINSTALL_SWITCH=1 ;;
  193. --) shift; break ;;
  194. esac
  195. shift
  196. done
  197. else
  198. err "Incorrect option provided, see installJRMC --help"; exit 1
  199. fi
  200. # Fallback to default install method in some scenarios
  201. if ! ((UNINSTALL_SWITCH || BUILD_SWITCH || CREATEREPO_SWITCH || LOCAL_INSTALL_SWITCH
  202. || CONTAINER_INSTALL_SWITCH || SNAP_INSTALL_SWITCH || APPIMAGE_INSTALL_SWITCH)) &&
  203. [[ ${#SERVICES[@]} -eq 0 && ${#CONTAINERS[@]} -eq 0 ]]; then
  204. debug "Defaulting to --install=repo"
  205. REPO_INSTALL_SWITCH=1
  206. fi
  207. if [[ -n $BETA_PASS ]] && ((REPO_INSTALL_SWITCH)); then
  208. echo "Warning: not all repositories have beta channels"
  209. echo "If the MC package is unavailable, try using --mcrepo to select another repository"
  210. fi
  211. # if [[ -n $CONTAINER_INSTALL_SWITCH ]] && ((LOCAL_INSTALL_SWITCH || REPO_INSTALL_SWITCH)); then
  212. # err "Some --install methods are incompatible"
  213. # fi
  214. }
  215. # @description Perform OS detection and generate OS-specific functions
  216. # @see parse_input
  217. init() {
  218. debug "${FUNCNAME[0]}()"
  219. declare -g USER
  220. declare -g SCRIPT_PATH; SCRIPT_PATH=$(readlink -f "${BASH_SOURCE[0]}")
  221. declare -g SCRIPT_DIR; SCRIPT_DIR=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
  222. declare -g OUTPUT_DIR="$SCRIPT_DIR/output"
  223. declare -g CREATEREPO_WEBROOT="/var/www/jriver"
  224. declare -g CREATEREPO_USER="$USER" # can be root
  225. declare -g ID VERSION_ID ARCH MC_ARCH NAME
  226. declare -g MC_MVERSION MC_RELEASE MC_PKG MC_RPM MC_ROOT
  227. declare -ga PKG_INSTALL PKG_REMOVE PKG_UPDATE PKG_QUERY
  228. declare -ga SERVICES CONTAINERS
  229. parse_input "$@"
  230. # Try to save users from themselves
  231. if [[ $EUID -eq 0 ]]; then
  232. err "Running as root but attempting to continue"
  233. ask_ok "Continue as root user (not recommended)?" || exit 1
  234. elif [[ -n $SUDO_USER ]]; then
  235. err "Sudo detected, installJRMC should not be run with sudo but attempting to continue"
  236. ask_ok "Continue as user $SUDO_USER (unsupported and may result in permission issues)?" || exit 1
  237. USER="${SUDO_USER:-$USER}"
  238. fi
  239. # Run the self-updater if enabled
  240. ((UPDATE_SWITCH)) && update "$@"
  241. # Get host information
  242. [[ -f /etc/os-release ]] && source /etc/os-release
  243. # Detect host architecture and translate to MC convention
  244. if ARCH=$(uname -m); then
  245. case $ARCH in
  246. x86_64) MC_ARCH="amd64" ;;
  247. aarch64) MC_ARCH="arm64" ;;
  248. *) MC_ARCH="$ARCH" ;;
  249. esac
  250. else
  251. ARCH="x86_64"
  252. MC_ARCH="amd64"
  253. err "Failed to detect host arch, using default: $ARCH"
  254. fi
  255. echo "Host: $ID $VERSION_ID $ARCH"
  256. # Parse user-provided architecture, allow either convention
  257. if [[ -n $USER_ARCH ]]; then
  258. case $USER_ARCH in
  259. x86_64|amd64) ARCH="x86_64"; MC_ARCH="amd64" ;;
  260. aarch64|arm64) ARCH="aarch64"; MC_ARCH="arm64" ;;
  261. *) ARCH="$USER_ARCH" ;;
  262. esac
  263. fi
  264. # Normalize ID and set host-specific vars
  265. case $ID in
  266. debian|fedora|centos) ;;
  267. rhel|almalinux) ID="centos" ;;
  268. linuxmint|neon|zorin|*ubuntu*) ID="ubuntu" ;;
  269. raspbian) ID="debian" ;;
  270. manjaro|arch) ID="arch"
  271. if ((REPO_INSTALL_SWITCH)); then
  272. debug "Automatically using --install=local for Arch"
  273. REPO_INSTALL_SWITCH=0
  274. BUILD_SWITCH=1
  275. LOCAL_INSTALL_SWITCH=1
  276. fi ;;
  277. *suse*) ID="suse"
  278. if ((REPO_INSTALL_SWITCH)); then
  279. debug "Automatically using --install=local for SUSE"
  280. REPO_INSTALL_SWITCH=0
  281. BUILD_SWITCH=1
  282. LOCAL_INSTALL_SWITCH=1
  283. fi ;;
  284. *) err "Auto-detecting distro, this is unreliable and --compat may be required"
  285. for cmd in dnf yum apt-get pacman; do
  286. if command -v "$cmd" &>/dev/null; then
  287. case "$cmd" in
  288. dnf) ID="fedora" ;;
  289. yum) ID="centos"; COMPAT_SWITCH=1 ;;
  290. apt-get) ID="ubuntu" ;;
  291. pacman) ID="arch" ;;
  292. esac
  293. break
  294. fi
  295. done
  296. if [[ -z $ID ]]; then
  297. err "OS detection failed!"
  298. if ask_ok "Continue with manual installation?"; then
  299. debug "Automatically using --install=local for unknown distro"
  300. ID="unknown"
  301. REPO_INSTALL_SWITCH=0
  302. BUILD_SWITCH=1
  303. LOCAL_INSTALL_SWITCH=1
  304. else
  305. exit 1
  306. fi
  307. fi ;;
  308. esac
  309. # Set distro-specific package manager commands for normalized IDs
  310. case $ID in
  311. fedora|centos)
  312. local rpm_mgr
  313. rpm_mgr=$(command -v dnf &>/dev/null && echo "dnf" || echo "yum")
  314. PKG_INSTALL=(sudo "$rpm_mgr" install -y)
  315. PKG_REMOVE=(sudo "$rpm_mgr" remove -y)
  316. PKG_UPDATE=(sudo "$rpm_mgr" makecache)
  317. PKG_QUERY=(rpm -q)
  318. PKG_INSTALL_LOCAL() { install_mc_rpm; }
  319. ;;
  320. debian|ubuntu)
  321. PKG_INSTALL=(sudo apt-get -f install --install-recommends -y -q0)
  322. PKG_REMOVE=(sudo apt-get remove --auto-remove -y -q0)
  323. PKG_UPDATE=(sudo apt-get update -y -q0)
  324. PKG_QUERY=(dpkg -s)
  325. PKG_INSTALL_LOCAL() { install_mc_deb "$@"; }
  326. ;;
  327. suse)
  328. PKG_INSTALL=(sudo zypper --gpg-auto-import-keys --non-interactive --quiet install --force --force-resolution --replacefiles --no-confirm)
  329. PKG_REMOVE=(sudo zypper --non-interactive --quiet remove --clean-deps)
  330. PKG_UPDATE=(sudo zypper --non-interactive --quiet refresh jriver)
  331. PKG_QUERY=(rpm -q)
  332. PKG_INSTALL_LOCAL() { install_mc_rpm; }
  333. ;;
  334. arch)
  335. PKG_INSTALL=(sudo pacman -Sy --noconfirm)
  336. PKG_REMOVE=(sudo pacman -Rs --noconfirm)
  337. PKG_UPDATE=(sudo pacman -Syy)
  338. PKG_QUERY=(sudo pacman -Qs)
  339. PKG_INSTALL_LOCAL() { install_mc_arch; }
  340. ;;
  341. unknown)
  342. PKG_INSTALL=(:)
  343. PKG_REMOVE=(:)
  344. PKG_UPDATE=(:)
  345. PKG_QUERY=(:)
  346. PKG_INSTALL_LOCAL() { install_mc_generic; }
  347. ;;
  348. esac
  349. # Set default targets
  350. BUILD_TARGET="${BUILD_TARGET:-$ID}"
  351. CREATEREPO_TARGET="${CREATEREPO_TARGET:-$ID}"
  352. # Repo selection
  353. # Match repo to the host on MC31+ unless overriden by user
  354. if [[ $ID =~ debian|ubuntu && "${USER_MC_MVERSION:-${MC_VERSION%%.*}}" -ge 31 ]]; then
  355. MC_REPO=${UBUNTU_CODENAME:-${VERSION_CODENAME:-$MC_REPO}}
  356. fi
  357. MC_REPO="${USER_MC_REPO:-$MC_REPO}" # allow user override
  358. echo "MC source -> target: $MC_REPO $MC_ARCH -> $BUILD_TARGET $ARCH"
  359. # Retrieves the latest MC version number if we need it
  360. if ((BUILD_SWITCH || LOCAL_INSTALL_SWITCH || CREATEREPO_SWITCH)); then
  361. get_latest_mc_version
  362. fi
  363. # Set MC version variables
  364. MC_VERSION="${USER_MC_VERSION:-$MC_VERSION}"
  365. MC_RELEASE="${USER_MC_RELEASE:-1}"
  366. MC_MVERSION="${USER_MC_MVERSION:-${MC_VERSION%%.*}}"
  367. MC_PKG="mediacenter$MC_MVERSION"
  368. MC_RPM="$OUTPUT_DIR/RPMS/$ARCH/mediacenter$MC_MVERSION-$MC_VERSION-$MC_RELEASE.$ARCH.rpm"
  369. MC_ROOT="/usr/lib/jriver/Media Center $MC_MVERSION"
  370. # Generate explicit package name
  371. if [[ -n $USER_MC_VERSION ]]; then
  372. # Append explicit package version when user provides --mcversion
  373. case $ID in
  374. fedora|centos|suse) MC_PKG+="-$MC_VERSION" ;;
  375. debian|ubuntu) MC_PKG+="=$MC_VERSION" ;;
  376. esac
  377. fi
  378. }
  379. # @description Determines the latest JRiver MC version using several methods
  380. get_latest_mc_version() {
  381. debug "${FUNCNAME[0]}()"
  382. local mc_version_source
  383. # User --mcversion
  384. if [[ -n $USER_MC_VERSION ]]; then
  385. mc_version_source="user input"
  386. # Containerized package manager
  387. elif create_mc_apt_container &&
  388. MC_VERSION=$(sudo buildah run "$CNT" -- apt-cache policy "mediacenter${USER_MC_MVERSION:-${MC_VERSION%%.*}}" | awk '/Candidate:/ {sub(/-.*/, "", $2); print $2}' | sort -V | tail -n1) &&
  389. execute sudo buildah rm "$CNT" &&
  390. [[ $MC_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  391. mc_version_source="containerized package manager"
  392. # Fallback to webscrape
  393. elif MC_VERSION=$(download "https://yabb.jriver.com/interact/index.php/board,$BOARD_ID.html" "-" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1) &&
  394. [[ $MC_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  395. mc_version_source="webscrape"
  396. # Fallback to hardcoded value
  397. else
  398. mc_version_source="hardcoded"
  399. err "Warning! Using hardcoded version number"
  400. fi
  401. echo "Determined MC version $MC_VERSION from the $MC_REPO repo (via $mc_version_source)"
  402. }
  403. # @description Installs a package using the system package manager
  404. # @arg $1 array One or more package names
  405. # @option --no-install-check Do not check if package is already installed
  406. # @option --no-gpg-check Disable GPG checks for RPM based distros
  407. # @option --allow-downgrades Useful for installing specific MC versions
  408. # @option --silent | -s Do not print errors (useful for optional packages)
  409. install_package() {
  410. debug "${FUNCNAME[0]}()" "$@"
  411. local -a pkg_array install_flags
  412. local -A pkg_aliases
  413. local input pkg _pkg
  414. local -i no_install_check=0 allow_downgrades=0 silent=0 refresh=0 no_gpg_check=0 reinstall=0
  415. local long_opts="no-install-check,allow-downgrades,no-gpg-check,refresh,reinstall,silent"
  416. input=$(getopt -o +s -l "$long_opts" -- "$@") || { err "Incorrect options provided"; exit 1; }
  417. eval set -- "$input"
  418. while true; do
  419. case $1 in
  420. --no-install-check) no_install_check=1 ;;
  421. --allow-downgrades) allow_downgrades=1 ;;
  422. --no-gpg-check) no_gpg_check=1 ;;
  423. --refresh) refresh=1 ;;
  424. --reinstall) reinstall=1 ;;
  425. --silent|-s) silent=1 ;;
  426. --) shift; break ;;
  427. esac
  428. shift
  429. done
  430. # Define package aliases based on the distribution
  431. case $ID in
  432. debian|ubuntu)
  433. pkg_aliases=(
  434. [rpm-build]="rpm"
  435. [createrepo_c]="createrepo"
  436. [tigervnc-server]="tigervnc-standalone-server"
  437. ) ;;
  438. suse)
  439. pkg_aliases=(
  440. [buildah]="buildah fuse-overlayfs"
  441. ) ;;
  442. esac
  443. # Filter out already installed packages to create pkg_array
  444. for pkg in "$@"; do
  445. # Use alias if present, otherwise just pkg itself
  446. pkg_names=("$pkg")
  447. if [[ -v pkg_aliases[$pkg] ]]; then
  448. debug "Aliasing $pkg to ${pkg_aliases[$pkg]}"
  449. IFS=' ' read -ra pkg_names <<< "${pkg_aliases[$pkg]}"
  450. fi
  451. for p in "${pkg_names[@]}"; do
  452. if (( no_install_check )) ||
  453. ! { command -v "$p" &>/dev/null || "${PKG_QUERY[@]}" "$p" &>/dev/null; }; then
  454. pkg_array+=("$p")
  455. else
  456. debug "$p is already installed, skipping installation"
  457. fi
  458. done
  459. done
  460. # Generate installation flags based on the distribution
  461. case $ID in
  462. debian|ubuntu)
  463. ((allow_downgrades)) && install_flags+=(--allow-downgrades)
  464. ((reinstall)) && install_flags+=(--reinstall) ;;
  465. fedora|centos)
  466. ((allow_downgrades)) && install_flags+=(--allowerasing)
  467. ((no_gpg_check)) && install_flags+=(--nogpgcheck)
  468. ((refresh)) && install_flags+=(--refresh)
  469. # if ((reinstall)) && [[ ${#pkg_array[@]} -eq 1 ]] && "${PKG_QUERY[@]}" "${pkg_array[0]}" &>/dev/null; then
  470. # PKG_INSTALL=("${PKG_INSTALL[@]/install/reinstall}")
  471. # fi
  472. ;;
  473. suse)
  474. ((no_gpg_check)) && install_flags+=(--allow-unsigned-rpm) ;;
  475. esac
  476. # Install packages
  477. if [[ ${#pkg_array[@]} -gt 0 ]]; then
  478. if ! execute "${PKG_INSTALL[@]}" "${install_flags[@]}" "${pkg_array[@]}"; then
  479. ((silent)) || err "Failed to install ${pkg_array[*]}"
  480. return 1
  481. fi
  482. fi
  483. return 0
  484. }
  485. # @description install host-specific external repos
  486. install_external_repos() {
  487. debug "${FUNCNAME[0]}()"
  488. case $ID in
  489. ubuntu)
  490. if ! grep -E '^deb|^Components' /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -q universe; then
  491. echo "Adding universe repository"
  492. if ! execute sudo add-apt-repository -y universe; then
  493. err "Adding universe repository failed"
  494. fi
  495. fi
  496. ;;
  497. centos)
  498. if ! command -v dpkg &>/dev/null; then
  499. echo "Adding EPEL repository"
  500. if ! install_package epel-release; then
  501. # If epel-release is not available, install it manually
  502. install_package --no-install-check \
  503. "https://dl.fedoraproject.org/pub/epel/epel-release-latest-${VERSION_ID%%.*}.noarch.rpm"
  504. fi
  505. fi
  506. if ! "${PKG_QUERY[@]}" rpmfusion-free-release &>/dev/null; then
  507. echo "Installing the RPMFusion repository"
  508. install_package --no-install-check \
  509. "https://download1.rpmfusion.org/free/el/rpmfusion-free-release-${VERSION_ID%%.*}.noarch.rpm"
  510. fi
  511. # Install mesa-va-drivers-freeworld separately from the RPM using dnf swap
  512. # install_mesa_freeworld # no longer provided by RPMFusion for EL9, etc.
  513. ;;
  514. fedora)
  515. if ! "${PKG_QUERY[@]}" rpmfusion-free-release &>/dev/null; then
  516. echo "Installing the RPMFusion repository"
  517. install_package --no-install-check \
  518. "https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$VERSION_ID.noarch.rpm"
  519. fi
  520. # Install mesa-va-drivers-freeworld separately from the RPM using dnf swap
  521. install_mesa_freeworld
  522. ;;
  523. suse) : # TODO may be needed if X11_XOrg is made unavailable in default repos
  524. # if ! zypper repos | grep -q "X11_XOrg"; then
  525. # echo "Installing the X11 repository"
  526. # execute sudo zypper --non-interactive --quiet addrepo \
  527. # "https://download.opensuse.org/repositories/X11:/XOrg/${NAME// /_}/X11:XOrg.repo"
  528. # execute sudo zypper --non-interactive --quiet refresh
  529. # fi
  530. ;;
  531. esac
  532. }
  533. # @description Installs mesa-va-drivers-freeworld
  534. install_mesa_freeworld() {
  535. debug "${FUNCNAME[0]}()"
  536. local pkg freeworld_pkg
  537. for pkg in mesa-va-drivers mesa-vdpau-drivers mesa-vulkan-drivers; do
  538. freeworld_pkg="${pkg}-freeworld"
  539. if ! "${PKG_QUERY[@]}" "$freeworld_pkg" &>/dev/null; then
  540. if "${PKG_QUERY[@]}" "$pkg" &>/dev/null; then
  541. if ! execute sudo dnf swap -y "$pkg" "$freeworld_pkg"; then
  542. err "Package swap failed for $pkg!"
  543. fi
  544. else
  545. "${PKG_INSTALL[@]}" "$freeworld_pkg"
  546. fi
  547. fi
  548. done
  549. }
  550. # @description Installs JRiver Media Center from a remote repository
  551. install_mc_repo() {
  552. debug "${FUNCNAME[0]}()"
  553. local repo_file repo_text channel
  554. case $ID in
  555. fedora|centos)
  556. repo_file="/etc/yum.repos.d/jriver.repo"
  557. read -r -d '' repo_text <<-EOF
  558. [jriver]
  559. name=JRiver Media Center by BryanC
  560. baseurl=https://repos.bryanroessler.com/jriver
  561. gpgcheck=0
  562. EOF
  563. ;;
  564. debian|ubuntu)
  565. [[ -n $BETAPASS ]] && channel="beta" || channel="latest"
  566. local major_version="${VERSION_ID%%.*}"
  567. local keyfile="/usr/share/keyrings/jriver-com-archive-keyring.gpg"
  568. if [[ ($ID == "ubuntu" && $major_version -ge 24) ||
  569. ($ID == "debian" && (-z $major_version || $major_version -ge 12)) ]]; then
  570. if [[ $channel == "beta" ]]; then
  571. repo_file="/etc/apt/sources.list.d/jriver-beta.sources"
  572. else
  573. repo_file="/etc/apt/sources.list.d/jriver.sources"
  574. fi
  575. # Remove deprecated repo files
  576. old_repo_files=(
  577. "/etc/apt/sources.list.d/jriver.list"
  578. "/etc/apt/sources.list.d/jriver-beta.list"
  579. "/etc/apt/sources.list.d/jriver_beta.list"
  580. )
  581. for f in "${old_repo_files[@]}"; do
  582. [[ -f $f ]] && execute sudo rm -f "$f"
  583. done
  584. read -r -d '' repo_text <<-EOF
  585. Types: deb
  586. URIs: https://dist.jriver.com/$channel/mediacenter/
  587. Signed-By: $keyfile
  588. Suites: $MC_REPO trixie bookworm bullseye oracular noble jammy focal
  589. Components: main
  590. Architectures: amd64 armhf arm64
  591. EOF
  592. else
  593. if [[ $channel == "beta" ]]; then
  594. execute sudo rm -f "/etc/apt/sources.list.d/jriver_beta.list"
  595. repo_file="/etc/apt/sources.list.d/jriver-beta.list"
  596. else
  597. repo_file="/etc/apt/sources.list.d/jriver.list"
  598. fi
  599. repo_text="deb [signed-by=$keyfile arch=amd64,armhf,arm64] https://dist.jriver.com/$channel/mediacenter/ $MC_REPO main"
  600. fi
  601. echo "Installing JRiver Media Center GPG key"
  602. download "https://dist.jriver.com/mediacenter@jriver.com.gpg.key" "-" |
  603. gpg --dearmor | sudo tee "$keyfile" &>/dev/null
  604. ;;
  605. *)
  606. err "An MC repository for $ID is not yet available"
  607. err "Use --install local to install MC on $ID"
  608. return 1
  609. ;;
  610. esac
  611. echo "Adding MC repository file: $repo_file"
  612. debug "repo_text: $repo_text"
  613. sudo tee "$repo_file" &>/dev/null <<< "$repo_text"
  614. "${PKG_UPDATE[@]}" || { err "Package update failed!"; return 1; }
  615. echo "Installing $MC_PKG"
  616. if ! install_package \
  617. --no-install-check \
  618. --allow-downgrades \
  619. --no-gpg-check \
  620. --reinstall \
  621. "$MC_PKG"; then
  622. err "Package install failed!"
  623. return 1
  624. fi
  625. }
  626. # @description Acquires the source DEB package from JRiver
  627. acquire_deb() {
  628. debug "${FUNCNAME[0]}()"
  629. declare -g MC_DEB MC_SOURCE
  630. local fname mnt
  631. [[ -d $OUTPUT_DIR/SOURCES ]] || execute mkdir -p "$OUTPUT_DIR/SOURCES"
  632. # Usually JRiver excludes the release number from the filename
  633. # but in some cases (test builds) it may be included
  634. if [[ $MC_RELEASE -gt 1 ]]; then
  635. fname="MediaCenter-$MC_VERSION-$MC_RELEASE-$MC_ARCH.deb"
  636. else
  637. fname="MediaCenter-$MC_VERSION-$MC_ARCH.deb"
  638. fi
  639. MC_DEB="$OUTPUT_DIR/SOURCES/$fname"
  640. MC_SOURCE="https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/latest/$fname"
  641. # If deb file already exists, skip download
  642. if [[ -f $MC_DEB ]]; then
  643. if [[ $(stat -c%s "$MC_DEB") -lt 10000000 ]]; then
  644. echo "Removing existing DEB under 10MB: $MC_DEB"
  645. execute rm -f "$MC_DEB"
  646. else
  647. echo "Using existing DEB: $MC_DEB"
  648. return 0
  649. fi
  650. fi
  651. # Download the deb file using the containerized package manager
  652. if ! { create_mc_apt_container "apt-get download --allow-unauthenticated mediacenter$MC_MVERSION &>/dev/null" &&
  653. mnt="$(sudo buildah mount "$CNT")" &&
  654. execute sudo find "$mnt" -maxdepth 1 -type f -name "*.deb" -exec cp {} "$MC_DEB" \; &&
  655. [[ -f $MC_DEB ]] &&
  656. execute sudo buildah umount "$CNT" &&
  657. execute sudo buildah rm "$CNT"; }; then
  658. err "Failed to download DEB from containerized package manager"
  659. echo "Using legacy download method"
  660. # Define the repository search order
  661. local -a repos
  662. [[ -n $BETAPASS ]] && repos=("https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/beta/$BETAPASS/$fname")
  663. repos+=(
  664. "https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/latest/$fname"
  665. "https://files.jriver-cdn.com/mediacenter/test/$fname")
  666. # Loop through the repositories and attempt to download
  667. for repo in "${repos[@]}"; do
  668. echo "Checking $repo for DEB package"
  669. if download "$repo" "$MC_DEB"; then
  670. echo "Found"
  671. MC_SOURCE="$repo"
  672. break
  673. fi
  674. done
  675. fi
  676. # Return if the download was successful
  677. [[ -f $MC_DEB ]]
  678. }
  679. # @description Creates a SPEC file and builds the RPM from the source DEB using rpmbuild
  680. build_rpm() {
  681. debug "${FUNCNAME[0]}()"
  682. local i rpmbuild_cmd stub
  683. local -a requires recommends
  684. local spec_file="$OUTPUT_DIR/SPECS/mediacenter$MC_MVERSION-$MC_VERSION-$MC_RELEASE-$BUILD_TARGET-$ARCH.spec"
  685. # skip rebuilding the rpm if it already exists
  686. debug "Checking for existing MC RPM: $MC_RPM"
  687. if [[ -f $MC_RPM && -f $spec_file ]]; then
  688. echo "Spec file and $MC_RPM already exists. Skipping build step"
  689. return 0
  690. fi
  691. # Load deb dependencies into array
  692. IFS=',' read -ra requires <<< "$(dpkg-deb -f "$MC_DEB" Depends)"
  693. IFS=',' read -ra recommends <<< "$(dpkg-deb -f "$MC_DEB" Recommends)"
  694. # Clean up formatting
  695. requires=("${requires[@]%%|*}")
  696. requires=("${requires[@]/?:/}")
  697. requires=("${requires[@]# }")
  698. requires=("${requires[@]% }")
  699. requires=("${requires[@]//\(/}")
  700. requires=("${requires[@]//)/}")
  701. recommends=("${recommends[@]%%|*}")
  702. recommends=("${recommends[@]/?:/}")
  703. recommends=("${recommends[@]# }")
  704. recommends=("${recommends[@]% }")
  705. recommends=("${recommends[@]//\(/}")
  706. recommends=("${recommends[@]//)/}")
  707. # Translate package names
  708. case $BUILD_TARGET in
  709. fedora|centos)
  710. requires=("${requires[@]/libc6/glibc}")
  711. requires=("${requires[@]/libasound2/alsa-lib}")
  712. requires=("${requires[@]/libuuid1/libuuid}")
  713. requires=("${requires[@]/libx11-6/libX11}")
  714. requires=("${requires[@]/libxext6/libXext}")
  715. requires=("${requires[@]/libxcb1*/libxcb}") # TODO Remove minimum version for MC31 (*)
  716. requires=("${requires[@]/libxau6/libXau}")
  717. requires=("${requires[@]/libxdmcp6/libXdmcp}")
  718. requires=("${requires[@]/libstdc++6/libstdc++}")
  719. requires=("${requires[@]/libgtk-3-0/gtk3}")
  720. requires=("${requires[@]/libegl1/mesa-libEGL}")
  721. requires=("${requires[@]/libgl1/mesa-libGL}")
  722. requires=("${requires[@]/libgles2/libglvnd-gles}")
  723. requires=("${requires[@]/libgbm1/mesa-libgbm}")
  724. requires=("${requires[@]/libegl-mesa0/mesa-libEGL}")
  725. requires=("${requires[@]/libvulkan1/vulkan-loader}")
  726. requires=("${requires[@]/libpango1.0-0/pango}")
  727. requires=("${requires[@]/libpango-1.0-0/pango}")
  728. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  729. requires=("${requires[@]/libpangox-1.0-0/pango}")
  730. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  731. requires=("${requires[@]/libnss3/nss}")
  732. requires=("${requires[@]/libnspr4/nspr}")
  733. requires=("${requires[@]/libgomp1/libgomp}")
  734. requires=("${requires[@]/libfribidi0/fribidi}")
  735. requires=("${requires[@]/libfontconfig1/fontconfig}")
  736. requires=("${requires[@]/libfreetype6/freetype}")
  737. requires=("${requires[@]/libharfbuzz0b/harfbuzz}")
  738. requires=("${requires[@]/libva2/libva}")
  739. requires=("${requires[@]/libva-drm2/libva}")
  740. requires=("${requires[@]/libepoxy0/libepoxy}")
  741. requires=("${requires[@]/liblcms2-2/lcms2}")
  742. requires=("${requires[@]/python/python3}")
  743. requires=("${requires[@]/libwebkit2gtk-4.0*/webkit2gtk4.0}")
  744. requires=("${requires[@]/libwebkit2gtk-4.1*/webkit2gtk4.1}")
  745. recommends+=(mesa-va-drivers-freeworld)
  746. ;;
  747. suse)
  748. requires=("${requires[@]/python*/python313}")
  749. requires=("${requires[@]/libc6/glibc}")
  750. requires=("${requires[@]/libasound2/alsa-lib}")
  751. requires=("${requires[@]/libx11-6/libX11-6}")
  752. requires=("${requires[@]/libxext6/libXext6}")
  753. requires=("${requires[@]/libxdmcp6/libXdmcp6}")
  754. requires=("${requires[@]/libgtk-3-0/gtk3}")
  755. requires=("${requires[@]/libgl1/Mesa-libGL1}")
  756. requires=("${requires[@]/libpango-1.0-0/pango}")
  757. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  758. requires=("${requires[@]/libpangox-1.0-0/pango}")
  759. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  760. requires=("${requires[@]/libnss3/mozilla-nss}")
  761. requires=("${requires[@]/libnspr4/mozilla-nspr}")
  762. requires=("${requires[@]/libfribidi0/fribidi}")
  763. requires=("${requires[@]/libfontconfig1/fontconfig}")
  764. requires=("${requires[@]/libharfbuzz0b/libharfbuzz0}")
  765. requires=("${requires[@]/libwebkit2gtk-4.0*/libwebkit2gtk-4_0-37}")
  766. requires=("${requires[@]/libwebkit2gtk-4.1*/libwebkit2gtk-4_1-0}")
  767. for i in "${!requires[@]}"; do
  768. [[ ${requires[$i]} == "mesa-vulkan-drivers" ]] && unset -v 'requires[i]'
  769. done
  770. recommends+=(libvulkan_intel)
  771. recommends+=(libvulkan_radeon)
  772. ;;
  773. esac
  774. # Convert array to newline delim'd string (for heredoc)
  775. printf -v requires "Requires: %s\n" "${requires[@]}"
  776. printf -v recommends "Recommends: %s\n" "${recommends[@]}"
  777. # Strip last newline
  778. requires="${requires%?}"
  779. recommends="${recommends%?}"
  780. if ((COMPAT_SWITCH)); then
  781. # Strip minimum versions
  782. requires=$(echo "$requires" | awk -F" " 'NF == 4 {print $1 " " $2} NF != 4 {print $0}')
  783. fi
  784. # Exclude MC stub executable <= MC31
  785. if [[ $MC_MVERSION -le 31 ]]; then
  786. stub=""
  787. else
  788. stub="%{_bindir}/mc$MC_MVERSION"
  789. fi
  790. # Create spec file
  791. cat <<-EOF > "$spec_file"
  792. Name: mediacenter$MC_MVERSION
  793. Version: $MC_VERSION
  794. Release: $MC_RELEASE
  795. Summary: JRiver Media Center
  796. Group: Applications/Media
  797. Source0: $MC_SOURCE
  798. %define _rpmfilename %%{ARCH}/%%{NAME}-%%{version}-%%{release}.%%{ARCH}.rpm
  799. AutoReq: 0
  800. $requires
  801. $recommends
  802. Conflicts: MediaCenter
  803. Provides: mediacenter$MC_MVERSION
  804. License: Copyright 1998-$(date +%Y), JRiver, Inc. All rights reserved. Protected by U.S. patents #7076468 and #7062468
  805. URL: https://www.jriver.com/
  806. %define __provides_exclude_from ^%{_libdir}/jriver/.*/.*\\.so.*$
  807. %description
  808. Media Center is more than a world class player.
  809. %global __os_install_post %{nil}
  810. %prep
  811. %build
  812. %install
  813. dpkg -x %{S:0} %{buildroot}
  814. %post -p /sbin/ldconfig
  815. %postun -p /sbin/ldconfig
  816. %files
  817. %{_bindir}/mediacenter$MC_MVERSION
  818. $stub
  819. %{_libdir}/jriver
  820. %{_datadir}
  821. %exclude %{_datadir}/applications/media_center_packageinstaller_$MC_MVERSION.desktop
  822. /etc/security/limits.d/*
  823. EOF
  824. # Run rpmbuild
  825. echo "Building MC $MC_VERSION RPM, this may take some time"
  826. rpmbuild_cmd=(
  827. rpmbuild
  828. --define="_topdir $OUTPUT_DIR"
  829. --define="_libdir /usr/lib"
  830. --target="$ARCH"
  831. -bb
  832. "$spec_file"
  833. )
  834. if execute "${rpmbuild_cmd[@]}" && [[ -f $MC_RPM ]] ; then
  835. echo "Build successful. The RPM file is located at: $MC_RPM"
  836. else
  837. err "Build failed"
  838. # After failure, remove the source DEB and reaquire it on next run
  839. if [[ -f $MC_DEB ]]; then
  840. echo "Removing source DEB"
  841. if ! execute rm -f "$MC_DEB"; then
  842. execute sudo rm -f "$MC_DEB"
  843. fi
  844. fi
  845. return 1
  846. fi
  847. }
  848. # @description Installs Media Center via DEB package w/ optional compatability fixes
  849. install_mc_deb() {
  850. debug "${FUNCNAME[0]}()"
  851. if ((COMPAT_SWITCH)); then
  852. local extract_dir; extract_dir="$(mktemp -d)"
  853. pushd "$extract_dir" &>/dev/null || return
  854. command -v ar &>/dev/null || { install_package binutils || return 1; }
  855. execute ar x "$MC_DEB"
  856. execute tar xJf "control.tar.xz"
  857. # Remove minimum version specifiers from control file
  858. execute sed -i 's/ ([^)]*)//g' control
  859. # Remove libwebkit2gtk and their fantastic package versioning strategy
  860. execute sed -E -i 's/,[[:space:]]*libwebkit2gtk[^,]*(,|\?)?//g' control
  861. # TODO workaround for legacy ZorinOS
  862. if [[ $ID == "ubuntu" && ${VERSION_ID%.*} -le 16 ]] &&
  863. grep -q zorin /etc/os-release; then
  864. execute sed -i 's/libva2/libva1/g' control
  865. fi
  866. execute tar -cJf "control.tar.xz" "control" "postinst"
  867. declare -g MC_DEB="${MC_DEB/.deb/.compat.deb}"
  868. execute ar rcs "$MC_DEB" "debian-binary" "control.tar.xz" "data.tar.xz"
  869. popd &>/dev/null || return
  870. execute rm -rf "$extract_dir"
  871. fi
  872. # Helper function to add repository
  873. add_temp_repo() {
  874. local repo_name="$1"
  875. local repo_uri="$2"
  876. local repo_suite="$3"
  877. local repo_key="$4"
  878. declare -g TEMP_REPO_FILE="/etc/apt/sources.list.d/${repo_name}.sources"
  879. echo "Creating temporary repository file $TEMP_REPO_FILE for $repo_suite"
  880. sudo bash -c "cat <<-EOF > $TEMP_REPO_FILE
  881. Types: deb
  882. URIs: $repo_uri
  883. Suites: $repo_suite
  884. Components: main
  885. Architectures: $MC_ARCH
  886. Signed-By: $repo_key
  887. EOF"
  888. "${PKG_UPDATE[@]}" || { err "Package update failed!"; return 1; }
  889. }
  890. # Add older repository for libwebkit2gtk-4.0-37, etc, on newer Debian/Ubuntu
  891. local -i remove_temp_repo=0
  892. if [[ "$ID" == "ubuntu" ]]; then
  893. local major_version="${VERSION_ID%%.*}"
  894. local minor_version="${VERSION_ID##*.}"
  895. if [[ $major_version -gt 24 || ($major_version == 24 && minor_version -ge 4) ]]; then
  896. echo "Temporarily adding jammy repository for libwebkit2gtk-4.0-37, etc."
  897. remove_temp_repo=1
  898. add_temp_repo "jammy" "https://archive.ubuntu.com/ubuntu" "jammy" "/usr/share/keyrings/ubuntu-archive-keyring.gpg"
  899. fi
  900. elif [[ "$ID" == "debian" ]]; then
  901. local major_version="${VERSION_ID%%.*}"
  902. if [[ $major_version -ge 13 || -z $major_version ]]; then
  903. echo "Temporarily adding bookworm repository for libwebkit2gtk-4.0-37, etc."
  904. remove_temp_repo=1
  905. add_temp_repo "bookworm" "https://deb.debian.org/debian" "bookworm" "/usr/share/keyrings/debian-archive-keyring.gpg"
  906. fi
  907. fi
  908. # Copy the DEB to a temporary file so _apt can read it
  909. debug "Creating temporary deb file owned by _apt"
  910. local temp_deb
  911. temp_deb=$(mktemp --suffix=.deb)
  912. execute sudo cp "$MC_DEB" "$temp_deb"
  913. id _apt &>/dev/null && execute sudo chown _apt "$temp_deb"
  914. # Use --reinstall to make sure local package is installed over repo package
  915. if ! install_package \
  916. --no-install-check \
  917. --no-gpg-check \
  918. --allow-downgrades \
  919. --reinstall \
  920. "$temp_deb"; then
  921. err "Local MC DEB installation failed"
  922. ((remove_temp_repo)) && { echo "Removing temporary repo"; execute sudo rm -f "$TEMP_REPO_FILE"; }
  923. if ask_ok "Remove source DEB and retry"; then
  924. execute sudo rm -f "$MC_DEB" "$temp_deb"
  925. exec "$SCRIPT_PATH" "$@" "--no-update"
  926. fi
  927. fi
  928. # Cleanup temporary repository and temporary DEB file
  929. ((remove_temp_repo)) && { echo "Removing temporary repo"; execute sudo rm -f "$TEMP_REPO_FILE"; }
  930. execute sudo rm -f "$temp_deb"
  931. }
  932. # @description Installs MC via RPM package
  933. install_mc_rpm() {
  934. debug "${FUNCNAME[0]}()"
  935. install_package --no-install-check --no-gpg-check --allow-downgrades --reinstall "$MC_RPM"
  936. }
  937. # @description Installs Media Center generically for unsupported OSes
  938. install_mc_generic() {
  939. debug "${FUNCNAME[0]}()"
  940. local extract_dir
  941. local -a raw_files
  942. echo "Using generic installation method"
  943. extract_dir="$(mktemp -d)"
  944. pushd "$extract_dir" &>/dev/null || return
  945. execute ar x "$MC_DEB"
  946. execute tar xJf "control.tar.xz"
  947. echo "You must install the following dependencies manually:"
  948. grep -i "Depends:" control
  949. readarray -t raw_files < <(tar xJvf data.tar.xz)
  950. # Output to log file
  951. for f in "${raw_files[@]/#./}"; do
  952. echo "$f" >> "$SCRIPT_DIR/.uninstall"
  953. done
  954. # Manually install files
  955. for f in "${raw_files[@]}"; do
  956. execute sudo cp -a "$f" "${f/#./}"
  957. done
  958. popd &>/dev/null || return
  959. execute rm -rf "$extract_dir"
  960. return 0
  961. }
  962. # @description Installs MC via PKGBUILD
  963. install_mc_arch() {
  964. debug "${FUNCNAME[0]}()"
  965. [[ -d $OUTPUT_DIR/PKGBUILD ]] || execute mkdir -p "$OUTPUT_DIR/PKGBUILD"
  966. cat <<-EOF > "$OUTPUT_DIR/PKGBUILD/mediacenter.pkgbuild"
  967. pkgname=mediacenter$MC_MVERSION
  968. pkgver=$MC_VERSION
  969. pkgrel=$MC_RELEASE
  970. pkgdesc="The Most Comprehensive Media Software"
  971. arch=("$ARCH")
  972. url="https://www.jriver.com/"
  973. license=('custom')
  974. depends=('alsa-lib' 'ca-certificates' 'gtk3' 'gcc-libs' 'libx11' 'libxext' 'libxcb' 'libxau' 'libxdmcp' 'util-linux' 'mesa-libgl' 'webkit2gtk')
  975. optdepends=(
  976. 'mesa-libgl: nouveau video support'
  977. 'nvidia-libgl: nvidia video support'
  978. 'nvidia-utils: nvidia vulkan support'
  979. 'vulkan-intel: intel vulkan support'
  980. 'vulkan-radeon: amd vulkan support'
  981. 'vorbis-tools: ogg vorbis support'
  982. 'musepack-tools: musepack support'
  983. )
  984. source=("$MC_SOURCE")
  985. package() {
  986. cd "\$srcdir"
  987. bsdtar xf data.tar.xz -C "\$pkgdir"
  988. }
  989. EOF
  990. pushd "$OUTPUT_DIR/PKGBUILD" &>/dev/null || return
  991. if ! execute makepkg \
  992. --install \
  993. --syncdeps \
  994. --clean \
  995. --cleanbuild \
  996. --skipinteg \
  997. --force \
  998. --noconfirm \
  999. -p mediacenter.pkgbuild; then
  1000. err "makepkg failed"; exit 1
  1001. fi
  1002. popd &>/dev/null || return
  1003. }
  1004. # @description Copy the RPM to createrepo-webroot and run createrepo as the createrepo-user
  1005. run_createrepo() {
  1006. debug "${FUNCNAME[0]}()"
  1007. install_package createrepo_c
  1008. # Ensure the webroot exists
  1009. if [[ ! -d $CREATEREPO_WEBROOT ]]; then
  1010. if ! execute sudo -u "$CREATEREPO_USER" mkdir -p "$CREATEREPO_WEBROOT"; then
  1011. if ! (execute sudo mkdir -p "$CREATEREPO_WEBROOT" ||
  1012. execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"); then
  1013. err "Could not create the createrepo-webroot path!"
  1014. err "Make sure that the webroot $CREATEREPO_WEBROOT is writable by user $CREATEREPO_USER"
  1015. err "Or change the repo ownership with --createrepo-user"
  1016. return 1
  1017. fi
  1018. fi
  1019. fi
  1020. # Copy built RPMs to webroot
  1021. if ! execute sudo cp -nf "$MC_RPM" "$CREATEREPO_WEBROOT" ||
  1022. ! execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"; then
  1023. err "Could not copy $MC_RPM to $CREATEREPO_WEBROOT"
  1024. return 1
  1025. fi
  1026. # Run createrepo
  1027. local -a cr_opts=(-q "$CREATEREPO_WEBROOT")
  1028. # [[ -d "$CREATEREPO_WEBROOT/repodata" ]] && cr_opts+=(--update) # TODO temporarily disabled for legacy createrepo
  1029. if ! execute sudo -u "$CREATEREPO_USER" createrepo "${cr_opts[@]}"; then
  1030. if ! (execute sudo createrepo "${cr_opts[@]}" && execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"); then
  1031. err "createrepo failed"
  1032. return 1
  1033. fi
  1034. fi
  1035. }
  1036. # @description Symlink certificates if they do not exist in default location
  1037. link_ssl_certs() {
  1038. debug "${FUNCNAME[0]}()"
  1039. local target_cert f
  1040. local mc_cert_link="$MC_ROOT/ca-certificates.crt"
  1041. local -a source_certs=(
  1042. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"
  1043. "/var/lib/ca-certificates/ca-bundle.pem"
  1044. "$MC_ROOT/local-ca-certificates.crt")
  1045. target_cert=$(readlink -f "$mc_cert_link")
  1046. [[ -f $target_cert ]] && return 0
  1047. for f in "${source_certs[@]}"; do
  1048. if [[ -f $f ]]; then
  1049. if execute sudo ln -fs "$f" "$mc_cert_link"; then
  1050. debug "Symlinked $mc_cert_link to $f"
  1051. return 0
  1052. fi
  1053. fi
  1054. done
  1055. err "Certificate symlinking failed"; return 1
  1056. }
  1057. # @description Restore the mjr license file from MJR_FILE or other common locations
  1058. restore_license() {
  1059. debug "${FUNCNAME[0]}()"
  1060. local newest f
  1061. local -a mjrfiles
  1062. # Glob mjr files from common directories
  1063. shopt -s nullglob
  1064. mjrfiles=(
  1065. "$SCRIPT_DIR"/*.mjr
  1066. "$OUTPUT_DIR"/*.mjr
  1067. "$HOME"/[dD]ownloads/*.mjr
  1068. "$HOME"/[dD]ocuments/*.mjr
  1069. )
  1070. shopt -u nullglob
  1071. if [[ ${#mjrfiles[@]} -gt 0 ]]; then
  1072. debug "mjrfiles=(${mjrfiles[*]})"
  1073. # Sort globbed files by time, newest first
  1074. newest=${mjrfiles[0]}
  1075. for f in "${mjrfiles[@]}"; do
  1076. if [[ -f $f && $f -nt $newest ]]; then
  1077. newest=$f
  1078. fi
  1079. done
  1080. debug "Latest mjrfile: $newest"
  1081. for f in "$MJR_FILE" "$newest"; do
  1082. if [[ -f $f ]]; then
  1083. if execute "mediacenter$MC_MVERSION" "/RestoreFromFile" "$f"; then
  1084. echo "Restored license from $f"
  1085. return 0
  1086. else
  1087. err "Failed to restore license from $f"
  1088. fi
  1089. fi
  1090. done
  1091. fi
  1092. }
  1093. # @description Opens ports using the system firewall tool
  1094. # @arg $1 string Service name
  1095. # @arg $2 array List of ports in firewall-cmd format
  1096. open_firewall() {
  1097. debug "${FUNCNAME[0]}()" "$@"
  1098. local service="$1"
  1099. shift
  1100. local -a f_ports=("$@") # for firewall-cmd
  1101. local u_ports="$*"
  1102. u_ports="${u_ports// /|}" # concatenate
  1103. u_ports="${u_ports//-/\:}" # for ufw
  1104. local port
  1105. if command -v firewall-cmd &>/dev/null; then
  1106. if ! sudo firewall-cmd --get-services | grep -q "$service"; then
  1107. execute sudo firewall-cmd --permanent "--new-service=$service"
  1108. execute sudo firewall-cmd --permanent "--service=$service" "--set-description=$service installed by installJRMC"
  1109. execute sudo firewall-cmd --permanent "--service=$service" "--set-short=$service"
  1110. for port in "${f_ports[@]}"; do
  1111. execute sudo firewall-cmd --permanent "--service=$service" "--add-port=$port"
  1112. done
  1113. execute sudo firewall-cmd --add-service "$service" --permanent
  1114. execute sudo firewall-cmd --reload
  1115. fi
  1116. elif command -v ufw &>/dev/null; then
  1117. sudo bash -c "cat <<-EOF > /etc/ufw/applications.d/$service
  1118. [$service]
  1119. title=$service
  1120. description=$service installed by installJRMC
  1121. ports=$u_ports
  1122. EOF"
  1123. execute sudo ufw app update "$service"
  1124. execute sudo ufw allow "$service"
  1125. else
  1126. echo "Warning: Install firewall-cmd or ufw to open firewall ports"
  1127. return 1
  1128. fi
  1129. }
  1130. # @description Create the xvnc or x11vnc password file
  1131. # @arg $1 string Service type (xvnc, x11vnc)
  1132. set_vnc_pass() {
  1133. debug "${FUNCNAME[0]}()"
  1134. local vncpassfile="$HOME/.vnc/jrmc_passwd"
  1135. [[ -d ${vncpassfile%/*} ]] || execute mkdir -p "${vncpassfile%/*}"
  1136. if [[ -f $vncpassfile ]]; then
  1137. if [[ ! -v VNCPASS ]]; then
  1138. err "Refusing to overwrite existing $vncpassfile with an empty password"
  1139. err "Remove existing $vncpassfile or use --vncpass ''"
  1140. return 1
  1141. else
  1142. execute rm -f "$vncpassfile"
  1143. fi
  1144. fi
  1145. if [[ -v VNCPASS ]]; then
  1146. if [[ $1 == "xvnc" ]]; then
  1147. echo "$VNCPASS" | vncpasswd -f > "$vncpassfile"
  1148. elif [[ $1 == "x11vnc" ]]; then
  1149. execute x11vnc -storepasswd "$VNCPASS" "$vncpassfile"
  1150. fi
  1151. return
  1152. else
  1153. declare -gi NOVNCAUTH=1
  1154. fi
  1155. }
  1156. # @description Set display and port variables
  1157. set_display_vars() {
  1158. debug "${FUNCNAME[0]}()"
  1159. declare -g THIS_DISPLAY THIS_DISPLAY_NUM NEXT_DISPLAY
  1160. # Check USER_DISPLAY, else environment DISPLAY, else set to :0
  1161. THIS_DISPLAY="${USER_DISPLAY:-${DISPLAY:-:0}}"
  1162. THIS_DISPLAY_NUM="${THIS_DISPLAY#*:}" # strip prefix
  1163. THIS_DISPLAY_NUM="${THIS_DISPLAY_NUM%%.*}" # strip suffix
  1164. # Increment each time we run this
  1165. if ((NEXT_DISPLAY_NUM)); then
  1166. declare -g NEXT_DISPLAY_NUM=$((NEXT_DISPLAY_NUM + 1))
  1167. else
  1168. declare -g NEXT_DISPLAY_NUM=$((THIS_DISPLAY_NUM + 1))
  1169. fi
  1170. NEXT_DISPLAY=":$NEXT_DISPLAY_NUM"
  1171. }
  1172. # @description Create associated service variables based on service name
  1173. # @arg $1 string Service name
  1174. set_service_vars() {
  1175. debug "${FUNCNAME[0]}()" "$@"
  1176. declare -g SERVICE_NAME SERVICE_FNAME TIMER_NAME TIMER_FNAME
  1177. declare -g USER_STRING GRAPHICAL_TARGET
  1178. declare -ga RELOAD ENABLE DISABLE IS_ENABLED IS_ACTIVE
  1179. local -a systemctl_prefix
  1180. local service_name="$1"
  1181. local service_type="${SERVICE_TYPE:-${2:-system}}"
  1182. local service_dir="/usr/lib/systemd/$service_type"
  1183. if [[ $USER == "root" && $service_type == "user" ]]; then
  1184. err "Trying to install user service as root"
  1185. err "Use --service-type service and/or execute installJRMC as non-root user"
  1186. return 1
  1187. fi
  1188. if [[ $service_type == "system" ]]; then
  1189. systemctl_prefix=(sudo systemctl)
  1190. GRAPHICAL_TARGET="graphical.target"
  1191. elif [[ $service_type == "user" ]]; then
  1192. systemctl_prefix=(systemctl --user)
  1193. GRAPHICAL_TARGET="default.target"
  1194. fi
  1195. # systemctl commands
  1196. RELOAD=("${systemctl_prefix[@]}" daemon-reload)
  1197. ENABLE=("${systemctl_prefix[@]}" enable --now)
  1198. DISABLE=("${systemctl_prefix[@]}" disable --now)
  1199. IS_ENABLED=("${systemctl_prefix[@]}" is-enabled --quiet)
  1200. IS_ACTIVE=("${systemctl_prefix[@]}" is-active --quiet)
  1201. [[ -d $service_dir ]] || execute sudo mkdir -p "$service_dir"
  1202. if [[ $service_type == "system" && $USER != "root" ]]; then
  1203. SERVICE_FNAME="$service_dir/$service_name@.service"
  1204. TIMER_FNAME="$service_dir/$service_name@.timer"
  1205. SERVICE_NAME="$service_name@$USER.service"
  1206. TIMER_NAME="$service_name@$USER.timer"
  1207. USER_STRING="User=%I"
  1208. else
  1209. SERVICE_NAME="$service_name.service"
  1210. TIMER_NAME="$service_name.timer"
  1211. SERVICE_FNAME="$service_dir/$SERVICE_NAME"
  1212. TIMER_FNAME="$service_dir/${TIMER_NAME}"
  1213. USER_STRING=""
  1214. fi
  1215. }
  1216. # @section Services
  1217. # @description Starts and enables (at startup) a JRiver Media Center service
  1218. # @arg $1 string Passes arguments as startup options to /usr/bin/mediacenter??
  1219. service_jriver-mediacenter() {
  1220. debug "${FUNCNAME[0]}()"
  1221. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1222. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1223. [Unit]
  1224. Description=JRiver Media Center $MC_MVERSION
  1225. After=$GRAPHICAL_TARGET
  1226. [Service]
  1227. Type=simple
  1228. $USER_STRING
  1229. ExecStart=/usr/bin/mediacenter$MC_MVERSION $*
  1230. KillMode=none
  1231. ExecStop=/usr/bin/mc$MC_MVERSION /MCC 20007
  1232. Restart=always
  1233. RestartSec=10
  1234. TimeoutStopSec=30
  1235. [Install]
  1236. WantedBy=$GRAPHICAL_TARGET
  1237. EOF"
  1238. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1239. "${RELOAD[@]}" &&
  1240. "${ENABLE[@]}" "$SERVICE_NAME"
  1241. }
  1242. # @description Starts and enables (at startup) a JRiver Media Server service
  1243. service_jriver-mediaserver() {
  1244. debug "${FUNCNAME[0]}()"
  1245. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1246. service_jriver-mediacenter "/MediaServer"
  1247. }
  1248. # @description Starts and enables (at startup) JRiver Media Center in a new Xvnc session
  1249. # TODO https://github.com/TigerVNC/tigervnc/blob/master/unix/vncserver/HOWTO.md
  1250. service_jriver-xvnc() {
  1251. debug "${FUNCNAME[0]}()"
  1252. local -a start_cmd
  1253. set_service_vars "${FUNCNAME[0]##*_}" "system"
  1254. set_display_vars
  1255. declare -g PORT=$((NEXT_DISPLAY_NUM + 5900))
  1256. install_package tigervnc-server
  1257. set_vnc_pass xvnc
  1258. start_cmd=(
  1259. /usr/bin/vncserver "$NEXT_DISPLAY"
  1260. -geometry 1440x900
  1261. -alwaysshared
  1262. -autokill
  1263. -xstartup "/usr/bin/mediacenter$MC_MVERSION"
  1264. )
  1265. if ((NOVNCAUTH)); then
  1266. start_cmd+=(
  1267. -name "jriver$NEXT_DISPLAY"
  1268. -SecurityTypes None)
  1269. else
  1270. start_cmd+=(
  1271. -rfbauth "$HOME/.vnc/jrmc_passwd")
  1272. fi
  1273. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1274. [Unit]
  1275. Description=Remote desktop service (VNC)
  1276. After=multi-user.target
  1277. [Service]
  1278. Type=forking
  1279. $USER_STRING
  1280. ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill $NEXT_DISPLAY &>/dev/null || :'
  1281. ExecStart=${start_cmd[*]}
  1282. ExecStop=/usr/bin/vncserver -kill $NEXT_DISPLAY
  1283. Restart=always
  1284. [Install]
  1285. WantedBy=multi-user.target
  1286. EOF"
  1287. "${RELOAD[@]}"
  1288. if ! "${ENABLE[@]}" "$SERVICE_NAME"; then
  1289. err "vncserver failed to start on DISPLAY $NEXT_DISPLAY"
  1290. # Allow to increment 10 times before breaking
  1291. max=$((THIS_DISPLAY_NUM + 10))
  1292. while [[ $NEXT_DISPLAY_NUM -lt $max ]]; do
  1293. echo "Incrementing DISPLAY and retrying"
  1294. service_jriver-xvnc && return
  1295. done
  1296. return 1
  1297. else
  1298. echo "Xvnc running on localhost:$PORT"
  1299. open_firewall "jriver-xvnc" "$PORT/tcp"
  1300. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1301. return 0
  1302. fi
  1303. }
  1304. # @description Starts and enables (at startup) x11vnc screen sharing for the local desktop
  1305. service_jriver-x11vnc() {
  1306. debug "${FUNCNAME[0]}()"
  1307. local -a start_cmd
  1308. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1309. set_display_vars
  1310. declare -g PORT=$((THIS_DISPLAY_NUM + 5900))
  1311. install_package x11vnc
  1312. set_vnc_pass x11vnc
  1313. # If .Xauthority file is missing, generate a dummy for x11vnc -auth guess
  1314. if [[ ! -f "$HOME/.Xauthority" ]]; then
  1315. [[ $XDG_SESSION_TYPE == "wayland" ]] &&
  1316. ask_ok "Unsupported Wayland session detected for x11vnc, continue?" || return 1
  1317. debug "Generating $HOME/.Xauthority"
  1318. execute touch "$HOME/.Xauthority"
  1319. execute chmod 644 "$HOME/.Xauthority"
  1320. xauth generate "$DISPLAY" . trusted
  1321. xauth add "$HOST$DISPLAY" . "$(xxd -l 16 -p /dev/urandom)"
  1322. fi
  1323. start_cmd=(
  1324. /usr/bin/x11vnc
  1325. -display "$DISPLAY"
  1326. -noscr
  1327. -auth guess
  1328. -forever
  1329. -bg
  1330. )
  1331. if ((NOVNCAUTH)); then
  1332. start_cmd+=(-nopw)
  1333. else
  1334. start_cmd+=(-rfbauth "$HOME/.vnc/jrmc_passwd")
  1335. fi
  1336. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1337. [Unit]
  1338. Description=x11vnc
  1339. After=$GRAPHICAL_TARGET
  1340. [Service]
  1341. $USER_STRING
  1342. Type=forking
  1343. Environment=DISPLAY=$DISPLAY
  1344. ExecStart=${start_cmd[*]}
  1345. Restart=always
  1346. RestartSec=10
  1347. [Install]
  1348. WantedBy=$GRAPHICAL_TARGET
  1349. EOF"
  1350. open_firewall "jriver-x11vnc" "$PORT/tcp"
  1351. "${RELOAD[@]}" &&
  1352. "${ENABLE[@]}" "$SERVICE_NAME" &&
  1353. echo "x11vnc running on localhost:$PORT"
  1354. }
  1355. # @description Starts and enables (at startup) an hourly service to build the latest version of
  1356. # JRiver Media Center RPM from the source DEB and create/update an RPM repository
  1357. service_jriver-createrepo() {
  1358. debug "${FUNCNAME[0]}()"
  1359. if [[ $CREATEREPO_USER != "$USER" ]]; then
  1360. USER="root" set_service_vars "${FUNCNAME[0]##*_}" "system"
  1361. else
  1362. set_service_vars "${FUNCNAME[0]##*_}" "system"
  1363. fi
  1364. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1365. [Unit]
  1366. Description=Builds JRiver Media Center RPM, moves it to the repo dir, and runs createrepo
  1367. [Service]
  1368. $USER_STRING
  1369. ExecStart=$SCRIPT_DIR/installJRMC --outputdir=$OUTPUT_DIR --createrepo=$CREATEREPO_TARGET \
  1370. --createrepo-webroot=$CREATEREPO_WEBROOT --createrepo-user=$CREATEREPO_USER --mcrepo=$MC_REPO --yes --no-update
  1371. [Install]
  1372. WantedBy=multi-user.target
  1373. EOF"
  1374. sudo bash -c "cat <<-EOF > $TIMER_FNAME
  1375. [Unit]
  1376. Description=Run JRiver MC rpmbuild hourly
  1377. [Timer]
  1378. OnCalendar=hourly
  1379. Persistent=true
  1380. [Install]
  1381. WantedBy=timers.target
  1382. EOF"
  1383. "${RELOAD[@]}" &&
  1384. "${ENABLE[@]}" "$TIMER_NAME"
  1385. }
  1386. # @description Detects if MC is installed on btrfs and disables CoW
  1387. disable_btrfs_cow() {
  1388. debug "${FUNCNAME[0]}()"
  1389. local dir
  1390. local mc_user_path="$HOME/.jriver"
  1391. for dir in "$MC_ROOT" "$mc_user_path"; do
  1392. [[ -d $mc_user_path ]] || execute mkdir -p "$mc_user_path"
  1393. if [[ $(stat -f -c %T "$dir") == "btrfs" ]] &&
  1394. ! lsattr -d "$dir" | cut -f1 -d" " | grep -q C &&
  1395. execute sudo chattr +C "$dir"; then
  1396. echo "Disabled btrfs CoW for $dir directory"
  1397. fi
  1398. done
  1399. }
  1400. # @description Completely uninstalls MC, services, and firewall rules
  1401. uninstall() {
  1402. debug "${FUNCNAME[0]}()"
  1403. local service type unit f
  1404. if ! ask_ok "Do you really want to uninstall JRiver Media Center?"; then
  1405. echo "Uninstall cancelled"
  1406. return 1
  1407. fi
  1408. echo "Stopping and removing all Media Center services"
  1409. for service in $(compgen -A "function" "service"); do
  1410. service="${service##service_}"
  1411. for type in user system; do
  1412. set_service_vars "$service" "$type";
  1413. for unit in "$SERVICE_NAME" "$TIMER_NAME"; do
  1414. if "${IS_ACTIVE[@]}" "$unit" ||
  1415. "${IS_ENABLED[@]}" "$unit" &>/dev/null; then
  1416. "${DISABLE[@]}" "$unit"
  1417. fi
  1418. done
  1419. for f in "$SERVICE_FNAME" "$TIMER_FNAME"; do
  1420. [[ -f $f ]] && execute sudo rm -f "$f"
  1421. done
  1422. "${RELOAD[@]}"
  1423. done
  1424. done
  1425. echo "Removing MC repository files"
  1426. execute sudo rm -f \
  1427. "/etc/yum.repos.d/jriver.repo" \
  1428. /etc/apt/sources.list.d/{jriver,mediacenter}*.{list,sources} # also remove legacy repo files
  1429. [[ $ID == "suse" ]] && execute sudo zypper --non-interactive removerepo jriver
  1430. echo "Removing firewall rules"
  1431. for service in jriver-mediacenter jriver-xvnc jriver-x11vnc; do
  1432. if command -v firewall-cmd &>/dev/null; then
  1433. execute sudo firewall-cmd --permanent --remove-service=$service
  1434. execute sudo firewall-cmd --permanent --delete-service=$service
  1435. execute sudo firewall-cmd --reload
  1436. elif command -v ufw &>/dev/null; then
  1437. execute sudo ufw delete allow $service
  1438. [[ -f /etc/ufw/applications.d/$service ]] &&
  1439. execute sudo rm -f /etc/ufw/applications.d/$service
  1440. fi
  1441. done
  1442. echo "Uninstalling the JRiver Media Center package"
  1443. if "${PKG_REMOVE[@]}" "${MC_PKG%%=*}"; then # remove version specifier
  1444. echo "Successfully uninstalled JRiver Media Center ${MC_PKG%%=*}"
  1445. elif [[ $? -eq 100 ]]; then
  1446. err "JRiver Media Center package '${MC_PKG%%=*}' is not present and was not uninstalled"
  1447. else
  1448. err "Could not remove Media Center package"
  1449. fi
  1450. local keyfile="/usr/share/keyrings/jriver-com-archive-keyring.gpg"
  1451. if [[ -f $keyfile ]]; then
  1452. echo "Removing the JRiver Media Center GPG key"
  1453. execute sudo rm -f "$keyfile"
  1454. fi
  1455. if [[ -f $SCRIPT_DIR/.uninstall ]]; then
  1456. echo "Removing files from .uninstall log"
  1457. while read -r p; do
  1458. [[ -d $p ]] && execute sudo rm -rf "$p"
  1459. done < "$SCRIPT_DIR/.uninstall"
  1460. mv "$SCRIPT_DIR/.uninstall" "$SCRIPT_DIR/.uninstall.bk"
  1461. fi
  1462. if [[ -d $OUTPUT_DIR ]]; then
  1463. if ask_ok "Remove installJRMC output directory $OUTPUT_DIR?"; then
  1464. execute sudo rm -rf "$OUTPUT_DIR"
  1465. fi
  1466. fi
  1467. echo "To reset your MC library: mv $HOME/.jriver $HOME/.jriver.bk"
  1468. echo "To remove your MC library: rm -rf $HOME/.jriver"
  1469. return 0
  1470. }
  1471. # @description Checks for installJRMC update and re-executes, if necessary
  1472. update() {
  1473. debug "${FUNCNAME[0]}()" "$@"
  1474. debug "Checking for installJRMC update"
  1475. # Extract and normalize version from a script
  1476. extract_version() {
  1477. local version_line
  1478. version_line=$(grep -m 1 'SCRIPT_VERSION=' "$1")
  1479. version_line=${version_line#*=}
  1480. version_line=${version_line#\"}
  1481. version_line=${version_line%-dev\"}
  1482. version_line=${version_line%\"}
  1483. echo "$version_line"
  1484. }
  1485. # Compare semantic version strings
  1486. version_greater() {
  1487. [[ "$(echo -e "$1\n$2" | sort -V | head -n 1)" != "$1" ]]
  1488. }
  1489. # Check if we're in a git directory and if it's the installJRMC repository
  1490. if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null &&
  1491. [[ "$(git -C "$SCRIPT_DIR" config --get remote.origin.url)" =~ installJRMC|installjrmc ]]; then
  1492. # Get the current commit hash
  1493. local before_pull_hash after_pull_hash
  1494. before_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
  1495. # Stash local changes before pull
  1496. execute git -C "$SCRIPT_DIR" stash push --quiet
  1497. # Pull latest changes
  1498. debug "Running git pull in $SCRIPT_DIR"
  1499. if ! git -C "$SCRIPT_DIR" pull | grep -q "Already up to date"; then
  1500. # Get the new commit hash after pull
  1501. after_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
  1502. # If the commit hash has changed, an update occurred
  1503. if [[ "$before_pull_hash" != "$after_pull_hash" ]]; then
  1504. echo "Detected installJRMC update, restarting"
  1505. execute git -C "$SCRIPT_DIR" stash pop --quiet
  1506. exec "$SCRIPT_PATH" "$@" "--no-update"
  1507. fi
  1508. fi
  1509. execute git -C "$SCRIPT_DIR" stash pop --quiet
  1510. else
  1511. debug "Not in a git repository or not the installJRMC repository. Checking for updates via download."
  1512. local tmp
  1513. tmp=$(mktemp) || { err "Failed to create temporary file."; return 1; }
  1514. # Acquire the latest version of the script
  1515. if ! download "$SCRIPT_URL" "$tmp"; then
  1516. err "Failed to download the latest script."
  1517. execute rm -f "$tmp"
  1518. return 1
  1519. fi
  1520. # Extract the latest version number
  1521. local remote_version
  1522. remote_version=$(extract_version "$tmp")
  1523. if [[ -z "$remote_version" ]]; then
  1524. err "Failed to extract version from the downloaded script."
  1525. execute rm -f "$tmp"
  1526. return 1
  1527. fi
  1528. # Compare versions and update if the remote version is greater
  1529. if version_greater "$remote_version" "$SCRIPT_VERSION"; then
  1530. execute mv "$tmp" "$SCRIPT_PATH" || { err "Failed to replace the script"; execute rm -f "$tmp"; return 1; }
  1531. execute chmod +x "$SCRIPT_PATH" || { err "Failed to make the script executable"; return 1; }
  1532. execute rm -f "$tmp"
  1533. echo "Detected installJRMC update, restarting"
  1534. exec "$SCRIPT_PATH" "$@" "--no-update"
  1535. else
  1536. debug "Current installJRMC $SCRIPT_VERSION is the latest version"
  1537. execute rm -f "$tmp"
  1538. return 0
  1539. fi
  1540. fi
  1541. }
  1542. # @description installJRMC main function
  1543. main() {
  1544. debug "${FUNCNAME[0]}()" "$@" # prints function name and arguments
  1545. echo "Starting installJRMC $SCRIPT_VERSION"
  1546. if ((DEBUG)); then
  1547. echo "Debugging on"
  1548. else
  1549. echo "To enable debugging output, use --debug or -d"
  1550. fi
  1551. # Parse input, set default/host variables, and MC version
  1552. init "$@"
  1553. ((UNINSTALL_SWITCH)) && uninstall
  1554. install_external_repos
  1555. if ((REPO_INSTALL_SWITCH)); then
  1556. echo "Installing JRiver Media Center from remote repository"
  1557. if install_mc_repo; then
  1558. echo "JRiver Media Center installed successfully from remote repository"
  1559. link_ssl_certs
  1560. restore_license
  1561. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1562. disable_btrfs_cow
  1563. else
  1564. err "JRiver Media Center installation from remote repository failed"
  1565. return 1
  1566. fi
  1567. fi
  1568. if ((BUILD_SWITCH)); then
  1569. acquire_deb || { err "Could not download Media Center DEB package"; return 1; }
  1570. if [[ $BUILD_TARGET =~ centos|fedora|suse || $CREATEREPO_TARGET =~ centos|fedora|suse ]]; then
  1571. install_package dpkg rpm-build
  1572. [[ -d $OUTPUT_DIR/SPECS ]] || execute mkdir -p "$OUTPUT_DIR/SPECS"
  1573. build_rpm
  1574. fi
  1575. fi
  1576. if ((LOCAL_INSTALL_SWITCH)); then
  1577. if PKG_INSTALL_LOCAL "$@"; then
  1578. echo "JRiver Media Center installed successfully from local package"
  1579. else
  1580. err "JRiver Media Center local package installation failed"
  1581. return 1
  1582. fi
  1583. link_ssl_certs
  1584. restore_license
  1585. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1586. disable_btrfs_cow
  1587. fi
  1588. if ((CREATEREPO_SWITCH)); then
  1589. if run_createrepo; then
  1590. echo "Successfully updated repo"
  1591. else
  1592. err "Repo creation failed"
  1593. fi
  1594. fi
  1595. if [[ ${#SERVICES[@]} -gt 0 ]]; then
  1596. declare service
  1597. for service in "${SERVICES[@]}"; do
  1598. if ! "service_$service"; then
  1599. if [[ $? -eq 127 ]]; then
  1600. err "Service $service does not exist, check service name"
  1601. else
  1602. err "Failed to create $service service"
  1603. fi
  1604. else
  1605. echo "Started and enabled $service service"
  1606. fi
  1607. done
  1608. unset service
  1609. fi
  1610. }
  1611. # @section Helper functions
  1612. debug() { ((DEBUG)) && echo "Debug: $*"; }
  1613. err() { echo "Error: $*" >&2; }
  1614. ask_ok() {
  1615. local response
  1616. ((YES_SWITCH)) && return 0
  1617. read -n 1 -r -p "$* [y/N]: " response
  1618. echo
  1619. [[ ${response,,} == y ]]
  1620. }
  1621. execute() {
  1622. if debug "$*"; then
  1623. "$@"
  1624. else
  1625. "$@" &>/dev/null
  1626. fi
  1627. }
  1628. download() {
  1629. debug "${FUNCNAME[0]}()" "$@"
  1630. local url="$1"
  1631. local output="${2:-}"
  1632. local -a cmd
  1633. if command -v curl &>/dev/null || install_package --silent curl; then
  1634. cmd=(curl --silent --fail --location)
  1635. if [[ -n "$output" ]]; then
  1636. cmd+=(--output "$output")
  1637. else
  1638. cmd+=(--remote-name)
  1639. fi
  1640. elif command -v wget &>/dev/null || install_package --silent wget; then
  1641. cmd=(wget --quiet)
  1642. [[ -n "$output" ]] && cmd+=("--output-document=$output")
  1643. else
  1644. err "Unable to install wget or curl"
  1645. return 1
  1646. fi
  1647. debug "${cmd[@]}" "$url"
  1648. "${cmd[@]}" "$url"
  1649. }
  1650. create_mc_apt_container() {
  1651. debug "${FUNCNAME[0]}()" "$@"
  1652. declare -g CNT
  1653. local -a cmds=("$@")
  1654. { command -v buildah &>/dev/null || install_package buildah &>/dev/null; } &&
  1655. CNT=$(sudo buildah from --quiet alpine:edge) &&
  1656. sudo buildah run "$CNT" -- sh -c '
  1657. apk add --no-cache apt curl gnupg &>/dev/null
  1658. curl -fsSL https://dist.jriver.com/mediacenter@jriver.com.gpg.key | gpg --dearmor -o /usr/share/keyrings/jriver-com-archive-keyring.gpg &>/dev/null
  1659. cat <<-EOF > /etc/apt/sources.list.d/jriver.sources
  1660. Types: deb
  1661. URIs: https://dist.jriver.com/latest/mediacenter/
  1662. Signed-By: /usr/share/keyrings/jriver-com-archive-keyring.gpg
  1663. Suites: '"$MC_REPO"'
  1664. Components: main
  1665. Architectures: '"$MC_ARCH"'
  1666. EOF
  1667. apt-get update &>/dev/null' &&
  1668. # If user passes command strings run them in the container
  1669. for cmd in "${cmds[@]}"; do
  1670. sudo buildah run "$CNT" -- sh -c "$cmd" || { err "$cmd failed"; return 1; }
  1671. done
  1672. }
  1673. # Roughly turn debugging on for pre-init
  1674. # Reset and reparse in parse_input() with getopt
  1675. [[ " $* " =~ ( --debug | -d ) ]] && DEBUG=1
  1676. main "$@"
  1677. exit