installJRMC 66 KB

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