installJRMC 65 KB

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