installJRMC 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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.4-dev"
  24. declare -g MC_VERSION="34.0.42" # 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|mandriva]
  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|mandriva]
  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. *mandriva*) ID="mandriva"
  268. if ((REPO_INSTALL_SWITCH)); then
  269. debug "Automatically using --install=local for Mandriva"
  270. REPO_INSTALL_SWITCH=0
  271. BUILD_SWITCH=1
  272. LOCAL_INSTALL_SWITCH=1
  273. fi ;;
  274. manjaro|arch) ID="arch"
  275. if ((REPO_INSTALL_SWITCH)); then
  276. debug "Automatically using --install=local for Arch"
  277. REPO_INSTALL_SWITCH=0
  278. BUILD_SWITCH=1
  279. LOCAL_INSTALL_SWITCH=1
  280. fi ;;
  281. *suse*) ID="suse"
  282. if ((REPO_INSTALL_SWITCH)); then
  283. debug "Automatically using --install=local for SUSE"
  284. REPO_INSTALL_SWITCH=0
  285. BUILD_SWITCH=1
  286. LOCAL_INSTALL_SWITCH=1
  287. fi ;;
  288. *) err "Auto-detecting distro, this is unreliable and --compat may be required"
  289. for cmd in dnf yum apt-get pacman; do
  290. if command -v "$cmd" &>/dev/null; then
  291. case "$cmd" in
  292. dnf) ID="fedora" ;;
  293. yum) ID="centos"; COMPAT_SWITCH=1 ;;
  294. apt-get) ID="ubuntu" ;;
  295. pacman) ID="arch" ;;
  296. esac
  297. break
  298. fi
  299. done
  300. if [[ -z $ID ]]; then
  301. err "OS detection failed!"
  302. if ask_ok "Continue with manual installation?"; then
  303. debug "Automatically using --install=local for unknown distro"
  304. ID="unknown"
  305. REPO_INSTALL_SWITCH=0
  306. BUILD_SWITCH=1
  307. LOCAL_INSTALL_SWITCH=1
  308. else
  309. exit 1
  310. fi
  311. fi ;;
  312. esac
  313. # Set distro-specific package manager commands for normalized IDs
  314. case $ID in
  315. fedora|centos|mandriva)
  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 --install-recommends -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. # Repo selection
  357. # Match repo to the host on MC31+ unless overriden by user
  358. if [[ $ID =~ debian|ubuntu && "${USER_MC_MVERSION:-${MC_VERSION%%.*}}" -ge 31 ]]; then
  359. MC_REPO=${UBUNTU_CODENAME:-${VERSION_CODENAME:-$MC_REPO}}
  360. fi
  361. MC_REPO="${USER_MC_REPO:-$MC_REPO}" # allow user override
  362. echo "MC source -> target: $MC_REPO $MC_ARCH -> $BUILD_TARGET $ARCH"
  363. # Retrieves the latest MC version number if we need it
  364. if ((BUILD_SWITCH || LOCAL_INSTALL_SWITCH || CREATEREPO_SWITCH)); then
  365. get_latest_mc_version
  366. fi
  367. # Set MC version variables
  368. MC_VERSION="${USER_MC_VERSION:-$MC_VERSION}"
  369. MC_RELEASE="${USER_MC_RELEASE:-1}"
  370. MC_MVERSION="${USER_MC_MVERSION:-${MC_VERSION%%.*}}"
  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|mandriva) 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 "${FUNCNAME[0]}()"
  386. local mc_version_source
  387. # User --mcversion
  388. if [[ -n $USER_MC_VERSION ]]; then
  389. mc_version_source="user input"
  390. # Containerized package manager
  391. elif create_mc_apt_container &&
  392. 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) &&
  393. execute sudo buildah rm "$CNT" &&
  394. [[ $MC_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  395. mc_version_source="containerized package manager"
  396. # Fallback to webscrape
  397. 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) &&
  398. [[ $MC_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  399. mc_version_source="webscrape"
  400. # Fallback to hardcoded value
  401. else
  402. mc_version_source="hardcoded"
  403. err "Warning! Using hardcoded version number"
  404. fi
  405. echo "Selected MC version $MC_VERSION from the $MC_REPO repo (via $mc_version_source)"
  406. }
  407. # @description Installs a package using the system package manager
  408. # @arg $1 array One or more package names
  409. # @option --no-install-check Do not check if package is already installed
  410. # @option --no-gpg-check Disable GPG checks for RPM based distros
  411. # @option --allow-downgrades Useful for installing specific MC versions
  412. # @option --silent | -s Do not print errors (useful for optional packages)
  413. install_package() {
  414. debug "${FUNCNAME[0]}()" "$@"
  415. local -a pkg_array install_flags
  416. local -A pkg_aliases
  417. local input pkg _pkg
  418. local -i no_install_check=0 allow_downgrades=0 silent=0 refresh=0 no_gpg_check=0 reinstall=0
  419. local long_opts="no-install-check,allow-downgrades,no-gpg-check,refresh,reinstall,silent"
  420. local -a pkg_install=("${PKG_INSTALL[@]}")
  421. input=$(getopt -o +s -l "$long_opts" -- "$@") || { err "Incorrect options provided"; exit 1; }
  422. eval set -- "$input"
  423. while true; do
  424. case $1 in
  425. --no-install-check) no_install_check=1 ;;
  426. --allow-downgrades) allow_downgrades=1 ;;
  427. --no-gpg-check) no_gpg_check=1 ;;
  428. --refresh) refresh=1 ;;
  429. --reinstall) reinstall=1 ;;
  430. --silent|-s) silent=1 ;;
  431. --) shift; break ;;
  432. esac
  433. shift
  434. done
  435. # Define distribution-specific package aliases
  436. case $ID in
  437. debian|ubuntu) pkg_aliases=(
  438. [rpm-build]="rpm"
  439. [createrepo_c]="createrepo"
  440. [tigervnc-server]="tigervnc-standalone-server"
  441. ) ;;
  442. suse) pkg_aliases=(
  443. [buildah]="buildah fuse-overlayfs"
  444. ) ;;
  445. mandriva) pkg_aliases=(
  446. [dpkg]="dpkg gnutar"
  447. ) ;;
  448. esac
  449. # Filter out already installed packages to create pkg_array
  450. for pkg in "$@"; do
  451. # Use alias if present, otherwise just pkg itself
  452. pkg_names=("$pkg")
  453. if [[ -v pkg_aliases[$pkg] ]]; then
  454. debug "Aliasing $pkg to ${pkg_aliases[$pkg]}"
  455. IFS=' ' read -ra pkg_names <<< "${pkg_aliases[$pkg]}"
  456. fi
  457. for p in "${pkg_names[@]}"; do
  458. if (( no_install_check )) ||
  459. ! { command -v "$p" &>/dev/null || "${PKG_QUERY[@]}" "$p" &>/dev/null; }; then
  460. pkg_array+=("$p")
  461. else
  462. debug "$p is already installed, skipping installation"
  463. fi
  464. done
  465. done
  466. # Generate installation flags based on the distribution
  467. case $ID in
  468. debian|ubuntu)
  469. ((allow_downgrades)) && install_flags+=(--allow-downgrades)
  470. ((reinstall)) && install_flags+=(--reinstall) ;;
  471. fedora|centos|mandriva)
  472. ((allow_downgrades)) && install_flags+=(--allowerasing)
  473. ((no_gpg_check)) && install_flags+=(--nogpgcheck)
  474. ((refresh)) && install_flags+=(--refresh)
  475. # Only add reinstall flag for mediacenter package
  476. if ((reinstall)) && [[ ${#pkg_array[@]} -eq 1 ]] && "${PKG_QUERY[@]}" "mediacenter$MC_MVERSION" &>/dev/null; then
  477. pkg_install=("${pkg_install[@]/install/reinstall}")
  478. fi
  479. ;;
  480. suse)
  481. ((no_gpg_check)) && install_flags+=(--allow-unsigned-rpm) ;;
  482. esac
  483. # Install packages
  484. if [[ ${#pkg_array[@]} -gt 0 ]]; then
  485. if ! execute "${pkg_install[@]}" "${install_flags[@]}" "${pkg_array[@]}"; then
  486. ((silent)) || err "Failed to install ${pkg_array[*]}"
  487. return 1
  488. fi
  489. fi
  490. return 0
  491. }
  492. # @description install host-specific external repos
  493. install_external_repos() {
  494. debug "${FUNCNAME[0]}()"
  495. case $ID in
  496. ubuntu)
  497. if ! grep -E '^deb|^Components' /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -q universe; then
  498. echo "Adding universe repository"
  499. if ! execute sudo add-apt-repository -y universe; then
  500. err "Adding universe repository failed"
  501. fi
  502. fi
  503. ;;
  504. centos)
  505. if ! command -v dpkg &>/dev/null; then
  506. echo "Adding EPEL repository"
  507. if ! install_package epel-release; then
  508. # If epel-release is not available, install it manually
  509. install_package --no-install-check \
  510. "https://dl.fedoraproject.org/pub/epel/epel-release-latest-${VERSION_ID%%.*}.noarch.rpm"
  511. fi
  512. fi
  513. if ! "${PKG_QUERY[@]}" rpmfusion-free-release &>/dev/null; then
  514. echo "Installing the RPMFusion repository"
  515. install_package --no-install-check \
  516. "https://download1.rpmfusion.org/free/el/rpmfusion-free-release-${VERSION_ID%%.*}.noarch.rpm"
  517. fi
  518. # Install mesa-va-drivers-freeworld separately from the RPM using dnf swap
  519. # install_mesa_freeworld # no longer provided by RPMFusion for EL9, etc.
  520. ;;
  521. fedora)
  522. if ! "${PKG_QUERY[@]}" rpmfusion-free-release &>/dev/null; then
  523. echo "Installing the RPMFusion repository"
  524. install_package --no-install-check \
  525. "https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$VERSION_ID.noarch.rpm"
  526. fi
  527. ;;
  528. suse) : # TODO may be needed if X11_XOrg is made unavailable in default repos
  529. # if ! zypper repos | grep -q "X11_XOrg"; then
  530. # echo "Installing the X11 repository"
  531. # execute sudo zypper --non-interactive --quiet addrepo \
  532. # "https://download.opensuse.org/repositories/X11:/XOrg/${NAME// /_}/X11:XOrg.repo"
  533. # execute sudo zypper --non-interactive --quiet refresh
  534. # fi
  535. ;;
  536. mandriva)
  537. local branch
  538. branch=$(grep ^PRETTY_NAME= /etc/os-release | tr -d '"' | rev | cut -d' ' -f1 | rev | tr '[:upper:]' '[:lower:]')
  539. execute sudo dnf config-manager --set-enabled "${branch}-${ARCH}-extra"
  540. ;;
  541. esac
  542. }
  543. # @description Installs mesa-va-drivers-freeworld on Fedora
  544. install_mesa_freeworld() {
  545. debug "${FUNCNAME[0]}()"
  546. local pkg freeworld_pkg
  547. case $ID in
  548. fedora)
  549. for pkg in mesa-va-drivers mesa-vdpau-drivers mesa-vulkan-drivers; do
  550. freeworld_pkg="${pkg}-freeworld"
  551. if ! "${PKG_QUERY[@]}" "$freeworld_pkg" &>/dev/null; then
  552. if "${PKG_QUERY[@]}" "$pkg" &>/dev/null; then
  553. if ! execute sudo dnf swap -y "$pkg" "$freeworld_pkg"; then
  554. err "Package swap failed for $pkg!"
  555. fi
  556. else
  557. "${PKG_INSTALL[@]}" "$freeworld_pkg"
  558. fi
  559. fi
  560. done
  561. ;;
  562. esac
  563. }
  564. # @description Installs JRiver Media Center from a remote repository
  565. install_mc_repo() {
  566. debug "${FUNCNAME[0]}()"
  567. local repo_file repo_text channel
  568. case $ID in
  569. fedora|centos)
  570. repo_file="/etc/yum.repos.d/jriver.repo"
  571. read -r -d '' repo_text <<-EOF
  572. [jriver]
  573. name=JRiver Media Center by BryanC
  574. baseurl=https://repos.bryanroessler.com/jriver
  575. gpgcheck=0
  576. EOF
  577. ;;
  578. debian|ubuntu)
  579. [[ -n $BETAPASS ]] && channel="beta" || channel="latest"
  580. local major_version="${VERSION_ID%%.*}"
  581. local keyfile="/usr/share/keyrings/jriver-com-archive-keyring.gpg"
  582. if [[ ($ID == "ubuntu" && $major_version -ge 24) ||
  583. ($ID == "debian" && (-z $major_version || $major_version -ge 12)) ]]; then
  584. if [[ $channel == "beta" ]]; then
  585. repo_file="/etc/apt/sources.list.d/jriver-beta.sources"
  586. else
  587. repo_file="/etc/apt/sources.list.d/jriver.sources"
  588. fi
  589. # Remove deprecated repo files
  590. old_repo_files=(
  591. "/etc/apt/sources.list.d/jriver.list"
  592. "/etc/apt/sources.list.d/jriver-beta.list"
  593. "/etc/apt/sources.list.d/jriver_beta.list"
  594. )
  595. for f in "${old_repo_files[@]}"; do
  596. [[ -f $f ]] && execute sudo rm -f "$f"
  597. done
  598. read -r -d '' repo_text <<-EOF
  599. Types: deb
  600. URIs: https://dist.jriver.com/$channel/mediacenter/
  601. Signed-By: $keyfile
  602. Suites: $MC_REPO
  603. Components: main
  604. Architectures: amd64 armhf arm64
  605. EOF
  606. else
  607. if [[ $channel == "beta" ]]; then
  608. execute sudo rm -f "/etc/apt/sources.list.d/jriver_beta.list"
  609. repo_file="/etc/apt/sources.list.d/jriver-beta.list"
  610. else
  611. repo_file="/etc/apt/sources.list.d/jriver.list"
  612. fi
  613. repo_text="deb [signed-by=$keyfile arch=amd64,armhf,arm64] https://dist.jriver.com/$channel/mediacenter/ $MC_REPO main"
  614. fi
  615. echo "Installing JRiver Media Center GPG key"
  616. download "https://dist.jriver.com/mediacenter@jriver.com.gpg.key" "-" |
  617. gpg --dearmor | sudo tee "$keyfile" &>/dev/null
  618. ;;
  619. *)
  620. err "An MC repository for $ID is not yet available"
  621. err "Use --install=local to install MC on $ID"
  622. return 1
  623. ;;
  624. esac
  625. if [[ -f $repo_file ]]; then
  626. ask_ok "Replace existing $repo_file repository file" && execute sudo rm -f "$repo_file"
  627. fi
  628. echo "Adding MC repository file: $repo_file"
  629. debug "$repo_text"
  630. sudo tee "$repo_file" &>/dev/null <<< "$repo_text"
  631. # Add older repository for libwebkit2gtk-4.0-37, etc, on newer Debian/Ubuntu
  632. if add_temp_repo; then
  633. trap 'execute sudo rm -f "$TEMP_REPO_FILE"' EXIT
  634. else
  635. err "Failed to add temporary repository"
  636. return 1
  637. fi
  638. "${PKG_UPDATE[@]}" || { err "Package update failed!"; return 1; }
  639. echo "Installing $MC_PKG package"
  640. if ! install_package \
  641. --no-install-check \
  642. --allow-downgrades \
  643. --no-gpg-check \
  644. --reinstall \
  645. "$MC_PKG"; then
  646. err "Package install failed!"
  647. return 1
  648. fi
  649. # Unset the trap and remove temporary legacy repository
  650. trap - EXIT
  651. [[ -f $TEMP_REPO_FILE ]] && execute sudo rm -f "$TEMP_REPO_FILE"
  652. return 0
  653. }
  654. # @description Acquires the source DEB package from JRiver
  655. acquire_deb() {
  656. debug "${FUNCNAME[0]}()"
  657. declare -g MC_DEB MC_SOURCE
  658. local fname mnt
  659. [[ -d $OUTPUT_DIR/SOURCES ]] || execute mkdir -p "$OUTPUT_DIR/SOURCES"
  660. # Usually JRiver excludes the release number from the filename
  661. # but in some cases (test builds) it may be included
  662. if [[ $MC_RELEASE -gt 1 ]]; then
  663. fname="MediaCenter-$MC_VERSION-$MC_RELEASE-$MC_ARCH.deb"
  664. else
  665. fname="MediaCenter-$MC_VERSION-$MC_ARCH.deb"
  666. fi
  667. MC_DEB="$OUTPUT_DIR/SOURCES/$fname"
  668. MC_SOURCE="https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/latest/$fname"
  669. # If deb file already exists, skip download
  670. if [[ -f $MC_DEB ]]; then
  671. if [[ $(stat -c%s "$MC_DEB") -lt 10000000 ]]; then
  672. echo "Removing existing DEB under 10MB: $MC_DEB"
  673. execute rm -f "$MC_DEB"
  674. else
  675. echo "Using existing DEB: $MC_DEB"
  676. return 0
  677. fi
  678. fi
  679. # Download the deb file using the containerized package manager
  680. if ! { create_mc_apt_container "apt-get download --allow-unauthenticated mediacenter$MC_MVERSION &>/dev/null" &&
  681. mnt="$(sudo buildah mount "$CNT")" &&
  682. execute sudo find "$mnt" -maxdepth 1 -type f -name "*.deb" -exec cp {} "$MC_DEB" \; &&
  683. [[ -f $MC_DEB ]] &&
  684. execute sudo buildah umount "$CNT" &&
  685. execute sudo buildah rm "$CNT"; }; then
  686. debug "Failed to download DEB using containerized package manager"
  687. echo "Using legacy download method"
  688. # Define the repository search order
  689. local -a repos
  690. [[ -n $BETAPASS ]] && repos=("https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/beta/$BETAPASS/$fname")
  691. repos+=(
  692. "https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/latest/$fname"
  693. "https://files.jriver-cdn.com/mediacenter/test/$fname")
  694. # Loop through the repositories and attempt to download
  695. for repo in "${repos[@]}"; do
  696. echo "Checking $repo for DEB package"
  697. if download "$repo" "$MC_DEB"; then
  698. echo "Found"
  699. MC_SOURCE="$repo"
  700. break
  701. fi
  702. done
  703. fi
  704. # Return if the download was successful
  705. [[ -f $MC_DEB ]]
  706. }
  707. # @description Creates a SPEC file and builds the RPM from the source DEB using rpmbuild
  708. build_rpm() {
  709. debug "${FUNCNAME[0]}()"
  710. local i rpmbuild_cmd stub
  711. local -a requires recommends
  712. local spec_file="$OUTPUT_DIR/SPECS/mediacenter$MC_MVERSION-$MC_VERSION-$MC_RELEASE-$BUILD_TARGET-$ARCH.spec"
  713. # skip rebuilding the rpm if it already exists
  714. debug "Checking for existing MC RPM: $MC_RPM"
  715. if [[ -f $MC_RPM && -f $spec_file ]]; then
  716. echo "Spec file and $MC_RPM already exists. Skipping build step"
  717. return 0
  718. fi
  719. # Load deb dependencies into array
  720. IFS=',' read -ra requires <<< "$(dpkg-deb -f "$MC_DEB" Depends)"
  721. IFS=',' read -ra recommends <<< "$(dpkg-deb -f "$MC_DEB" Recommends)"
  722. # Clean up formatting
  723. requires=("${requires[@]%%|*}")
  724. requires=("${requires[@]/?:/}")
  725. requires=("${requires[@]# }")
  726. requires=("${requires[@]% }")
  727. requires=("${requires[@]//\(/}")
  728. requires=("${requires[@]//)/}")
  729. recommends=("${recommends[@]%%|*}")
  730. recommends=("${recommends[@]/?:/}")
  731. recommends=("${recommends[@]# }")
  732. recommends=("${recommends[@]% }")
  733. recommends=("${recommends[@]//\(/}")
  734. recommends=("${recommends[@]//)/}")
  735. # Translate package names
  736. case $BUILD_TARGET in
  737. fedora|centos)
  738. requires=("${requires[@]/libc6/glibc}")
  739. requires=("${requires[@]/libasound2/alsa-lib}")
  740. requires=("${requires[@]/libuuid1/libuuid}")
  741. requires=("${requires[@]/libx11-6/libX11}")
  742. requires=("${requires[@]/libxext6/libXext}")
  743. requires=("${requires[@]/libxcb1*/libxcb}") # TODO Remove minimum version for MC31 (*)
  744. requires=("${requires[@]/libxau6/libXau}")
  745. requires=("${requires[@]/libxdmcp6/libXdmcp}")
  746. requires=("${requires[@]/libstdc++6/libstdc++}")
  747. requires=("${requires[@]/libgtk-3-0/gtk3}")
  748. requires=("${requires[@]/libegl1/mesa-libEGL}")
  749. requires=("${requires[@]/libgl1/mesa-libGL}")
  750. requires=("${requires[@]/libgles2/libglvnd-gles}")
  751. requires=("${requires[@]/libgbm1/mesa-libgbm}")
  752. requires=("${requires[@]/libegl-mesa0/mesa-libEGL}")
  753. requires=("${requires[@]/libvulkan1/vulkan-loader}")
  754. requires=("${requires[@]/libpango1.0-0/pango}")
  755. requires=("${requires[@]/libpango-1.0-0/pango}")
  756. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  757. requires=("${requires[@]/libpangox-1.0-0/pango}")
  758. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  759. requires=("${requires[@]/libnss3/nss}")
  760. requires=("${requires[@]/libnspr4/nspr}")
  761. requires=("${requires[@]/libgomp1/libgomp}")
  762. requires=("${requires[@]/libfribidi0/fribidi}")
  763. requires=("${requires[@]/libfontconfig1/fontconfig}")
  764. requires=("${requires[@]/libfreetype6/freetype}")
  765. requires=("${requires[@]/libharfbuzz0b/harfbuzz}")
  766. requires=("${requires[@]/libva2/libva}")
  767. requires=("${requires[@]/libva-drm2/libva}")
  768. requires=("${requires[@]/libepoxy0/libepoxy}")
  769. requires=("${requires[@]/liblcms2-2/lcms2}")
  770. requires=("${requires[@]/libwebkit2gtk-4.0*/webkit2gtk4.0}")
  771. requires=("${requires[@]/libwebkit2gtk-4.1*/webkit2gtk4.1}")
  772. requires=("${requires[@]/libsdbus-c++1/sdbus-cpp}")
  773. recommends=("${recommends[@]/fdkaac/fdk-aac-free}")
  774. recommends+=("mesa-va-drivers-freeworld|mesa-va-drivers")
  775. recommends+=("mesa-vulkan-drivers-freeworld|mesa-vulkan-drivers")
  776. recommends+=("mesa-vdpau-driver-freeworld|mesa-vdpau-driver")
  777. ;;
  778. suse)
  779. requires=("${requires[@]/libc6/glibc}")
  780. requires=("${requires[@]/libasound2/alsa-lib}")
  781. requires=("${requires[@]/libx11-6/libX11-6}")
  782. requires=("${requires[@]/libxext6/libXext6}")
  783. requires=("${requires[@]/libxdmcp6/libXdmcp6}")
  784. requires=("${requires[@]/libgtk-3-0/gtk3}")
  785. requires=("${requires[@]/libgl1/Mesa-libGL1}")
  786. requires=("${requires[@]/libpango-1.0-0/pango}")
  787. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  788. requires=("${requires[@]/libpangox-1.0-0/pango}")
  789. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  790. requires=("${requires[@]/libnss3/mozilla-nss}")
  791. requires=("${requires[@]/libnspr4/mozilla-nspr}")
  792. requires=("${requires[@]/libfribidi0/fribidi}")
  793. requires=("${requires[@]/libfontconfig1/fontconfig}")
  794. requires=("${requires[@]/libharfbuzz0b/libharfbuzz0}")
  795. requires=("${requires[@]/libwebkit2gtk-4.0*/libwebkit2gtk-4_0-37}")
  796. requires=("${requires[@]/libwebkit2gtk-4.1*/libwebkit2gtk-4_1-0}")
  797. for i in "${!requires[@]}"; do
  798. [[ ${requires[$i]} == "mesa-vulkan-drivers" ]] && unset -v 'requires[i]'
  799. [[ ${requires[$i]} == "libsdbus-c++1" ]] && unset -v 'requires[i]'
  800. done
  801. recommends+=(libvulkan1)
  802. recommends+=(libvulkan_intel)
  803. recommends+=(libvulkan_radeon)
  804. recommends+=(libvulkan_nouveau)
  805. ;;
  806. mandriva)
  807. requires=("${requires[@]/libc6/glibc}")
  808. requires=("${requires[@]/libasound2/lib64asound2}")
  809. requires=("${requires[@]/libuuid1/lib64uuid1}")
  810. requires=("${requires[@]/libx11-6/lib64x11_6}")
  811. requires=("${requires[@]/libxext6/lib64xext6}")
  812. requires=("${requires[@]/libxcb1/lib64xcb1}")
  813. requires=("${requires[@]/libxdmcp6/lib64xdmcp6}")
  814. requires=("${requires[@]/libstdc++6/lib64stdc++6}")
  815. requires=("${requires[@]/libgtk-3-0/lib64gtk3_0}")
  816. requires=("${requires[@]/libgl1/lib64GL1}")
  817. requires=("${requires[@]/libgles2/lib64GLESv2_2}")
  818. requires=("${requires[@]/libegl-mesa0/lib64EGL_mesa0}")
  819. requires=("${requires[@]/libpango1.0-0/lib64pango1.0_0}")
  820. requires=("${requires[@]/libpango-1.0-0/lib64pango1.0_0}")
  821. requires=("${requires[@]/libpangoft2-1.0-0/lib64pangoft2_1.0_0}")
  822. requires=("${requires[@]/libpango-cairo-1.0-0/lib64pangocairo1.0_0}")
  823. requires=("${requires[@]/libpangoxft-1.0-0/lib64pangoxft1.0_0}")
  824. requires=("${requires[@]/libnss3/lib64nss3}")
  825. requires=("${requires[@]/libnspr4/lib64nspr4}")
  826. requires=("${requires[@]/libgomp1/lib64gomp1}")
  827. requires=("${requires[@]/libfribidi0/lib64fribidi0}")
  828. requires=("${requires[@]/libfontconfig1/lib64fontconfig}")
  829. requires=("${requires[@]/libfreetype6/lib64freetype6}")
  830. requires=("${requires[@]/libharfbuzz0b/lib64harfbuzz}")
  831. requires=("${requires[@]/libgbm1/lib64gbm1}")
  832. requires=("${requires[@]/libva2/lib64va2}")
  833. requires=("${requires[@]/libva-drm2/lib64va-drm2}")
  834. requires=("${requires[@]/libvulkan1/lib64vulkan1}")
  835. requires=("${requires[@]/mesa-vulkan-drivers/lib64dri-drivers}")
  836. requires=("${requires[@]/vulkan-icd/vulkan-loader}")
  837. requires=("${requires[@]/libwebkit2gtk-4.1-0/lib64webkit2gtk4.1}")
  838. recommends=("${recommends[@]/musepack-tools/mppenc}")
  839. for i in "${!recommends[@]}"; do
  840. [[ ${recommends[$i]} == "fdkaac" ]] && unset -v 'recommends[i]'
  841. done
  842. ;;
  843. esac
  844. # Convert array to newline delim'd string (for heredoc)
  845. printf -v requires "Requires: %s\n" "${requires[@]}"
  846. printf -v recommends "Recommends: %s\n" "${recommends[@]}"
  847. # Strip last newline
  848. requires="${requires%?}"
  849. recommends="${recommends%?}"
  850. if ((COMPAT_SWITCH)); then
  851. # Strip minimum versions
  852. requires=$(echo "$requires" | awk -F" " 'NF == 4 {print $1 " " $2} NF != 4 {print $0}')
  853. fi
  854. # Exclude MC stub executable <= MC31
  855. if [[ $MC_MVERSION -le 31 ]]; then
  856. stub=""
  857. else
  858. stub="%{_bindir}/mc$MC_MVERSION"
  859. fi
  860. # Create spec file
  861. cat <<-EOF > "$spec_file"
  862. Name: mediacenter$MC_MVERSION
  863. Version: $MC_VERSION
  864. Release: $MC_RELEASE
  865. Summary: JRiver Media Center
  866. Group: Applications/Media
  867. Source0: $MC_SOURCE
  868. %define _rpmfilename %%{ARCH}/%%{NAME}-%%{version}-%%{release}.%%{ARCH}.rpm
  869. AutoReq: 0
  870. $requires
  871. $recommends
  872. Conflicts: MediaCenter
  873. Provides: mediacenter$MC_MVERSION
  874. License: Copyright 1998-$(date +%Y), JRiver, Inc. All rights reserved. Protected by U.S. patents #7076468 and #7062468
  875. URL: https://www.jriver.com/
  876. %define __provides_exclude_from ^%{_libdir}/jriver/.*/.*\\.so.*$
  877. %description
  878. Media Center is more than a world class player.
  879. %global __os_install_post %{nil}
  880. %prep
  881. %build
  882. %install
  883. dpkg -x %{S:0} %{buildroot}
  884. %post -p /sbin/ldconfig
  885. %postun -p /sbin/ldconfig
  886. %files
  887. %{_bindir}/mediacenter$MC_MVERSION
  888. $stub
  889. %{_libdir}/jriver
  890. %{_datadir}
  891. %exclude %{_datadir}/applications/media_center_packageinstaller_$MC_MVERSION.desktop
  892. /etc/security/limits.d/*
  893. EOF
  894. # Run rpmbuild
  895. echo "Building $MC_RPM, this may take some time"
  896. rpmbuild_cmd=(
  897. rpmbuild
  898. --define="_topdir $OUTPUT_DIR"
  899. --define="_libdir /usr/lib"
  900. --target="$ARCH"
  901. -bb
  902. "$spec_file"
  903. )
  904. execute "${rpmbuild_cmd[@]}" && [[ -f $MC_RPM ]]
  905. }
  906. # @description Installs Media Center via DEB package w/ optional compatability fixes
  907. install_mc_deb() {
  908. debug "${FUNCNAME[0]}()" "$@"
  909. if ((COMPAT_SWITCH)); then
  910. local extract_dir; extract_dir="$(mktemp -d)"
  911. pushd "$extract_dir" &>/dev/null || return
  912. command -v ar &>/dev/null || { install_package binutils || return 1; }
  913. execute ar x "$MC_DEB"
  914. execute tar xJf "control.tar.xz"
  915. # Remove minimum version specifiers from control file
  916. execute sed -i 's/ ([^)]*)//g' control
  917. # Remove libwebkit2gtk and their fantastic package versioning strategy
  918. execute sed -E -i 's/,[[:space:]]*libwebkit2gtk[^,]*(,|\?)?//g' control
  919. # TODO workaround for legacy ZorinOS
  920. if [[ $ID == "ubuntu" && ${VERSION_ID%.*} -le 16 ]] &&
  921. grep -q zorin /etc/os-release; then
  922. execute sed -i 's/libva2/libva1/g' control
  923. fi
  924. execute tar -cJf "control.tar.xz" "control" "postinst"
  925. declare -g MC_DEB="${MC_DEB/.deb/.compat.deb}"
  926. execute ar rcs "$MC_DEB" "debian-binary" "control.tar.xz" "data.tar.xz"
  927. popd &>/dev/null || return
  928. execute rm -rf "$extract_dir"
  929. fi
  930. # Add older repository for libwebkit2gtk-4.0-37, etc, on newer Debian/Ubuntu
  931. if add_temp_repo; then
  932. trap 'execute sudo rm -f "$TEMP_REPO_FILE"' EXIT
  933. else
  934. err "Failed to add temporary repository"
  935. return 1
  936. fi
  937. "${PKG_UPDATE[@]}" || { err "Package update failed!"; return 1; }
  938. # Copy the DEB to a temporary file so _apt can read it
  939. debug "Creating temporary deb file owned by _apt"
  940. local temp_deb
  941. temp_deb=$(mktemp --suffix=.deb)
  942. execute sudo cp "$MC_DEB" "$temp_deb"
  943. id _apt &>/dev/null && execute sudo chown _apt "$temp_deb"
  944. # Use --reinstall to make sure local package is installed over repo package
  945. if ! install_package \
  946. --no-install-check \
  947. --no-gpg-check \
  948. --allow-downgrades \
  949. --reinstall \
  950. "$temp_deb"; then
  951. err "Local MC DEB installation failed"
  952. if ask_ok "Remove source DEB and retry"; then
  953. execute sudo rm -f "$MC_DEB" "$temp_deb"
  954. exec "$SCRIPT_PATH" "$@" "--no-update"
  955. fi
  956. fi
  957. # Unset the trap and remove temporary legacy repository
  958. trap - EXIT
  959. [[ -f $TEMP_REPO_FILE ]] && execute sudo rm -f "$TEMP_REPO_FILE"
  960. execute sudo rm -f "$temp_deb"
  961. return 0
  962. }
  963. # @description Installs MC via RPM package
  964. install_mc_rpm() {
  965. debug "${FUNCNAME[0]}()"
  966. install_package --no-install-check --no-gpg-check --allow-downgrades --reinstall "$MC_RPM"
  967. }
  968. # @description Installs Media Center generically for unsupported OSes
  969. install_mc_generic() {
  970. debug "${FUNCNAME[0]}()"
  971. local extract_dir
  972. local -a raw_files
  973. echo "Using generic installation method"
  974. extract_dir="$(mktemp -d)"
  975. pushd "$extract_dir" &>/dev/null || return
  976. execute ar x "$MC_DEB"
  977. execute tar xJf "control.tar.xz"
  978. echo "You must install the following dependencies manually:"
  979. grep -i "Depends:" control
  980. readarray -t raw_files < <(tar xJvf data.tar.xz)
  981. # Output to log file
  982. for f in "${raw_files[@]/#./}"; do
  983. echo "$f" >> "$SCRIPT_DIR/.uninstall"
  984. done
  985. # Manually install files
  986. for f in "${raw_files[@]}"; do
  987. execute sudo cp -a "$f" "${f/#./}"
  988. done
  989. popd &>/dev/null || return
  990. execute rm -rf "$extract_dir"
  991. return 0
  992. }
  993. # @description Installs MC via PKGBUILD
  994. install_mc_arch() {
  995. debug "${FUNCNAME[0]}()"
  996. [[ -d $OUTPUT_DIR/PKGBUILD ]] || execute mkdir -p "$OUTPUT_DIR/PKGBUILD"
  997. cat <<-EOF > "$OUTPUT_DIR/PKGBUILD/mediacenter.pkgbuild"
  998. pkgname=mediacenter$MC_MVERSION
  999. pkgver=$MC_VERSION
  1000. pkgrel=$MC_RELEASE
  1001. pkgdesc="The Most Comprehensive Media Software"
  1002. arch=("$ARCH")
  1003. url="https://www.jriver.com/"
  1004. license=('custom')
  1005. depends=('alsa-lib' 'ca-certificates' 'gtk3' 'gcc-libs' 'libx11' 'libxext' 'libxcb' 'libxau' 'libxdmcp' 'util-linux' 'mesa-libgl' 'webkit2gtk')
  1006. optdepends=(
  1007. 'mesa-libgl: nouveau video support'
  1008. 'nvidia-libgl: nvidia video support'
  1009. 'nvidia-utils: nvidia vulkan support'
  1010. 'vulkan-intel: intel vulkan support'
  1011. 'vulkan-radeon: amd vulkan support'
  1012. 'vorbis-tools: ogg vorbis support'
  1013. 'musepack-tools: musepack support'
  1014. )
  1015. source=("$MC_SOURCE")
  1016. package() {
  1017. cd "\$srcdir"
  1018. bsdtar xf data.tar.xz -C "\$pkgdir"
  1019. }
  1020. EOF
  1021. pushd "$OUTPUT_DIR/PKGBUILD" &>/dev/null || return
  1022. if ! execute makepkg \
  1023. --install \
  1024. --syncdeps \
  1025. --clean \
  1026. --cleanbuild \
  1027. --skipinteg \
  1028. --force \
  1029. --noconfirm \
  1030. -p mediacenter.pkgbuild; then
  1031. err "makepkg failed"; exit 1
  1032. fi
  1033. popd &>/dev/null || return
  1034. }
  1035. # @description Copy the RPM to createrepo-webroot and run createrepo as the createrepo-user
  1036. run_createrepo() {
  1037. debug "${FUNCNAME[0]}()"
  1038. install_package createrepo_c
  1039. # Ensure the webroot exists
  1040. if [[ ! -d $CREATEREPO_WEBROOT ]]; then
  1041. if ! execute sudo -u "$CREATEREPO_USER" mkdir -p "$CREATEREPO_WEBROOT"; then
  1042. if ! (execute sudo mkdir -p "$CREATEREPO_WEBROOT" ||
  1043. execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"); then
  1044. err "Could not create the createrepo-webroot path!"
  1045. err "Make sure that the webroot $CREATEREPO_WEBROOT is writable by user $CREATEREPO_USER"
  1046. err "Or change the repo ownership with --createrepo-user"
  1047. return 1
  1048. fi
  1049. fi
  1050. fi
  1051. # Copy built RPMs to webroot
  1052. if ! execute sudo cp -nf "$MC_RPM" "$CREATEREPO_WEBROOT" ||
  1053. ! execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"; then
  1054. err "Could not copy $MC_RPM to $CREATEREPO_WEBROOT"
  1055. return 1
  1056. fi
  1057. # Run createrepo
  1058. local -a cr_opts=(-q "$CREATEREPO_WEBROOT")
  1059. # [[ -d "$CREATEREPO_WEBROOT/repodata" ]] && cr_opts+=(--update) # TODO temporarily disabled for legacy createrepo
  1060. if ! execute sudo -u "$CREATEREPO_USER" createrepo "${cr_opts[@]}"; then
  1061. if ! (execute sudo createrepo "${cr_opts[@]}" && execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"); then
  1062. err "createrepo failed"
  1063. return 1
  1064. fi
  1065. fi
  1066. }
  1067. # @description Symlink certificates if they do not exist in default location
  1068. link_ssl_certs() {
  1069. debug "${FUNCNAME[0]}()"
  1070. local target_cert f
  1071. local mc_cert_link="$MC_ROOT/ca-certificates.crt"
  1072. local -a source_certs=(
  1073. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"
  1074. "/var/lib/ca-certificates/ca-bundle.pem"
  1075. "$MC_ROOT/local-ca-certificates.crt")
  1076. target_cert=$(readlink -f "$mc_cert_link")
  1077. [[ -f $target_cert ]] && return 0
  1078. for f in "${source_certs[@]}"; do
  1079. if [[ -f $f ]]; then
  1080. if execute sudo ln -fs "$f" "$mc_cert_link"; then
  1081. debug "Symlinked $mc_cert_link to $f"
  1082. return 0
  1083. fi
  1084. fi
  1085. done
  1086. err "Certificate symlinking failed"; return 1
  1087. }
  1088. # @description Restore the mjr license file from MJR_FILE or other common locations
  1089. restore_license() {
  1090. debug "${FUNCNAME[0]}()"
  1091. local newest f
  1092. local -a mjrfiles
  1093. # Glob mjr files from common directories
  1094. shopt -s nullglob
  1095. mjrfiles=(
  1096. "$SCRIPT_DIR"/*.mjr
  1097. "$OUTPUT_DIR"/*.mjr
  1098. "$HOME"/[dD]ownloads/*.mjr
  1099. "$HOME"/[dD]ocuments/*.mjr
  1100. )
  1101. shopt -u nullglob
  1102. if [[ ${#mjrfiles[@]} -gt 0 ]]; then
  1103. debug "mjrfiles=(${mjrfiles[*]})"
  1104. # Sort globbed files by time, newest first
  1105. newest=${mjrfiles[0]}
  1106. for f in "${mjrfiles[@]}"; do
  1107. if [[ -f $f && $f -nt $newest ]]; then
  1108. newest=$f
  1109. fi
  1110. done
  1111. debug "Latest mjrfile: $newest"
  1112. for f in "$MJR_FILE" "$newest"; do
  1113. if [[ -f $f ]]; then
  1114. if execute "mediacenter$MC_MVERSION" "/RestoreFromFile" "$f"; then
  1115. echo "Restored license from $f"
  1116. return 0
  1117. else
  1118. err "Failed to restore license from $f"
  1119. fi
  1120. fi
  1121. done
  1122. fi
  1123. }
  1124. # @description Opens ports using the system firewall tool
  1125. # @arg $1 string Service name
  1126. # @arg $2 array List of ports in firewall-cmd format
  1127. open_firewall() {
  1128. debug "${FUNCNAME[0]}()" "$@"
  1129. local service="$1"
  1130. shift
  1131. local -a f_ports=("$@") # for firewall-cmd
  1132. local u_ports="$*"
  1133. u_ports="${u_ports// /|}" # concatenate
  1134. u_ports="${u_ports//-/\:}" # for ufw
  1135. local port
  1136. if command -v firewall-cmd &>/dev/null; then
  1137. if ! sudo firewall-cmd --get-services | grep -q "$service"; then
  1138. execute sudo firewall-cmd --permanent "--new-service=$service"
  1139. execute sudo firewall-cmd --permanent "--service=$service" "--set-description=$service installed by installJRMC"
  1140. execute sudo firewall-cmd --permanent "--service=$service" "--set-short=$service"
  1141. for port in "${f_ports[@]}"; do
  1142. execute sudo firewall-cmd --permanent "--service=$service" "--add-port=$port"
  1143. done
  1144. execute sudo firewall-cmd --add-service "$service" --permanent
  1145. execute sudo firewall-cmd --reload
  1146. fi
  1147. elif command -v ufw &>/dev/null; then
  1148. sudo bash -c "cat <<-EOF > /etc/ufw/applications.d/$service
  1149. [$service]
  1150. title=$service
  1151. description=$service installed by installJRMC
  1152. ports=$u_ports
  1153. EOF"
  1154. execute sudo ufw app update "$service"
  1155. execute sudo ufw allow "$service"
  1156. else
  1157. echo "Warning: Install firewall-cmd or ufw to open firewall ports"
  1158. return 1
  1159. fi
  1160. }
  1161. # @description Create the xvnc or x11vnc password file
  1162. # @arg $1 string Service type (xvnc, x11vnc)
  1163. set_vnc_pass() {
  1164. debug "${FUNCNAME[0]}()"
  1165. local vncpassfile="$HOME/.vnc/jrmc_passwd"
  1166. [[ -d ${vncpassfile%/*} ]] || execute mkdir -p "${vncpassfile%/*}"
  1167. if [[ -f $vncpassfile ]]; then
  1168. if [[ ! -v VNCPASS ]]; then
  1169. err "Refusing to overwrite existing $vncpassfile with an empty password"
  1170. err "Remove existing $vncpassfile or use --vncpass ''"
  1171. return 1
  1172. else
  1173. execute rm -f "$vncpassfile"
  1174. fi
  1175. fi
  1176. if [[ -v VNCPASS ]]; then
  1177. if [[ $1 == "xvnc" ]]; then
  1178. echo "$VNCPASS" | vncpasswd -f > "$vncpassfile"
  1179. elif [[ $1 == "x11vnc" ]]; then
  1180. execute x11vnc -storepasswd "$VNCPASS" "$vncpassfile"
  1181. fi
  1182. return
  1183. else
  1184. declare -gi NOVNCAUTH=1
  1185. fi
  1186. }
  1187. # @description Set display and port variables
  1188. set_display_vars() {
  1189. debug "${FUNCNAME[0]}()"
  1190. declare -g THIS_DISPLAY THIS_DISPLAY_NUM NEXT_DISPLAY
  1191. # Check USER_DISPLAY, else environment DISPLAY, else set to :0
  1192. THIS_DISPLAY="${USER_DISPLAY:-${DISPLAY:-:0}}"
  1193. THIS_DISPLAY_NUM="${THIS_DISPLAY#*:}" # strip prefix
  1194. THIS_DISPLAY_NUM="${THIS_DISPLAY_NUM%%.*}" # strip suffix
  1195. # Increment each time we run this
  1196. if ((NEXT_DISPLAY_NUM)); then
  1197. declare -g NEXT_DISPLAY_NUM=$((NEXT_DISPLAY_NUM + 1))
  1198. else
  1199. declare -g NEXT_DISPLAY_NUM=$((THIS_DISPLAY_NUM + 1))
  1200. fi
  1201. NEXT_DISPLAY=":$NEXT_DISPLAY_NUM"
  1202. }
  1203. # @description Create associated service variables based on service name
  1204. # @arg $1 string Service name
  1205. set_service_vars() {
  1206. debug "${FUNCNAME[0]}()" "$@"
  1207. declare -g SERVICE_NAME SERVICE_FNAME TIMER_NAME TIMER_FNAME
  1208. declare -g USER_STRING GRAPHICAL_TARGET
  1209. declare -ga RELOAD ENABLE DISABLE IS_ENABLED IS_ACTIVE
  1210. local -a systemctl_prefix
  1211. local service_name="$1"
  1212. local service_type="${SERVICE_TYPE:-${2:-system}}"
  1213. local service_dir="/usr/lib/systemd/$service_type"
  1214. if [[ $USER == "root" && $service_type == "user" ]]; then
  1215. err "Trying to install user service as root"
  1216. err "Use --service-type service and/or execute installJRMC as non-root user"
  1217. return 1
  1218. fi
  1219. if [[ $service_type == "system" ]]; then
  1220. systemctl_prefix=(sudo systemctl)
  1221. GRAPHICAL_TARGET="graphical.target"
  1222. elif [[ $service_type == "user" ]]; then
  1223. systemctl_prefix=(systemctl --user)
  1224. GRAPHICAL_TARGET="default.target"
  1225. fi
  1226. # systemctl commands
  1227. RELOAD=("${systemctl_prefix[@]}" daemon-reload)
  1228. ENABLE=("${systemctl_prefix[@]}" enable --now)
  1229. DISABLE=("${systemctl_prefix[@]}" disable --now)
  1230. IS_ENABLED=("${systemctl_prefix[@]}" is-enabled --quiet)
  1231. IS_ACTIVE=("${systemctl_prefix[@]}" is-active --quiet)
  1232. [[ -d $service_dir ]] || execute sudo mkdir -p "$service_dir"
  1233. if [[ $service_type == "system" && $USER != "root" ]]; then
  1234. SERVICE_FNAME="$service_dir/$service_name@.service"
  1235. TIMER_FNAME="$service_dir/$service_name@.timer"
  1236. SERVICE_NAME="$service_name@$USER.service"
  1237. TIMER_NAME="$service_name@$USER.timer"
  1238. USER_STRING="User=%I"
  1239. else
  1240. SERVICE_NAME="$service_name.service"
  1241. TIMER_NAME="$service_name.timer"
  1242. SERVICE_FNAME="$service_dir/$SERVICE_NAME"
  1243. TIMER_FNAME="$service_dir/${TIMER_NAME}"
  1244. USER_STRING=""
  1245. fi
  1246. }
  1247. # @section Services
  1248. # @description Starts and enables (at startup) a JRiver Media Center service
  1249. # @arg $1 string Passes arguments as startup options to /usr/bin/mediacenter??
  1250. service_jriver-mediacenter() {
  1251. debug "${FUNCNAME[0]}()"
  1252. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1253. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1254. [Unit]
  1255. Description=JRiver Media Center $MC_MVERSION
  1256. After=$GRAPHICAL_TARGET
  1257. [Service]
  1258. Type=simple
  1259. $USER_STRING
  1260. ExecStart=/usr/bin/mediacenter$MC_MVERSION $*
  1261. KillMode=none
  1262. ExecStop=/usr/bin/mc$MC_MVERSION /MCC 20007
  1263. Restart=always
  1264. RestartSec=10
  1265. TimeoutStopSec=30
  1266. [Install]
  1267. WantedBy=$GRAPHICAL_TARGET
  1268. EOF"
  1269. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1270. "${RELOAD[@]}" &&
  1271. "${ENABLE[@]}" "$SERVICE_NAME"
  1272. }
  1273. # @description Starts and enables (at startup) a JRiver Media Server service
  1274. service_jriver-mediaserver() {
  1275. debug "${FUNCNAME[0]}()"
  1276. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1277. service_jriver-mediacenter "/MediaServer"
  1278. }
  1279. # @description Starts and enables (at startup) JRiver Media Center in a new Xvnc session
  1280. # TODO https://github.com/TigerVNC/tigervnc/blob/master/unix/vncserver/HOWTO.md
  1281. service_jriver-xvnc() {
  1282. debug "${FUNCNAME[0]}()"
  1283. local -a start_cmd
  1284. set_service_vars "${FUNCNAME[0]##*_}" "system"
  1285. set_display_vars
  1286. declare -g PORT=$((NEXT_DISPLAY_NUM + 5900))
  1287. install_package tigervnc-server
  1288. set_vnc_pass xvnc
  1289. start_cmd=(
  1290. /usr/bin/vncserver "$NEXT_DISPLAY"
  1291. -geometry 1440x900
  1292. -alwaysshared
  1293. -autokill
  1294. -xstartup "/usr/bin/mediacenter$MC_MVERSION"
  1295. )
  1296. if ((NOVNCAUTH)); then
  1297. start_cmd+=(
  1298. -name "jriver$NEXT_DISPLAY"
  1299. -SecurityTypes None)
  1300. else
  1301. start_cmd+=(
  1302. -rfbauth "$HOME/.vnc/jrmc_passwd")
  1303. fi
  1304. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1305. [Unit]
  1306. Description=Remote desktop service (VNC)
  1307. After=multi-user.target
  1308. [Service]
  1309. Type=forking
  1310. $USER_STRING
  1311. ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill $NEXT_DISPLAY &>/dev/null || :'
  1312. ExecStart=${start_cmd[*]}
  1313. ExecStop=/usr/bin/vncserver -kill $NEXT_DISPLAY
  1314. Restart=always
  1315. [Install]
  1316. WantedBy=multi-user.target
  1317. EOF"
  1318. "${RELOAD[@]}"
  1319. if ! "${ENABLE[@]}" "$SERVICE_NAME"; then
  1320. err "vncserver failed to start on DISPLAY $NEXT_DISPLAY"
  1321. # Allow to increment 10 times before breaking
  1322. max=$((THIS_DISPLAY_NUM + 10))
  1323. while [[ $NEXT_DISPLAY_NUM -lt $max ]]; do
  1324. echo "Incrementing DISPLAY and retrying"
  1325. service_jriver-xvnc && return
  1326. done
  1327. return 1
  1328. else
  1329. echo "Xvnc running on localhost:$PORT"
  1330. open_firewall "jriver-xvnc" "$PORT/tcp"
  1331. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1332. return 0
  1333. fi
  1334. }
  1335. # @description Starts and enables (at startup) x11vnc screen sharing for the local desktop
  1336. service_jriver-x11vnc() {
  1337. debug "${FUNCNAME[0]}()"
  1338. local -a start_cmd
  1339. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1340. set_display_vars
  1341. declare -g PORT=$((THIS_DISPLAY_NUM + 5900))
  1342. install_package x11vnc
  1343. set_vnc_pass x11vnc
  1344. # If .Xauthority file is missing, generate a dummy for x11vnc -auth guess
  1345. if [[ ! -f "$HOME/.Xauthority" ]]; then
  1346. [[ $XDG_SESSION_TYPE == "wayland" ]] &&
  1347. ask_ok "Unsupported Wayland session detected for x11vnc, continue?" || return 1
  1348. debug "Generating $HOME/.Xauthority"
  1349. execute touch "$HOME/.Xauthority"
  1350. execute chmod 644 "$HOME/.Xauthority"
  1351. xauth generate "$DISPLAY" . trusted
  1352. xauth add "$HOST$DISPLAY" . "$(xxd -l 16 -p /dev/urandom)"
  1353. fi
  1354. start_cmd=(
  1355. /usr/bin/x11vnc
  1356. -display "$DISPLAY"
  1357. -noscr
  1358. -auth guess
  1359. -forever
  1360. -bg
  1361. )
  1362. if ((NOVNCAUTH)); then
  1363. start_cmd+=(-nopw)
  1364. else
  1365. start_cmd+=(-rfbauth "$HOME/.vnc/jrmc_passwd")
  1366. fi
  1367. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1368. [Unit]
  1369. Description=x11vnc
  1370. After=$GRAPHICAL_TARGET
  1371. [Service]
  1372. $USER_STRING
  1373. Type=forking
  1374. Environment=DISPLAY=$DISPLAY
  1375. ExecStart=${start_cmd[*]}
  1376. Restart=always
  1377. RestartSec=10
  1378. [Install]
  1379. WantedBy=$GRAPHICAL_TARGET
  1380. EOF"
  1381. open_firewall "jriver-x11vnc" "$PORT/tcp"
  1382. "${RELOAD[@]}" &&
  1383. "${ENABLE[@]}" "$SERVICE_NAME" &&
  1384. echo "x11vnc running on localhost:$PORT"
  1385. }
  1386. # @description Starts and enables (at startup) an hourly service to build the latest version of
  1387. # JRiver Media Center RPM from the source DEB and create/update an RPM repository
  1388. service_jriver-createrepo() {
  1389. debug "${FUNCNAME[0]}()"
  1390. if [[ $CREATEREPO_USER != "$USER" ]]; then
  1391. USER="root" set_service_vars "${FUNCNAME[0]##*_}" "system"
  1392. else
  1393. set_service_vars "${FUNCNAME[0]##*_}" "system"
  1394. fi
  1395. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1396. [Unit]
  1397. Description=Builds JRiver Media Center RPM, moves it to the repo dir, and runs createrepo
  1398. [Service]
  1399. $USER_STRING
  1400. ExecStart=$SCRIPT_DIR/installJRMC --outputdir=$OUTPUT_DIR --createrepo=$CREATEREPO_TARGET \
  1401. --createrepo-webroot=$CREATEREPO_WEBROOT --createrepo-user=$CREATEREPO_USER --mcrepo=$MC_REPO --yes --no-update
  1402. [Install]
  1403. WantedBy=multi-user.target
  1404. EOF"
  1405. sudo bash -c "cat <<-EOF > $TIMER_FNAME
  1406. [Unit]
  1407. Description=Run JRiver MC rpmbuild hourly
  1408. [Timer]
  1409. OnCalendar=hourly
  1410. Persistent=true
  1411. [Install]
  1412. WantedBy=timers.target
  1413. EOF"
  1414. "${RELOAD[@]}" &&
  1415. "${ENABLE[@]}" "$TIMER_NAME"
  1416. }
  1417. # @description Detects if MC is installed on btrfs and disables CoW
  1418. fix_dotjriver_permissions() {
  1419. debug "${FUNCNAME[0]}()"
  1420. # Ensure the user owns the .jriver directory
  1421. [[ -d "$HOME/.jriver" ]] && execute sudo chown -R "$USER:$USER" "$HOME/.jriver"
  1422. }
  1423. # @description Completely uninstalls MC, services, and firewall rules
  1424. uninstall() {
  1425. debug "${FUNCNAME[0]}()"
  1426. local service type unit f
  1427. if ! ask_ok "Do you really want to uninstall JRiver Media Center?"; then
  1428. echo "Uninstall cancelled"
  1429. return 1
  1430. fi
  1431. echo "Stopping and removing all Media Center services"
  1432. for service in $(compgen -A "function" "service"); do
  1433. service="${service##service_}"
  1434. for type in user system; do
  1435. set_service_vars "$service" "$type";
  1436. for unit in "$SERVICE_NAME" "$TIMER_NAME"; do
  1437. if "${IS_ACTIVE[@]}" "$unit" ||
  1438. "${IS_ENABLED[@]}" "$unit" &>/dev/null; then
  1439. "${DISABLE[@]}" "$unit"
  1440. fi
  1441. done
  1442. for f in "$SERVICE_FNAME" "$TIMER_FNAME"; do
  1443. [[ -f $f ]] && execute sudo rm -f "$f"
  1444. done
  1445. "${RELOAD[@]}"
  1446. done
  1447. done
  1448. # Remove the repository files
  1449. for file in "/etc/yum.repos.d/jriver.repo" /etc/apt/sources.list.d/{jriver,mediacenter}*.{list,sources}; do
  1450. if [[ -e $file ]]; then
  1451. echo "Removing repository file: $file"
  1452. execute sudo rm -f "$file"
  1453. fi
  1454. done
  1455. [[ $ID == "suse" ]] && execute sudo zypper --non-interactive removerepo jriver
  1456. echo "Removing firewall rules"
  1457. for service in jriver-mediacenter jriver-xvnc jriver-x11vnc; do
  1458. if command -v firewall-cmd &>/dev/null; then
  1459. execute sudo firewall-cmd --permanent --remove-service=$service
  1460. execute sudo firewall-cmd --permanent --delete-service=$service
  1461. execute sudo firewall-cmd --reload
  1462. elif command -v ufw &>/dev/null; then
  1463. execute sudo ufw delete allow $service
  1464. [[ -f /etc/ufw/applications.d/$service ]] &&
  1465. execute sudo rm -f /etc/ufw/applications.d/$service
  1466. fi
  1467. done
  1468. echo "Uninstalling the JRiver Media Center package"
  1469. if "${PKG_REMOVE[@]}" "${MC_PKG%%=*}"; then # strip version specifier
  1470. echo "Successfully uninstalled the ${MC_PKG%%=*} package"
  1471. elif [[ $? -eq 100 ]]; then
  1472. err "JRiver Media Center package '${MC_PKG%%=*}' is not present and was not uninstalled"
  1473. else
  1474. err "Could not remove Media Center package"
  1475. fi
  1476. local keyfile="/usr/share/keyrings/jriver-com-archive-keyring.gpg"
  1477. if [[ -f $keyfile ]]; then
  1478. echo "Removing the JRiver Media Center GPG key"
  1479. execute sudo rm -f "$keyfile"
  1480. fi
  1481. if [[ -f $SCRIPT_DIR/.uninstall ]]; then
  1482. echo "Removing files from .uninstall log"
  1483. while read -r p; do
  1484. [[ -d $p ]] && execute sudo rm -rf "$p"
  1485. done < "$SCRIPT_DIR/.uninstall"
  1486. mv "$SCRIPT_DIR/.uninstall" "$SCRIPT_DIR/.uninstall.bk"
  1487. fi
  1488. if [[ -d $OUTPUT_DIR ]]; then
  1489. if ask_ok "Remove installJRMC output directory $OUTPUT_DIR?"; then
  1490. execute sudo rm -rf "$OUTPUT_DIR"
  1491. fi
  1492. fi
  1493. if [[ -d $MC_ROOT ]]; then
  1494. if ask_ok "Remove MC installation directory $MC_ROOT?"; then
  1495. execute sudo rm -rf "$MC_ROOT"
  1496. fi
  1497. fi
  1498. if [[ -d $HOME/.jriver ]]; then
  1499. if ask_ok "Backup and reset your MC library?"; then
  1500. execute mv "$HOME/.jriver" "$HOME/.jriver.bk"
  1501. echo "Your MC library has been reset and backed up to $HOME/.jriver.bk"
  1502. echo "To restore your MC library: mv $HOME/.jriver.bk $HOME/.jriver"
  1503. return
  1504. fi
  1505. echo "To reset and backup your MC library: mv $HOME/.jriver $HOME/.jriver.bk"
  1506. echo "To remove your MC library: rm -rf $HOME/.jriver"
  1507. fi
  1508. }
  1509. # @description Checks for installJRMC update and re-executes, if necessary
  1510. update() {
  1511. debug "${FUNCNAME[0]}()" "$@"
  1512. debug "Checking for installJRMC update"
  1513. # Extract and normalize version from a script
  1514. extract_version() {
  1515. local version_line
  1516. version_line=$(grep -m 1 'SCRIPT_VERSION=' "$1")
  1517. version_line=${version_line#*=}
  1518. version_line=${version_line#\"}
  1519. version_line=${version_line%-dev\"}
  1520. version_line=${version_line%\"}
  1521. echo "$version_line"
  1522. }
  1523. # Compare semantic version strings
  1524. version_greater() {
  1525. [[ "$(echo -e "$1\n$2" | sort -V | head -n 1)" != "$1" ]]
  1526. }
  1527. # Check if we're in a git directory and if it's the installJRMC repository
  1528. if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null &&
  1529. [[ "$(git -C "$SCRIPT_DIR" config --get remote.origin.url)" =~ installJRMC|installjrmc ]]; then
  1530. # Get the current commit hash
  1531. local before_pull_hash
  1532. before_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
  1533. # Stash local changes before pull
  1534. execute git -C "$SCRIPT_DIR" stash push --quiet
  1535. # Pull latest changes
  1536. execute git -C "$SCRIPT_DIR" pull --quiet
  1537. # Restore local changes
  1538. execute git -C "$SCRIPT_DIR" stash pop --quiet
  1539. # If the commit hash has changed, an update occurred
  1540. if [[ "$before_pull_hash" != $(git -C "$SCRIPT_DIR" rev-parse HEAD) ]]; then
  1541. echo "Detected installJRMC update, restarting"
  1542. exec "$SCRIPT_PATH" "$@" "--no-update"
  1543. fi
  1544. else
  1545. debug "Not in the installJRMC repository, checking for installJRMC update via webscrape."
  1546. local tmp
  1547. tmp=$(mktemp) || { err "Failed to create temporary file."; return 1; }
  1548. # Acquire the latest version of the script
  1549. if ! download "$SCRIPT_URL" "$tmp"; then
  1550. err "Failed to download the latest script."
  1551. execute rm -f "$tmp"
  1552. return 1
  1553. fi
  1554. # Extract the latest version number
  1555. local remote_version
  1556. remote_version=$(extract_version "$tmp")
  1557. if [[ -z "$remote_version" ]]; then
  1558. err "Failed to extract version from the downloaded script."
  1559. execute rm -f "$tmp"
  1560. return 1
  1561. fi
  1562. # Compare versions and update if the remote version is greater
  1563. if version_greater "$remote_version" "$SCRIPT_VERSION"; then
  1564. execute mv "$tmp" "$SCRIPT_PATH" || { err "Failed to replace the script"; execute rm -f "$tmp"; return 1; }
  1565. execute chmod +x "$SCRIPT_PATH" || { err "Failed to make the script executable"; return 1; }
  1566. execute rm -f "$tmp"
  1567. echo "Detected installJRMC update, restarting"
  1568. exec "$SCRIPT_PATH" "$@" "--no-update"
  1569. else
  1570. debug "Current installJRMC $SCRIPT_VERSION is the latest version"
  1571. execute rm -f "$tmp"
  1572. return 0
  1573. fi
  1574. fi
  1575. }
  1576. # @description installJRMC main function
  1577. main() {
  1578. debug "${FUNCNAME[0]}()" "$@" # prints function name and arguments
  1579. echo "Starting installJRMC $SCRIPT_VERSION"
  1580. if ((DEBUG)); then
  1581. echo "Debugging on"
  1582. else
  1583. echo "To enable debugging output, use --debug or -d"
  1584. fi
  1585. # Parse input, set default/host variables, and MC version
  1586. init "$@"
  1587. ((UNINSTALL_SWITCH)) && uninstall
  1588. # Exit now if only --uninstall is passed
  1589. if ! (( BUILD_SWITCH || CREATEREPO_SWITCH || REPO_INSTALL_SWITCH || LOCAL_INSTALL_SWITCH ||
  1590. CONTAINER_INSTALL_SWITCH || SNAP_INSTALL_SWITCH || APPIMAGE_INSTALL_SWITCH )) &&
  1591. [[ ${#SERVICES[@]} -eq 0 && ${#CONTAINERS[@]} -eq 0 ]]; then
  1592. exit 0
  1593. fi
  1594. install_external_repos
  1595. if ((REPO_INSTALL_SWITCH)); then
  1596. echo "Installing JRiver Media Center from remote repository"
  1597. if install_mc_repo; then
  1598. echo "JRiver Media Center installed successfully from remote repository"
  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. else
  1605. err "JRiver Media Center installation from remote repository failed"
  1606. return 1
  1607. fi
  1608. fi
  1609. if ((BUILD_SWITCH)); then
  1610. acquire_deb || { err "Could not download Media Center DEB package"; return 1; }
  1611. if [[ $BUILD_TARGET =~ centos|fedora|suse|mandriva || $CREATEREPO_TARGET =~ centos|fedora|suse|mandriva ]]; then
  1612. install_package dpkg rpm-build
  1613. [[ -d $OUTPUT_DIR/SPECS ]] || execute mkdir -p "$OUTPUT_DIR/SPECS"
  1614. if build_rpm; then
  1615. echo "RPM package built successfully"
  1616. else
  1617. err "Failed to build RPM package"
  1618. # On build failure, remove the source DEB in case it is corrupted
  1619. if [[ -f $MC_DEB ]]; then
  1620. echo "Removing source DEB"
  1621. if ! execute rm -f "$MC_DEB"; then
  1622. execute sudo rm -f "$MC_DEB"
  1623. fi
  1624. fi
  1625. return 1
  1626. fi
  1627. fi
  1628. fi
  1629. if ((LOCAL_INSTALL_SWITCH)); then
  1630. echo "Installing JRiver Media Center from local package"
  1631. if PKG_INSTALL_LOCAL "$@"; then
  1632. echo "JRiver Media Center installed successfully from local package"
  1633. else
  1634. err "JRiver Media Center local package installation failed"
  1635. return 1
  1636. fi
  1637. install_mesa_freeworld
  1638. link_ssl_certs
  1639. restore_license
  1640. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1641. disable_btrfs_cow
  1642. fi
  1643. if ((CREATEREPO_SWITCH)); then
  1644. if run_createrepo; then
  1645. echo "Successfully updated repo"
  1646. else
  1647. err "Repo creation failed"
  1648. fi
  1649. fi
  1650. if [[ ${#SERVICES[@]} -gt 0 ]]; then
  1651. declare service
  1652. for service in "${SERVICES[@]}"; do
  1653. if ! "service_$service"; then
  1654. if [[ $? -eq 127 ]]; then
  1655. err "Service $service does not exist, check service name"
  1656. else
  1657. err "Failed to create $service service"
  1658. fi
  1659. else
  1660. echo "Started and enabled $service service"
  1661. fi
  1662. done
  1663. unset service
  1664. fi
  1665. }
  1666. # @section Helper functions
  1667. debug() { ((DEBUG)) && echo "Debug: $*"; }
  1668. err() { echo "Error: $*" >&2; }
  1669. ask_ok() {
  1670. local response
  1671. ((YES_SWITCH)) && return 0
  1672. read -n 1 -r -p "$* [y/N]: " response
  1673. echo
  1674. [[ ${response,,} == y ]]
  1675. }
  1676. execute() {
  1677. if debug "$*"; then
  1678. "$@"
  1679. else
  1680. "$@" &>/dev/null
  1681. fi
  1682. }
  1683. download() {
  1684. debug "${FUNCNAME[0]}()" "$@"
  1685. local url="$1"
  1686. local output="${2:-}"
  1687. local -a cmd
  1688. if command -v curl &>/dev/null || install_package --silent curl; then
  1689. cmd=(curl --silent --fail --location)
  1690. if [[ -n "$output" ]]; then
  1691. cmd+=(--output "$output")
  1692. else
  1693. cmd+=(--remote-name)
  1694. fi
  1695. elif command -v wget &>/dev/null || install_package --silent wget; then
  1696. cmd=(wget --quiet)
  1697. [[ -n "$output" ]] && cmd+=("--output-document=$output")
  1698. else
  1699. err "Unable to install wget or curl"
  1700. return 1
  1701. fi
  1702. debug "${cmd[@]}" "$url"
  1703. "${cmd[@]}" "$url"
  1704. }
  1705. create_mc_apt_container() {
  1706. debug "${FUNCNAME[0]}()" "$@"
  1707. declare -g CNT
  1708. local -a cmds=("$@")
  1709. { command -v buildah &>/dev/null || install_package buildah &>/dev/null; } &&
  1710. CNT=$(sudo buildah from --quiet alpine:edge) &&
  1711. sudo buildah run "$CNT" -- sh -c '
  1712. apk add --no-cache apt curl gnupg &>/dev/null
  1713. curl -fsSL https://dist.jriver.com/mediacenter@jriver.com.gpg.key | gpg --dearmor -o /usr/share/keyrings/jriver-com-archive-keyring.gpg &>/dev/null
  1714. cat <<-EOF > /etc/apt/sources.list.d/jriver.sources
  1715. Types: deb
  1716. URIs: https://dist.jriver.com/latest/mediacenter/
  1717. Signed-By: /usr/share/keyrings/jriver-com-archive-keyring.gpg
  1718. Suites: '"$MC_REPO"'
  1719. Components: main
  1720. Architectures: '"$MC_ARCH"'
  1721. EOF
  1722. apt-get update &>/dev/null' &&
  1723. # If user passes command strings run them in the container
  1724. for cmd in "${cmds[@]}"; do
  1725. sudo buildah run "$CNT" -- sh -c "$cmd" || { err "$cmd failed"; return 1; }
  1726. done
  1727. }
  1728. add_temp_repo() {
  1729. debug "${FUNCNAME[0]}()"
  1730. local repo_name repo_uri repo_suite repo_key
  1731. if [[ "$ID" == "ubuntu" ]]; then
  1732. local major_version="${VERSION_ID%%.*}"
  1733. local minor_version="${VERSION_ID##*.}"
  1734. minor_version="${minor_version#0}" # strip leading zero
  1735. if [[ $major_version -gt 24 || ( $major_version -eq 24 && minor_version -ge 4 ) ]]; then
  1736. echo "Temporarily adding jammy repository for libwebkit2gtk-4.0-37, etc."
  1737. repo_name="ubuntu-jammy-temp"
  1738. repo_uri="https://archive.ubuntu.com/ubuntu"
  1739. repo_suite="jammy"
  1740. repo_key="/usr/share/keyrings/ubuntu-archive-keyring.gpg"
  1741. else
  1742. return 0
  1743. fi
  1744. elif [[ "$ID" == "debian" ]]; then
  1745. local major_version="${VERSION_ID%%.*}"
  1746. if [[ $major_version -ge 13 ]]; then
  1747. echo "Temporarily adding bookworm repository for libwebkit2gtk-4.0-37, etc."
  1748. repo_name="debian-bookworm-temp"
  1749. repo_uri="https://deb.debian.org/debian"
  1750. repo_suite="bookworm"
  1751. repo_key="/usr/share/keyrings/debian-archive-keyring.gpg"
  1752. else
  1753. return 0
  1754. fi
  1755. else
  1756. # For other distributions, do nothing.
  1757. return 0
  1758. fi
  1759. declare -g TEMP_REPO_FILE="/etc/apt/sources.list.d/${repo_name}.sources"
  1760. echo "Creating temporary repository file $TEMP_REPO_FILE for $repo_suite"
  1761. sudo bash -c "cat <<-EOF > $TEMP_REPO_FILE
  1762. Types: deb
  1763. URIs: $repo_uri
  1764. Suites: $repo_suite
  1765. Components: main
  1766. Architectures: $MC_ARCH
  1767. Signed-By: $repo_key
  1768. EOF"
  1769. }
  1770. # Roughly turn debugging on for pre-init
  1771. # Reset and reparse in parse_input() with getopt
  1772. [[ " $* " =~ ( --debug | -d ) ]] && DEBUG=1
  1773. main "$@"
  1774. exit