installJRMC 62 KB

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