installJRMC 62 KB

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