installJRMC 63 KB

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