installJRMC 61 KB

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