installJRMC 61 KB

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