installJRMC 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. #!/usr/bin/env bash
  2. # Install JRiver Media Center and associated services
  3. # See installJRMC --help or printHelp() below
  4. #
  5. # Copyright (c) 2021-2023 Bryan C. Roessler
  6. # This software is released under the Apache License.
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. #
  10. # TODO (v1)
  11. # 1. Finish refactoring
  12. #
  13. # TODO (v2)
  14. # 1. Interactive installation (ncurses?)
  15. # 2. Additional containerization (createrepo and rpmbuild)
  16. # 3. Tests
  17. #
  18. # BUGS
  19. # 1. No createrepo on Mint
  20. shopt -s extglob
  21. declare -g SCRIPTVERSION="1.0-dev"
  22. declare -g OUTPUTDIR="$PWD/output"
  23. declare -g BOARDURL="https://yabb.jriver.com/interact/index.php/board,76.0.html" # MC30
  24. declare -g DEBIANBASE="buster"
  25. declare -g MCVERSION_HARDCODE="${MCVERSION:-"30.0.55"}" # Hardcoded fallback
  26. declare -g CREATEREPO_WEBROOT="/var/www/jriver"
  27. declare -g USER="${SUDO_USER:-$USER}"
  28. declare -g HOME; HOME=$(getent passwd "$USER" | cut -d: -f6)
  29. printHelp() {
  30. debug "Running: ${FUNCNAME[0]}"
  31. cat <<-'EOF'
  32. USAGE:
  33. installJRMC [[OPTION] [VALUE]]...
  34. If no options (excluding -d or --debug) are provided installJRMC defaults to '--install repo'.
  35. OPTIONS
  36. --install, -i repo|local
  37. repo: Install MC from repository, updates are handled by the system package manager
  38. local: Build and install MC package locally from official source release
  39. --build[=suse|fedora|centos]
  40. Build RPM from source DEB but do not install
  41. Optionally, specify a target distro for cross-building (ex. --build=suse, note the '=')
  42. --compat
  43. Build/install MC locally without minimum dependency version requirements
  44. --mcversion VERSION
  45. Specify the MC version, ex. "30.0.55" (default: latest version)
  46. --arch VERSION
  47. Specify the MC architecture, ex. "amd64", "arm64", etc (default: host architecture)
  48. --outputdir PATH
  49. Generate rpmbuild output in this directory (default: ./output)
  50. --restorefile RESTOREFILE
  51. Restore file location for automatic license registration
  52. --betapass PASSWORD
  53. Enter beta team password for access to beta builds
  54. --service, -s SERVICE
  55. See SERVICES section below for a list of possible services to install
  56. --service-type user|system
  57. Starts services at boot (system) or at user login (user) (default: per service, see SERVICES)
  58. --container, -c CONTAINER (TODO: Under construction)
  59. See CONTAINERS section below for a list of possible services to install
  60. --createrepo[=suse|fedora|centos]
  61. Build rpm, copy to webroot, and run createrepo.
  62. Use in conjunction with --build=TARGET for crossbuilding repos
  63. Optionally, specify a target distro for non-native repo (ex. --createrepo=fedora, note the '=')
  64. --createrepo-webroot PATH
  65. Specify the webroot directory to install the repo (default: /var/www/jriver)
  66. --createrepo-user USER
  67. Specify the web server user if it differs from $USER
  68. --version, -v
  69. Print this script version and exit
  70. --debug, -d
  71. Print debug output
  72. --help, -h
  73. Print help dialog and exit
  74. --uninstall, -u
  75. Uninstall JRiver MC, remove services, containers, and firewall rules (does not remove library files)
  76. SERVICES
  77. jriver-mediaserver (default --service-type=user)
  78. Enable and start a mediaserver systemd service (requires an existing X server)
  79. jriver-mediacenter (user)
  80. Enable and start a mediacenter systemd service (requires an existing X server)
  81. jriver-x11vnc (user)
  82. Enable and start x11vnc for the local desktop (requires an existing X server)
  83. Usually combined with jriver-mediaserver or jriver-mediacenter services
  84. --vncpass and --display are optional (see below)
  85. jriver-xvnc (system)
  86. Enable and start a new Xvnc session running JRiver Media Center
  87. --vncpass PASSWORD
  88. Set the vnc password for x11vnc/Xvnc access. If no password is set, installJRMC
  89. will either use existing password stored in $HOME/.vnc/jrmc_passwd or use no password
  90. --display DISPLAY
  91. Display to use for x11vnc/Xvnc (default: The current display (x11vnc) or the
  92. current display incremented by 1 (Xvnc))
  93. jriver-createrepo (system)
  94. Install hourly service to build latest MC RPM and run createrepo
  95. CONTAINERS (TODO: Under construction)
  96. mediacenter-xvnc
  97. createrepo
  98. EOF
  99. }
  100. # Helpers
  101. debug() { (( DEBUG )) && echo "Debug: $*"; }
  102. err() { echo "Error: $*" >&2; }
  103. askOk() {
  104. declare response
  105. read -r -p "$* [y/N]: " response
  106. [[ "${response,,}" =~ ^(yes|y)$ ]]
  107. }
  108. execute() {
  109. if debug "$*"; then
  110. eval "$*"
  111. else
  112. eval "$* &>/dev/null"
  113. fi
  114. }
  115. #######################################
  116. # Parses user input and sets sensible defaults
  117. #######################################
  118. parseInput() {
  119. debug "Running: ${FUNCNAME[0]}"
  120. declare -g BUILD_SWITCH REPO_INSTALL_SWITCH COMPAT_SWITCH TEST_SWITCH
  121. declare -g LOCAL_INSTALL_SWITCH CREATEREPO_SWITCH UNINSTALL_SWITCH
  122. declare -g USER_VERSION_SWITCH USER_ARCH
  123. declare -g OUTPUTDIR RESTOREFILE BETAPASS SERVICE_TYPE
  124. declare -g VNCPASS USER_DISPLAY CREATEREPO_WEBROOT
  125. declare -ga SERVICES CONTAINERS
  126. declare long_opts short_opts input
  127. # Allow some environment variables to override and set sane fallbacks
  128. declare -g TARGET=${TARGET:-$ID}
  129. declare -g CREATEREPO_USER="${CREATEREPO_USER:-$USER}"
  130. if [[ $# -eq 0 ]] || [[ $# -eq 1 && " $1 " =~ ^( --debug | -d )$ ]]; then
  131. REPO_INSTALL_SWITCH=1
  132. elif [[ $# -eq 1 && " $1 " =~ ^( --compat )$ ]]; then
  133. BUILD_SWITCH=1
  134. LOCAL_INSTALL_SWITCH=1
  135. fi
  136. long_opts="install:,build::,outputdir:,mcversion:,restorefile:,betapass:,"
  137. long_opts+="service-type:,service:,services:,version,debug,help,uninstall,"
  138. long_opts+="createrepo::,createrepo-webroot:,createrepo-user:,"
  139. long_opts+="vncpass:,display:,container:,tests,compat,arch:"
  140. short_opts="+i:vb::dhus:c:"
  141. # Redeclare DEBUG and catch permanently with getopt
  142. declare -g DEBUG=0
  143. if input=$(getopt -o $short_opts -l $long_opts -- "$@"); then
  144. eval set -- "$input"
  145. while true; do
  146. case "$1" in
  147. --install|-i)
  148. shift
  149. case "$1" in
  150. local|rpm)
  151. BUILD_SWITCH=1
  152. LOCAL_INSTALL_SWITCH=1
  153. ;;
  154. repo|remote)
  155. REPO_INSTALL_SWITCH=1
  156. ;;
  157. esac
  158. ;;
  159. --build|-b)
  160. BUILD_SWITCH=1
  161. shift && TARGET="$1"
  162. ;;
  163. --outputdir)
  164. shift && OUTPUTDIR="$1"
  165. ;;
  166. --mcversion)
  167. shift
  168. MCVERSION="$1"
  169. USER_VERSION_SWITCH=1
  170. ;;
  171. --arch)
  172. shift
  173. USER_ARCH="$1"
  174. ;;
  175. --restorefile)
  176. shift && RESTOREFILE="$1"
  177. ;;
  178. --betapass)
  179. shift && BETAPASS="$1"
  180. ;;
  181. --service-type)
  182. shift && SERVICE_TYPE="$1"
  183. ;;
  184. --service|-s|--services)
  185. shift && SERVICES+=("$1")
  186. ;;
  187. --createrepo)
  188. BUILD_SWITCH=1
  189. CREATEREPO_SWITCH=1
  190. shift && TARGET="$1"
  191. ;;
  192. --createrepo-webroot)
  193. shift && CREATEREPO_WEBROOT="$1"
  194. ;;
  195. --createrepo-user)
  196. shift && CREATEREPO_USER="$1"
  197. ;;
  198. --vncpass)
  199. shift && VNCPASS="$1"
  200. ;;
  201. --display)
  202. shift && USER_DISPLAY="$1"
  203. ;;
  204. --compat)
  205. COMPAT_SWITCH=1
  206. ;;
  207. --container|-c)
  208. shift && CONTAINERS+=("$1")
  209. ;;
  210. --version|-v)
  211. echo "Version: $SCRIPTVERSION"
  212. exit 0
  213. ;;
  214. --debug|-d)
  215. DEBUG=1
  216. ;;
  217. --help|-h)
  218. printHelp
  219. exit
  220. ;;
  221. --uninstall|-u)
  222. UNINSTALL_SWITCH=1
  223. ;;
  224. --tests)
  225. TEST_SWITCH=1
  226. ;;
  227. --)
  228. shift
  229. break
  230. ;;
  231. esac
  232. shift
  233. done
  234. else
  235. err "Incorrect options provided"
  236. printHelp && exit 1
  237. fi
  238. }
  239. #######################################
  240. # Perform OS detection and fallback
  241. # Generate OS-specific functions
  242. #######################################
  243. init() {
  244. debug "Running: ${FUNCNAME[0]}"
  245. declare -g ID RPM_MGR ARCH
  246. declare -ga PKG_INSTALL PKG_REMOVE PKG_UPDATE
  247. echo "Starting installJRMC"
  248. (( DEBUG )) || echo "To enable debugging output, use --debug or -d"
  249. if [[ -e "/etc/os-release" ]]; then
  250. source "/etc/os-release"
  251. else
  252. err "/etc/os-release not found"
  253. err "Your OS is unsupported"
  254. printHelp
  255. exit 1
  256. fi
  257. # Detect architecture and translate to MC convention
  258. # Also catch user input in getopt
  259. ARCH=$(uname -m)
  260. case "$ARCH" in
  261. x86_64)
  262. ARCH="amd64"
  263. ;;
  264. esac
  265. debug "Detected host platform: $ID $VERSION_ID $ARCH"
  266. # normalize ID and set distro-specific vars
  267. case "$ID" in
  268. debian|arch)
  269. ;;
  270. centos|fedora)
  271. if hash dnf &>/dev/null; then
  272. RPM_MGR="dnf"
  273. elif hash yum &>/dev/null; then
  274. RPM_MGR="yum"
  275. fi
  276. ;;
  277. rhel)
  278. ID="centos"
  279. ;;
  280. linuxmint|neon|*ubuntu*)
  281. ID="ubuntu"
  282. ;;
  283. *suse*)
  284. ID="suse"
  285. ;;
  286. raspbian)
  287. ID="debian"
  288. ;;
  289. *)
  290. err "Autodetecting distro, this is unreliable and --compat may be required"
  291. if hash dnf &>/dev/null; then
  292. ID="fedora"
  293. RPM_MGR="dnf"
  294. elif hash yum &>/dev/null; then
  295. ID="centos"
  296. RPM_MGR="yum"
  297. COMPAT_SWITCH=1
  298. elif hash apt &>/dev/null; then
  299. ID="ubuntu"
  300. elif hash pacman &>/dev/null; then
  301. ID="arch"
  302. else
  303. err "OS detection failed!"
  304. exit 1
  305. fi
  306. esac
  307. debug "Using host platform: $ID $VERSION_ID"
  308. # Abstract distro-specific package manager commands
  309. case "$ID" in
  310. fedora|centos)
  311. PKG_INSTALL=(sudo "$RPM_MGR" install -y)
  312. PKG_REMOVE=(sudo "$RPM_MGR" remove -y)
  313. PKG_UPDATE=(sudo "$RPM_MGR" makecache)
  314. PKG_QUERY(){ rpm -q "$@"; }
  315. PKG_INSTALL_LOCAL(){ installMCRPM; }
  316. ;;
  317. debian|ubuntu)
  318. PKG_INSTALL=(sudo apt-get -f install -y -q0)
  319. PKG_REMOVE=(sudo apt-get remove --auto-remove -y -q0)
  320. PKG_UPDATE=(sudo apt-get update -y -q0)
  321. PKG_QUERY(){ dpkg -s "$@"; }
  322. PKG_INSTALL_LOCAL(){ installMCDEB; }
  323. ;;
  324. suse)
  325. PKG_INSTALL=(sudo zypper --non-interactive -q install --force --no-confirm)
  326. PKG_REMOVE=(sudo zypper --non-interactive -q remove --clean-deps)
  327. PKG_UPDATE=(sudo zypper --non-interactive -q refresh jriver)
  328. PKG_QUERY(){ rpm -q "$@"; }
  329. PKG_INSTALL_LOCAL(){ installMCRPM; }
  330. ;;
  331. arch)
  332. PKG_INSTALL=(sudo pacman -Sy --noconfirm)
  333. PKG_REMOVE=(sudo pacman -Rs --noconfirm)
  334. PKG_UPDATE=(sudo pacman -Syy)
  335. PKG_QUERY(){ sudo pacman -Qs "$@"; }
  336. PKG_INSTALL_LOCAL(){ installMCARCH; }
  337. ;;
  338. esac
  339. }
  340. #######################################
  341. # Uses several methods to determine the latest JRiver MC version
  342. # TODO but how to determine build distro `$DEBIANBASE=buster`?
  343. #######################################
  344. setMCVersion() {
  345. debug "Running: ${FUNCNAME[0]}"
  346. declare -g MCVERSION_SOURCE MVERSION MCPKG MCRPM
  347. declare cnt
  348. # User input
  349. if (( USER_VERSION_SWITCH )) &&
  350. [[ "$MCVERSION" =~ ([0-9]+.[0-9]+.[0-9]+) ]]; then
  351. MCVERSION_SOURCE="user input"
  352. # Containerized package manager
  353. elif installPackage --silent buildah &&
  354. cnt=$(buildah from --quiet debian:$DEBIANBASE-slim) &>/dev/null &&
  355. buildah run "$cnt" -- bash -c \
  356. "echo 'deb [trusted=no arch=amd64,i386,armhf,arm64] http://dist.jriver.com/latest/mediacenter/ $DEBIANBASE main' > /etc/apt/sources.list 2>&1" &>/dev/null &&
  357. buildah run "$cnt" -- bash -c \
  358. "apt update --allow-insecure-repositories &>/dev/null" &>/dev/null &&
  359. MCVERSION=$(buildah run "$cnt" -- apt-cache policy mediacenter?? | grep Candidate | awk '{print $2}' | sort -V | tail -n1) &>/dev/null &&
  360. [[ "$MCVERSION" =~ ([0-9]+.[0-9]+.[0-9]+) ]]; then
  361. MCVERSION_SOURCE="containerized package manager"
  362. buildah rm "$cnt" &>/dev/null
  363. # Webscrape
  364. elif installPackage wget && MCVERSION=$(wget -qO- "$BOARDURL" | grep -o "[0-9][0-9]\.[0-9]\.[0-9]\+" | head -n 1) &&
  365. [[ "$MCVERSION" =~ ([0-9]+.[0-9]+.[0-9]+) ]]; then
  366. MCVERSION_SOURCE="webscrape"
  367. # Hardcoded
  368. else
  369. declare -g MCVERSION="$MCVERSION_HARDCODE"
  370. MCVERSION_SOURCE="hardcoded or MCVERSION env"
  371. err "Warning! Using hardcoded version number"
  372. fi
  373. MVERSION="${MCVERSION%%.*}"
  374. MCPKG="mediacenter$MVERSION"
  375. MCRPM="$OUTPUTDIR/RPMS/x86_64/mediacenter$MVERSION-$MCVERSION.x86_64.rpm"
  376. if [[ "$MCVERSION_SOURCE" == "user input" ]]; then
  377. # Append explicit package version when user provides --mcversion
  378. case "$ID" in
  379. fedora|centos|suse)
  380. MCPKG+="-$MCVERSION"
  381. ;;
  382. debian|ubuntu)
  383. MCPKG+="=$MCVERSION"
  384. ;;
  385. esac
  386. fi
  387. echo "Using MC version $MCVERSION determined by $MCVERSION_SOURCE"
  388. [[ "$MCVERSION_SOURCE" == "user input" ]] || echo "To override, use --mcversion"
  389. debug "MVERSION: $MVERSION, MCVERSION: $MCVERSION, MCPKG: $MCPKG, MCRPM: $MCRPM"
  390. }
  391. #######################################
  392. # Installs a package using the system package manager
  393. # Arguments:
  394. # One or more package names
  395. # Options:
  396. # --no-install-check: Do not check if package is already installed
  397. # --no-gpg-check: Disable GPG checks for RPM based distros
  398. # --allow-downgrades: Useful for installing specific MC versions
  399. # --silent, -s: Do not print errors (useful for optional packages)
  400. #######################################
  401. installPackage() {
  402. debug "Running: ${FUNCNAME[0]}" "$@"
  403. declare -a pkg_array install_flags
  404. declare long_opts input pkg
  405. declare no_install_check allow_downgrades silent refresh no_gpg_check
  406. declare -A pkg_aliases
  407. long_opts="no-install-check,allow-downgrades,no-gpg-check,refresh,silent"
  408. if input=$(getopt -o +s -l "$long_opts" -- "$@"); then
  409. eval set -- "$input"
  410. while true; do
  411. case "$1" in
  412. --no-install-check)
  413. no_install_check=1
  414. ;;
  415. --allow-downgrades)
  416. allow_downgrades=1
  417. ;;
  418. --no-gpg-check)
  419. no_gpg_check=1
  420. ;;
  421. --refresh)
  422. refresh=1
  423. ;;
  424. --silent|-s)
  425. silent=1
  426. ;;
  427. --)
  428. shift
  429. break
  430. ;;
  431. esac
  432. shift
  433. done
  434. else
  435. err "Incorrect options provided"
  436. exit 1
  437. fi
  438. # Package aliases
  439. case "$ID" in
  440. debian|ubuntu)
  441. pkg_aliases["rpm-build"]="rpm"
  442. pkg_aliases["createrepo_c"]="createrepo"
  443. pkg_aliases["tigervnc-server"]="tigervnc-standalone-server"
  444. ;;
  445. esac
  446. # Filter installed packages
  447. for pkg in "$@"; do
  448. if [[ -v pkg_aliases[$pkg] ]]; then
  449. pkg=${pkg_aliases[$pkg]}
  450. fi
  451. if (( no_install_check )) ||
  452. ! (hash "$pkg" &>/dev/null ||
  453. PKG_QUERY "$pkg" &>/dev/null); then
  454. pkg_array+=("$pkg")
  455. else
  456. debug "$pkg already installed, skipping installation"
  457. fi
  458. done
  459. # Generate distro-specific install flags
  460. case "$ID" in
  461. debian|ubuntu)
  462. (( allow_downgrades )) && install_flags+=(--allow-downgrades)
  463. ;;
  464. fedora|centos)
  465. (( allow_downgrades )) && install_flags+=(--allowerasing)
  466. (( no_gpg_check )) && install_flags+=(--nogpgcheck)
  467. (( refresh )) && install_flags+=(--refresh)
  468. ;;
  469. suse)
  470. (( no_gpg_check )) && install_flags+=(--allow-unsigned-rpm)
  471. ;;
  472. esac
  473. # Install packages from package array
  474. if [[ ${#pkg_array[@]} -ge 1 ]]; then
  475. if ! execute "${PKG_INSTALL[*]} ${install_flags[*]} ${pkg_array[*]}"; then
  476. (( silent )) || err "Failed to install ${pkg_array[*]}. Attempting to continue"
  477. return 1
  478. fi
  479. fi
  480. return 0
  481. }
  482. #######################################
  483. # Installs JRiver Media Center from a remote repository
  484. #######################################
  485. installMCFromRepo() {
  486. debug "Running: ${FUNCNAME[0]}"
  487. case "$ID" in
  488. fedora|centos)
  489. sudo bash -c "cat <<-EOF > /etc/yum.repos.d/jriver.repo
  490. [jriver]
  491. name=JRiver Media Center repo by BryanC
  492. baseurl=https://repos.bryanroessler.com/jriver
  493. gpgcheck=0
  494. EOF"
  495. ;;
  496. debian|ubuntu)
  497. declare repo_dir="/etc/apt/sources.list.d"
  498. [[ -d $repo_dir ]] || execute "sudo mkdir -p $repo_dir"
  499. sudo rm -rf "$repo_dir"/mediacenter*.list
  500. installPackage wget
  501. sudo bash -c "cat <<-EOF > $repo_dir/jriver.list
  502. deb [trusted=yes arch=amd64,i386,armhf,arm64] http://dist.jriver.com/latest/mediacenter/ $DEBIANBASE main
  503. EOF"
  504. wget -qO- "http://dist.jriver.com/mediacenter@jriver.com.gpg.key" |
  505. sudo tee /etc/apt/trusted.gpg.d/jriver.asc &>/dev/null
  506. ;;
  507. *)
  508. err "An MC repository for $ID is not yet available"
  509. err "Try using --install local to install MC on $ID"
  510. return 1
  511. ;;
  512. esac
  513. if ! execute "${PKG_UPDATE[@]}"; then
  514. err "Package update failed!"
  515. return 1
  516. fi
  517. # Install mesa-va-drivers-freeworld separately from the RPM using dnf swap
  518. installMesa
  519. if ! execute installPackage \
  520. --no-install-check \
  521. --allow-downgrades \
  522. --no-gpg-check \
  523. "$MCPKG"; then
  524. err "Package install failed!"
  525. return 1
  526. fi
  527. }
  528. #######################################
  529. # Acquires the source DEB package from JRiver
  530. #######################################
  531. acquireDeb() {
  532. debug "Running: ${FUNCNAME[0]}"
  533. declare -g MCDEB="$OUTPUTDIR/SOURCES/MediaCenter-$MCVERSION-${USER_ARCH:-$ARCH}.deb"
  534. debug "MCDEB=$MCDEB"
  535. # If necessary, create SOURCES dir
  536. [[ -d "$OUTPUTDIR/SOURCES" ]] || execute "mkdir -p $OUTPUTDIR/SOURCES"
  537. # If deb file already exists, skip download
  538. if [[ -f "$MCDEB" ]]; then
  539. echo "Using existing DEB: $MCDEB"
  540. return 0
  541. fi
  542. if [[ -v BETAPASS ]] &&
  543. echo "Checking beta repo for DEB package" && wget -q -O "$MCDEB" \
  544. "https://files.jriver.com/mediacenter/channels/v$MVERSION/beta/$BETAPASS/MediaCenter-$MCVERSION-${USER_ARCH:-$ARCH}.deb"; then
  545. echo "Found!"
  546. elif echo "Checking latest repo for DEB package" && wget -q -O "$MCDEB" \
  547. "https://files.jriver.com/mediacenter/channels/v$MVERSION/latest/MediaCenter-$MCVERSION-${USER_ARCH:-$ARCH}.deb"; then
  548. echo "Found!"
  549. elif echo "Checking test repo for DEB package" && wget -q -O "$MCDEB" \
  550. "https://files.jriver.com/mediacenter/test/MediaCenter-$MCVERSION-${USER_ARCH:-$ARCH}.deb"; then
  551. echo "Found!"
  552. else
  553. err "Cannot find DEB file"
  554. exit 1
  555. fi
  556. if [[ -f "$MCDEB" ]]; then
  557. echo "Downloaded MC $MCVERSION DEB to $MCDEB"
  558. else
  559. err "Downloaded DEB file missing or corrupted"
  560. exit 1
  561. fi
  562. }
  563. #######################################
  564. # Creates a SPEC file and builds the RPM from the source DEB using rpmbuild
  565. #######################################
  566. buildRPM() {
  567. debug "Running: ${FUNCNAME[0]}"
  568. declare i rpmbuild_cmd
  569. declare -a requires recommends
  570. # skip rebuilding the rpm if it already exists
  571. if [[ -f "$MCRPM" ]]; then
  572. echo "$MCRPM already exists. Skipping build step"
  573. return 0
  574. fi
  575. [[ -d "$OUTPUTDIR/SPECS" ]] || mkdir -p "$OUTPUTDIR/SPECS"
  576. # Load deb dependencies into array
  577. IFS=',' read -ra requires <<< "$(dpkg-deb -f "$MCDEB" Depends)"
  578. IFS=',' read -ra recommends <<< "$(dpkg-deb -f "$MCDEB" Recommends)"
  579. # Clean up formatting
  580. requires=("${requires[@]%%|*}")
  581. requires=("${requires[@]/?:/}")
  582. requires=("${requires[@]# }")
  583. requires=("${requires[@]% }")
  584. requires=("${requires[@]//\(/}")
  585. requires=("${requires[@]//)/}")
  586. recommends=("${recommends[@]%%|*}")
  587. recommends=("${recommends[@]/?:/}")
  588. recommends=("${recommends[@]# }")
  589. recommends=("${recommends[@]% }")
  590. recommends=("${recommends[@]//\(/}")
  591. recommends=("${recommends[@]//)/}")
  592. # Translate package names
  593. case "$TARGET" in
  594. fedora|centos)
  595. requires=("${requires[@]/libc6/glibc}")
  596. requires=("${requires[@]/libasound2/alsa-lib}")
  597. requires=("${requires[@]/libuuid1/libuuid}")
  598. requires=("${requires[@]/libx11-6/libX11}")
  599. requires=("${requires[@]/libxext6/libXext}")
  600. requires=("${requires[@]/libxcb1/libxcb}")
  601. requires=("${requires[@]/libxdmcp6/libXdmcp}")
  602. requires=("${requires[@]/libstdc++6/libstdc++}")
  603. requires=("${requires[@]/libgtk-3-0/gtk3}")
  604. requires=("${requires[@]/libgl1/mesa-libGL}")
  605. requires=("${requires[@]/libpango-1.0-0/pango}")
  606. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  607. requires=("${requires[@]/libpangox-1.0-0/pango}")
  608. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  609. requires=("${requires[@]/libnss3/nss}")
  610. requires=("${requires[@]/libnspr4/nspr}")
  611. requires=("${requires[@]/libgomp1/libgomp}")
  612. requires=("${requires[@]/libfribidi0/fribidi}")
  613. requires=("${requires[@]/libfontconfig1/fontconfig}")
  614. requires=("${requires[@]/libfreetype6/freetype}")
  615. requires=("${requires[@]/libharfbuzz0b/harfbuzz}")
  616. requires=("${requires[@]/libgbm1/mesa-libgbm}")
  617. requires=("${requires[@]/libva2/libva}")
  618. requires=("${requires[@]/libepoxy0/libepoxy}")
  619. requires=("${requires[@]/liblcms2-2/lcms2}")
  620. requires=("${requires[@]/libvulkan1/vulkan-loader}")
  621. requires=("${requires[@]/libepoxy0/libepoxy}")
  622. requires=("${requires[@]/python/python3}")
  623. recommends+=(mesa-va-drivers-freeworld)
  624. ;;
  625. suse)
  626. requires=("${requires[@]/libc6/glibc}")
  627. requires=("${requires[@]/libasound2/alsa-lib}")
  628. requires=("${requires[@]/libx11-6/libX11-6}")
  629. requires=("${requires[@]/libxext6/libXext6}")
  630. requires=("${requires[@]/libxdmcp6/libXdmcp6}")
  631. requires=("${requires[@]/libgtk-3-0/gtk3}")
  632. requires=("${requires[@]/libgl1/Mesa-libGL1}")
  633. requires=("${requires[@]/libpango-1.0-0/pango}")
  634. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  635. requires=("${requires[@]/libpangox-1.0-0/pango}")
  636. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  637. requires=("${requires[@]/libnss3/mozilla-nss}")
  638. requires=("${requires[@]/libnspr4/mozilla-nspr}")
  639. requires=("${requires[@]/libfribidi0/fribidi}")
  640. requires=("${requires[@]/libfontconfig1/fontconfig}")
  641. requires=("${requires[@]/libfreetype6*/freetype}") # Remove minimum version specifier
  642. requires=("${requires[@]/libharfbuzz0b/libharfbuzz0}")
  643. for i in "${!requires[@]}"; do
  644. [[ "${requires[$i]}" == "mesa-vulkan-drivers" ]] && unset -v 'requires[i]'
  645. done
  646. recommends+=(libvulkan_intel)
  647. recommends+=(libvulkan_radeon)
  648. ;;
  649. esac
  650. # Convert array to newline delim'd string (for heredoc)
  651. printf -v requires "Requires: %s\n" "${requires[@]}"
  652. printf -v recommends "Recommends: %s\n" "${recommends[@]}"
  653. # Strip last newline
  654. requires="${requires%?}"
  655. recommends="${recommends%?}"
  656. if (( COMPAT_SWITCH )); then
  657. # Strip minimum versions
  658. requires=$(echo "$requires" | awk -F" " 'NF == 4 {print $1 " " $2} NF != 4 {print $0}')
  659. fi
  660. # Create spec file
  661. cat <<-EOF > "$OUTPUTDIR/SPECS/mediacenter.spec"
  662. Name: mediacenter$MVERSION
  663. Version: $MCVERSION
  664. Release: 1
  665. Summary: JRiver Media Center
  666. Group: Applications/Media
  667. Source0: http://files.jriver.com/mediacenter/channels/v$MVERSION/latest/MediaCenter-$MCVERSION-${USER_ARCH:-$ARCH}.deb
  668. BuildArch: x86_64
  669. %define _rpmfilename %%{ARCH}/%%{NAME}-%%{version}.%%{ARCH}.rpm
  670. AutoReq: 0
  671. $requires
  672. $recommends
  673. Conflicts: MediaCenter
  674. Provides: mediacenter$MVERSION
  675. License: Copyright 1998-2023, JRiver, Inc. All rights reserved. Protected by U.S. patents #7076468 and #7062468
  676. URL: http://www.jriver.com/
  677. %define __provides_exclude_from ^%{_libdir}/jriver/.*/.*\\.so.*$
  678. %description
  679. Media Center is more than a world class player.
  680. %global __os_install_post %{nil}
  681. %prep
  682. %build
  683. %install
  684. dpkg -x %{S:0} %{buildroot}
  685. %post -p /sbin/ldconfig
  686. %postun -p /sbin/ldconfig
  687. %files
  688. %{_bindir}/mediacenter$MVERSION
  689. %{_libdir}/jriver
  690. %{_datadir}
  691. %exclude %{_datadir}/applications/media_center_packageinstaller_$MVERSION.desktop
  692. /etc/security/limits.d/*
  693. EOF
  694. # Run rpmbuild
  695. echo "Building MC $MCVERSION RPM, this may take awhile"
  696. rpmbuild_cmd=(
  697. rpmbuild
  698. --define=\"%_topdir "$OUTPUTDIR"\"
  699. --define=\"%_libdir /usr/lib\"
  700. -bb
  701. "$OUTPUTDIR/SPECS/mediacenter.spec"
  702. )
  703. if execute "${rpmbuild_cmd[*]}" && [[ -f "$MCRPM" ]] ; then
  704. echo "Build successful. The RPM file is located at: $MCRPM"
  705. else
  706. err "Build failed"
  707. # For automation, let's remove the source DEB and reaquire it on next
  708. # run after failure in case it is corrupted or buggy
  709. [[ -f "$MCDEB" ]] && echo "Removing source DEB" && rm -f "$MCDEB"
  710. exit 1
  711. fi
  712. }
  713. #######################################
  714. # Installs local Media Center DEB package and optional compatability fixes
  715. #######################################
  716. installMCDEB() {
  717. debug "Running: ${FUNCNAME[0]}"
  718. if (( COMPAT_SWITCH )); then
  719. declare extract_dir && extract_dir="$(mktemp -d)"
  720. pushd "$extract_dir" &>/dev/null || return
  721. ar x "$MCDEB"
  722. tar -xJf "control.tar.xz"
  723. # Remove minimum version specifiers from control file
  724. sed -i 's/ ([^)]*)//g' "control"
  725. sed -i 's/([^)]*)//g' "control" # TODO MC DEB package error
  726. [[ "$ID" == "ubuntu" && "${VERSION_ID%.*}" -le 16 ]] &&
  727. sed -i 's/libva2/libva1/g' "control"
  728. tar -cJf "control.tar.xz" "control" "postinst"
  729. declare -g MCDEB="${MCDEB/.deb/.compat.deb}"
  730. ar rcs "$MCDEB" "debian-binary" "control.tar.xz" "data.tar.xz"
  731. popd &>/dev/null || return
  732. rm -rf "$extract_dir"
  733. fi
  734. execute installPackage \
  735. --no-install-check \
  736. --no-gpg-check \
  737. --allow-downgrades \
  738. "$MCDEB"
  739. }
  740. #######################################
  741. # Installs local Media Center RPM package
  742. #######################################
  743. installMCRPM() {
  744. debug "Running: ${FUNCNAME[0]}"
  745. # Install mesa-va-freeworld separately from the RPM for dnf swap
  746. installMesa
  747. installPackage --no-install-check --no-gpg-check --allow-downgrades "$MCRPM"
  748. }
  749. #######################################
  750. # Installs mesa-va-drivers-freeworld
  751. #######################################
  752. installMesa() {
  753. debug "Running: ${FUNCNAME[0]}"
  754. # Currently only necessary in Fedora/CentOS
  755. case "$ID" in
  756. fedora|centos)
  757. if ! PKG_QUERY mesa-va-drivers-freeworld &>/dev/null; then
  758. if PKG_QUERY mesa-va-drivers &>/dev/null; then
  759. if ! execute sudo dnf swap -y \
  760. mesa-va-drivers \
  761. mesa-va-drivers-freeworld; then
  762. err "Package swap failed!"
  763. return 1
  764. fi
  765. else
  766. execute "${PKG_INSTALL[@]}" mesa-va-drivers-freeworld
  767. fi
  768. fi
  769. ;;
  770. esac
  771. }
  772. #######################################
  773. # Installs local Media Center PKGBUILD
  774. #######################################
  775. installMCARCH() {
  776. debug "Running: ${FUNCNAME[0]}"
  777. [[ -d "$OUTPUTDIR/PKGBUILD" ]] || mkdir -p "$OUTPUTDIR/PKGBUILD"
  778. cat <<-EOF > "$OUTPUTDIR/PKGBUILD/mediacenter.pkgbuild"
  779. pkgname=mediacenter$MVERSION
  780. pkgver=$MCVERSION
  781. pkgrel=1
  782. pkgdesc="The Most Comprehensive Media Software"
  783. arch=('x86_64')
  784. url="http://www.jriver.com/"
  785. license=('custom')
  786. depends=('alsa-lib' 'gcc-libs' 'libx11' 'libxext' 'libxcb' 'libxau' 'libxdmcp' 'util-linux' 'libxext' 'gtk3')
  787. optdepends=(
  788. 'mesa-libgl: nouveau video support'
  789. 'nvidia-libgl: nvidia video support'
  790. 'nvidia-utils: nvidia vulkan support'
  791. 'vulkan-intel: intel vulkan support'
  792. 'vulkan-radeon: amd vulkan support'
  793. 'vorbis-tools: ogg vorbis support'
  794. 'musepack-tools: musepack support'
  795. )
  796. source=("http://files.jriver.com/mediacenter/channels/v$MVERSION/latest/MediaCenter-$MCVERSION-${USER_ARCH:-$ARCH}.deb")
  797. package() {
  798. cd "\$srcdir"
  799. bsdtar xf data.tar.xz -C "\$pkgdir"
  800. }
  801. EOF
  802. pushd "$OUTPUTDIR/PKGBUILD" &>/dev/null || return
  803. if ! execute makepkg \
  804. --install \
  805. --syncdeps \
  806. --clean \
  807. --cleanbuild \
  808. --skipinteg \
  809. --force \
  810. --noconfirm \
  811. -p mediacenter.pkgbuild; then
  812. echo "makepkg failed"
  813. exit 1
  814. fi
  815. popd &>/dev/null || return
  816. }
  817. #######################################
  818. # Copy the RPM to createrepo-webroot and runs createrepo as the createrepo-user
  819. #######################################
  820. runCreaterepo() {
  821. debug "Running: ${FUNCNAME[0]}"
  822. declare -a cr_cmd
  823. installPackage createrepo_c
  824. # If the webroot does not exist, create it
  825. if [[ ! -d "$CREATEREPO_WEBROOT" ]]; then
  826. if ! execute "sudo -u $CREATEREPO_USER mkdir -p $CREATEREPO_WEBROOT"; then
  827. if ! ( execute "sudo mkdir -p $CREATEREPO_WEBROOT" &&
  828. execute "sudo chown -R $CREATEREPO_USER:$CREATEREPO_USER $CREATEREPO_WEBROOT" ); then
  829. err "Could not create the createrepo-webroot path!"
  830. err "Make sure that the webroot $CREATEREPO_WEBROOT is writeable by user $CREATEREPO_USER"
  831. err "Or change the repo ownership with --createrepo-user"
  832. return 1
  833. fi
  834. fi
  835. fi
  836. # Copy built rpms to webroot
  837. if ! ( execute "sudo cp -nf $MCRPM $CREATEREPO_WEBROOT" &&
  838. execute "sudo chown -R $CREATEREPO_USER:$CREATEREPO_USER $CREATEREPO_WEBROOT" ); then
  839. err "Could not copy $MCRPM to $CREATEREPO_WEBROOT"
  840. return 1
  841. fi
  842. # Run createrepo
  843. cr_cmd=(sudo -u "$CREATEREPO_USER" createrepo -q "$CREATEREPO_WEBROOT")
  844. [[ -d "$CREATEREPO_WEBROOT/repodata" ]] && cr_cmd+=(--update)
  845. if ! execute "${cr_cmd[*]}"; then
  846. cr_cmd=(sudo createrepo -q "$CREATEREPO_WEBROOT")
  847. [[ -d "$CREATEREPO_WEBROOT/repodata" ]] && cr_cmd+=(--update)
  848. if ! (execute "${cr_cmd[*]}" &&
  849. execute "sudo chown -R $CREATEREPO_USER:$CREATEREPO_USER $CREATEREPO_WEBROOT"); then
  850. err "createrepo failed"
  851. return 1
  852. fi
  853. fi
  854. }
  855. #######################################
  856. # Symlink certificates if they do not exist in default location
  857. #######################################
  858. symlinkCerts() {
  859. debug "Running: ${FUNCNAME[0]}"
  860. declare mc_cert_link="/usr/lib/jriver/MC$MVERSION/ca-certificates.crt"
  861. declare target_cert f
  862. declare -a source_certs=(
  863. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"
  864. "/var/lib/ca-certificates/ca-bundle.pem")
  865. target_cert=$(readlink -f "$mc_cert_link")
  866. [[ -f $target_cert ]] && return 0
  867. for f in "${source_certs[@]}"; do
  868. if [[ -f "$f" ]]; then
  869. if ! execute ln -fs "$f" "$mc_cert_link"; then
  870. err "Symlinking certificate failed"
  871. return 1
  872. fi
  873. break
  874. fi
  875. done
  876. }
  877. #######################################
  878. # Restore the mjr license file from RESTOREFILE or other common locations
  879. #######################################
  880. restoreLicense() {
  881. debug "Running: ${FUNCNAME[0]}"
  882. declare f newest
  883. # Glob mjr files from common directories
  884. shopt -s nullglob
  885. declare -a mjrfiles=(
  886. "$PWD"/*.mjr
  887. "$OUTPUTDIR"/*.mjr
  888. "$HOME"/[dD]ownloads/*.mjr
  889. "$HOME"/[dD]ocuments/*.mjr
  890. )
  891. shopt -u nullglob
  892. debug "mjrfiles=(${mjrfiles[*]})"
  893. # Sort globbed files by time, newest first
  894. newest=${mjrfiles[0]}
  895. for f in "${mjrfiles[@]}"; do
  896. if [[ -f $f && $f -nt $newest ]]; then
  897. newest=$f
  898. fi
  899. done
  900. debug "Latest mjrfile: $newest"
  901. for f in "$RESTOREFILE" "$newest"; do
  902. if [[ -f "$f" ]]; then
  903. if "mediacenter$MVERSION" "/RestoreFromFile" "$f"; then
  904. return 0
  905. fi
  906. fi
  907. done
  908. }
  909. #######################################
  910. # Opens ports using the system firewall tool
  911. # Arguments:
  912. # 1. Service name
  913. # 2. List of ports in firewall-cmd format
  914. #######################################
  915. openFirewall() {
  916. debug "Running: ${FUNCNAME[0]}" "$@"
  917. declare port
  918. declare service="$1"
  919. shift
  920. declare -a f_ports=("$@") # for firewall-cmd
  921. declare u_ports="$*"
  922. declare u_ports="${u_ports// /|}" # concatenate
  923. u_ports="${u_ports//-/\:}" # for ufw
  924. if hash firewall-cmd 2>/dev/null; then
  925. if ! sudo firewall-cmd --get-services | grep -q "$service"; then
  926. execute "sudo firewall-cmd --permanent --new-service=$service"
  927. execute "sudo firewall-cmd --permanent --service=$service --set-description=$service installed by installJRMC"
  928. execute "sudo firewall-cmd --permanent --service=$service --set-short=$service"
  929. for port in "${f_ports[@]}"; do
  930. execute "sudo firewall-cmd --permanent --service=$service --add-port=$port"
  931. done
  932. execute "sudo firewall-cmd --add-service $service --permanent"
  933. execute "sudo firewall-cmd --reload"
  934. fi
  935. elif hash ufw 2>/dev/null; then
  936. sudo bash -c "cat <<-EOF > /etc/ufw/applications.d/$service
  937. [$service]
  938. title=$service
  939. description=$service installed by installJRMC
  940. ports=$u_ports
  941. EOF"
  942. execute "sudo ufw app update $service"
  943. execute "sudo ufw allow $service"
  944. else
  945. err "Please install firewall-cmd or ufw to open firewall ports"
  946. return 1
  947. fi
  948. }
  949. #######################################
  950. # Create the xvnc or x11vnc password file
  951. # Arguments:
  952. # Service type (xvnc, x11vnc)
  953. #######################################
  954. setVNCPass() {
  955. debug "Running: ${FUNCNAME[0]}"
  956. declare vncpassfile="$HOME/.vnc/jrmc_passwd"
  957. [[ -d "${vncpassfile%/*}" ]] || execute "mkdir -p ${vncpassfile%/*}"
  958. if [[ -f "$vncpassfile" ]]; then
  959. if [[ ! -v VNCPASS ]]; then
  960. err "Refusing to overwrite existing $vncpassfile with an empty password"
  961. err "Remove existing $vncpassfile or use --vncpass ''"
  962. return 1
  963. else
  964. rm -f "$vncpassfile"
  965. fi
  966. fi
  967. if [[ -v VNCPASS ]]; then
  968. if [[ $1 == "xvnc" ]]; then
  969. execute "echo $VNCPASS | vncpasswd -f > $vncpassfile"
  970. elif [[ $1 == "x11vnc" ]]; then
  971. execute "x11vnc -storepasswd $VNCPASS $vncpassfile"
  972. fi
  973. return $?
  974. else
  975. declare -g NOVNCAUTH=1
  976. fi
  977. }
  978. #######################################
  979. # Set display and port variables
  980. #######################################
  981. setDisplay() {
  982. debug "Running: ${FUNCNAME[0]}"
  983. declare -g DISPLAY DISPLAYNUM NEXT_DISPLAY NEXT_DISPLAYNUM
  984. # Check USER_DISPLAY, else environment DISPLAY, else set to :0
  985. DISPLAY="${USER_DISPLAY:-${DISPLAY:-0}}"
  986. DISPLAYNUM="${DISPLAY#*:}" # strip prefix
  987. DISPLAYNUM="${DISPLAYNUM%%.*}" # strip suffix
  988. NEXT_DISPLAYNUM=$(( DISPLAYNUM + 1 ))
  989. NEXT_DISPLAY=":$NEXT_DISPLAYNUM"
  990. }
  991. #######################################
  992. # Create associated service variables based on service name
  993. # Arguments
  994. # Pre-defined service name
  995. #######################################
  996. setServiceVars() {
  997. debug "Running: ${FUNCNAME[0]}" "$*"
  998. declare -g SERVICE_NAME SERVICE_FNAME TIMER_NAME TIMER_FNAME
  999. declare -g USER_STRING DISPLAY_STRING GRAPHICAL_TARGET
  1000. declare -g RELOAD ENABLE DISABLE IS_ENABLED IS_ACTIVE
  1001. declare -a systemctl_prefix
  1002. declare service_name="$1"
  1003. declare service_type="${2:-${SERVICE_TYPE:-system}}"
  1004. declare service_dir="/usr/lib/systemd/$service_type"
  1005. if [[ "$USER" == "root" && "$service_type" == "user" ]]; then
  1006. err "Trying to install user service as root"
  1007. err "Use --service-type service and/or execute installJRMC as non-root user"
  1008. return 1
  1009. fi
  1010. if [[ "$service_type" == "system" ]]; then
  1011. systemctl_prefix=(sudo systemctl)
  1012. GRAPHICAL_TARGET="graphical.target"
  1013. elif [[ "$service_type" == "user" ]]; then
  1014. systemctl_prefix=(systemctl --user)
  1015. GRAPHICAL_TARGET="default.target"
  1016. fi
  1017. # systemctl commands
  1018. RELOAD="${systemctl_prefix[*]} daemon-reload"
  1019. ENABLE="${systemctl_prefix[*]} enable --now"
  1020. DISABLE="${systemctl_prefix[*]} disable --now"
  1021. IS_ENABLED="${systemctl_prefix[*]} is-enabled -q"
  1022. IS_ACTIVE="${systemctl_prefix[*]} is-active -q"
  1023. [[ -d "$service_dir" ]] || execute "sudo mkdir -p $service_dir"
  1024. # TODO Ubuntu needs these in the service file, fedora (and others?) do not
  1025. case "$ID" in
  1026. ubuntu|debian)
  1027. DISPLAY_STRING="Environment=DISPLAY=$DISPLAY"
  1028. DISPLAY_STRING+=$'\n'"Environment=XAUTHORITY=$XAUTHORITY"
  1029. ;;
  1030. *)
  1031. DISPLAY_STRING=""
  1032. ;;
  1033. esac
  1034. if [[ "$SERVICE_TYPE" == "system" && "$USER" != "root" ]]; then
  1035. SERVICE_FNAME="$service_dir/$service_name@.service"
  1036. TIMER_FNAME="$service_dir/$service_name@.timer"
  1037. SERVICE_NAME="$service_name@$USER.service"
  1038. TIMER_NAME="$service_name@$USER.timer"
  1039. USER_STRING="User=%I"
  1040. else
  1041. SERVICE_NAME="$service_name.service"
  1042. TIMER_NAME="$service_name.timer"
  1043. SERVICE_FNAME="$service_dir/$SERVICE_NAME"
  1044. TIMER_FNAME="$service_dir/${TIMER_NAME}"
  1045. USER_STRING=""
  1046. fi
  1047. }
  1048. #######################################
  1049. # Starts and enables (at startup) a JRiver Media Center service
  1050. # Arguments:
  1051. # Passes arguments as startup options to /usr/bin/mediacenter??
  1052. #######################################
  1053. service_jriver-mediacenter() {
  1054. debug "Running: ${FUNCNAME[0]}"
  1055. setServiceVars "${FUNCNAME[0]##*_}" "user"
  1056. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1057. [Unit]
  1058. Description=JRiver Media Center $MVERSION
  1059. After=$GRAPHICAL_TARGET
  1060. [Service]
  1061. $USER_STRING
  1062. $DISPLAY_STRING
  1063. Type=simple
  1064. Environment=DISPLAY=$DISPLAY
  1065. Environment=XAUTHORITY=$XAUTHORITY
  1066. ExecStart=/usr/bin/mediacenter$MVERSION $*
  1067. Restart=always
  1068. RestartSec=10
  1069. KillSignal=SIGHUP
  1070. TimeoutStopSec=30
  1071. [Install]
  1072. WantedBy=$GRAPHICAL_TARGET
  1073. EOF"
  1074. execute "$RELOAD" &&
  1075. execute "$ENABLE $SERVICE_NAME" &&
  1076. openFirewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1077. }
  1078. #######################################
  1079. # Starts and enables (at startup) a JRiver Media Server service
  1080. #######################################
  1081. service_jriver-mediaserver() {
  1082. debug "Running: ${FUNCNAME[0]}"
  1083. setServiceVars "${FUNCNAME[0]##*_}" "user"
  1084. service_jriver-mediacenter "/MediaServer"
  1085. }
  1086. #######################################
  1087. # Starts and enables (at startup) JRiver Media Center in a new Xvnc session
  1088. # TODO https://github.com/TigerVNC/tigervnc/blob/master/unix/vncserver/HOWTO.md
  1089. #######################################
  1090. service_jriver-xvnc() {
  1091. debug "Running: ${FUNCNAME[0]}"
  1092. setServiceVars "${FUNCNAME[0]##*_}" "system"
  1093. setDisplay
  1094. declare -a start_cmd
  1095. declare -g PORT=$(( NEXT_DISPLAYNUM + 5900 ))
  1096. installPackage tigervnc-server
  1097. setVNCPass xvnc
  1098. if [[ $# -eq 1 && $1 == "increment" ]]; then
  1099. NEXT_DISPLAYNUM=$(( NEXT_DISPLAYNUM + 1 ))
  1100. DISPLAY=":$NEXT_DISPLAYNUM"
  1101. PORT=$(( NEXT_DISPLAYNUM + 5900 ))
  1102. fi
  1103. start_cmd=(
  1104. /usr/bin/vncserver "$NEXT_DISPLAY"
  1105. -geometry 1440x900
  1106. -alwaysshared
  1107. -autokill
  1108. -xstartup "/usr/bin/mediacenter$MVERSION"
  1109. )
  1110. if (( NOVNCAUTH )); then
  1111. start_cmd+=(
  1112. -name "jriver$NEXT_DISPLAY"
  1113. -SecurityTypes None
  1114. )
  1115. else
  1116. start_cmd+=(
  1117. -rfbauth "$HOME/.vnc/jrmc_passwd"
  1118. )
  1119. fi
  1120. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1121. [Unit]
  1122. Description=Remote desktop service (VNC)
  1123. After=multi-user.target
  1124. [Service]
  1125. Type=forking
  1126. $USER_STRING
  1127. ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill $NEXT_DISPLAY &>/dev/null || :'
  1128. ExecStart=${start_cmd[*]}
  1129. ExecStop=/usr/bin/vncserver -kill $NEXT_DISPLAY
  1130. Restart=always
  1131. [Install]
  1132. WantedBy=multi-user.target
  1133. EOF"
  1134. execute "$RELOAD"
  1135. if ! execute "$ENABLE $SERVICE_NAME"; then
  1136. err "vncserver failed to start on DISPLAY $NEXT_DISPLAY"
  1137. err "Incrementing DISPLAY and retrying"
  1138. service_jriver-xvnc increment
  1139. return
  1140. fi
  1141. echo "Xvnc running on localhost:$PORT"
  1142. openFirewall "jriver-xvnc" "$PORT/tcp"
  1143. openFirewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1144. }
  1145. #######################################
  1146. # Starts and enables (at startup) x11vnc screen sharing for the local desktop
  1147. #######################################
  1148. service_jriver-x11vnc() {
  1149. debug "Running: ${FUNCNAME[0]}"
  1150. setServiceVars "${FUNCNAME[0]##*_}" "user"
  1151. setDisplay
  1152. declare -a start_cmd
  1153. declare -g PORT=$(( DISPLAYNUM + 5900 ))
  1154. installPackage x11vnc
  1155. setVNCPass x11vnc
  1156. # If .Xauthority file is missing, generate a dummy for x11vnc -auth guess
  1157. if [[ ! -f "$HOME/.Xauthority" ]]; then
  1158. [[ "$XDG_SESSION_TYPE" == "wayland" ]] &&
  1159. ask_ok "Unsupported Wayland session detected for x11vnc, continue?" || return 1
  1160. touch "$HOME/.Xauthority"
  1161. xauth generate "$DISPLAY" . trusted
  1162. xauth add "$HOST$DISPLAY" . "$(xxd -l 16 -p /dev/urandom)"
  1163. fi
  1164. start_cmd=(
  1165. /usr/bin/x11vnc
  1166. -display "$DISPLAY"
  1167. -noscr
  1168. -auth guess
  1169. -forever
  1170. -bg
  1171. )
  1172. if (( NOVNCAUTH )); then
  1173. start_cmd+=(-nopw)
  1174. else
  1175. start_cmd+=(-rfbauth "$HOME/.vnc/jrmc_passwd")
  1176. fi
  1177. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1178. [Unit]
  1179. Description=x11vnc
  1180. After=$GRAPHICAL_TARGET
  1181. [Service]
  1182. $USER_STRING
  1183. Type=forking
  1184. Environment=DISPLAY=$DISPLAY
  1185. ExecStart=${start_cmd[*]}
  1186. Restart=always
  1187. RestartSec=10
  1188. [Install]
  1189. WantedBy=$GRAPHICAL_TARGET
  1190. EOF"
  1191. execute "$RELOAD" &&
  1192. execute "$ENABLE" "$SERVICE_NAME" &&
  1193. echo "x11vnc running on localhost:$PORT" &&
  1194. openFirewall "jriver-x11vnc" "$PORT/tcp"
  1195. }
  1196. #######################################
  1197. # Starts and enables (at startup) an hourly service to build the latest version of
  1198. # JRiver Media Center RPM from the source DEB and create/update an RPM repository
  1199. #######################################
  1200. service_jriver-createrepo() {
  1201. debug "Running: ${FUNCNAME[0]}"
  1202. if [[ "$CREATEREPO_USER" != "$USER" ]]; then
  1203. USER="root" setServiceVars "${FUNCNAME[0]##*_}" "system"
  1204. else
  1205. setServiceVars "${FUNCNAME[0]##*_}" "system"
  1206. fi
  1207. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1208. [Unit]
  1209. Description=Builds JRiver Media Center RPM, moves it to the repo dir, and runs createrepo
  1210. [Service]
  1211. $USER_STRING
  1212. ExecStart=$PWD/installJRMC --outputdir $OUTPUTDIR --createrepo=$TARGET --createrepo-webroot $CREATEREPO_WEBROOT --createrepo-user $CREATEREPO_USER
  1213. [Install]
  1214. WantedBy=multi-user.target
  1215. EOF"
  1216. sudo bash -c "cat <<-EOF > $TIMER_FNAME
  1217. [Unit]
  1218. Description=Run JRiver MC rpmbuild hourly
  1219. [Timer]
  1220. OnCalendar=hourly
  1221. Persistent=true
  1222. [Install]
  1223. WantedBy=timers.target
  1224. EOF"
  1225. execute "$RELOAD" &&
  1226. execute "$ENABLE" "$TIMER_NAME"
  1227. }
  1228. #######################################
  1229. # CONTAINERS
  1230. #######################################
  1231. # container_jriver-createrepo() {
  1232. # :
  1233. # }
  1234. # container_jriver-xvnc() {
  1235. # :
  1236. # }
  1237. # container_jriver-mediacenter() {
  1238. # installPackage buildah podman
  1239. # if ! CNT=$(buildah from debian:$DEBIANBASE-slim); then
  1240. # echo "Bad base image for container, skipping"
  1241. # return 1
  1242. # fi
  1243. # brc() { buildah run "$CNT" bash -c "$*"; }
  1244. # brc "add-pkg gnupg2 libxss1 wmctrl xdotool ca-certificates inotify-tools libgbm1 ffmpeg"
  1245. # # Install JRiver
  1246. # brc "
  1247. # add-pkg ca-certificates gnupg &&
  1248. # add-pkg --virtual build-dependencies wget &&
  1249. # wget -qO- http://dist.jriver.com/mediacenter@jriver.com.gpg.key | tee /etc/apt/trusted.gpg.d/jriver.asc &&
  1250. # wget -O /etc/apt/sources.list.d/mediacenter${MVERSION}.list http://dist.jriver.com/latest/mediacenter/mediacenter${MVERSION}.list &&
  1251. # apt update &&
  1252. # add-pkg mediacenter${MVERSION} &&
  1253. # del-pkg build-dependencies
  1254. # "
  1255. # buildah config "$CNT" \
  1256. # --author "bryanroessler@gmail.com" \
  1257. # --label maintainer="$MAINTAINER" \
  1258. # --env TZ="$TZ" \
  1259. # --workingdir /app \
  1260. # --cmd "mediacenter$MVERSION"
  1261. # # EXPOSE 5800 5900 52100 52101 52199 1900/udp
  1262. # podman_create_cmd=(
  1263. # podman create
  1264. # --name "mediacenter$MVERSION"
  1265. # )
  1266. # podman_create_cmd+=(-v "$HOME/.jriver:/root/.jriver")
  1267. # podman_create_cmd+=(-v "$DOWNLOAD_ROOT:/downloads:z")
  1268. # podman_create_cmd+=(-v "$MONITOR_ROOT/nzbs:/nzbs")
  1269. # podman_create_cmd+=(-p "${CONTAINER[HOST_PORT]}:${CONTAINER[CONTAINER_PORT]}")
  1270. # # mkcdirs() {
  1271. # # declare dir
  1272. # # for dir in "$@"; do
  1273. # # if [[ ! -d "$dir" ]]; then
  1274. # # if ! mkdir -p "$dir"; then
  1275. # # err "Could not create directory $dir, check your permissions"
  1276. # # fi
  1277. # # fi
  1278. # # if ! chcon -t container_file_t -R "$dir"; then
  1279. # # err "Could not set container_file_t attribute for $dir, check your permissions"
  1280. # # fi
  1281. # # done
  1282. # # }
  1283. # # mkcdirs "$HOME/.jriver"
  1284. # brc sh -s <<-EOF
  1285. # wget -q "http://dist.jriver.com/mediacenter@jriver.com.gpg.key" -O- | apt-key add - &>/dev/null
  1286. # EOF
  1287. # brc wget "http://dist.jriver.com/latest/mediacenter/mediacenter$MVERSION.list" -O "/etc/apt/sources.list.d/mediacenter$MVERSION.list"
  1288. # brc apt update -y -q0
  1289. # brc add-pkg "mediacenter$MVERSION"
  1290. # brc del-pkg .build-deps
  1291. # }
  1292. #######################################
  1293. # Detects if MC is installed on btrfs and disables CoW
  1294. #######################################
  1295. disableCoW() {
  1296. debug "Running: ${FUNCNAME[0]}"
  1297. declare dir
  1298. declare mc_system_path="/usr/lib/jriver"
  1299. declare mc_user_path="$HOME/.jriver"
  1300. [[ -d "$mc_user_path" ]] || mkdir -p "$mc_user_path"
  1301. for dir in "$mc_system_path" "$mc_user_path"; do
  1302. [[ -d "$dir" ]] || return
  1303. if [[ $(stat -f -c %T "$dir") == "btrfs" ]] &&
  1304. ! lsattr -d "$dir" | cut -f1 -d" " | grep -q C; then
  1305. echo "Disabling Btrfs CoW for $dir"
  1306. execute "sudo chattr +C $dir"
  1307. fi
  1308. done
  1309. }
  1310. #######################################
  1311. # Migrate major versions
  1312. #######################################
  1313. migrateLibrary() {
  1314. debug "Running: ${FUNCNAME[0]}"
  1315. declare mc_user_path="$HOME/.jriver"
  1316. declare current_config_path="$mc_user_path/Media Center $MVERSION"
  1317. declare previous_config_path="$mc_user_path/Media Center $(( MVERSION - 1 ))"
  1318. if [[ ! -d "$current_config_path" ]] &&
  1319. [[ -d "$previous_config_path" ]] &&
  1320. mkdir -p "$current_config_path"; then
  1321. echo "Migrating $previous_config_path to $current_config_path"
  1322. execute cp -fa "$previous_config_path"/* "$current_config_path"
  1323. fi
  1324. }
  1325. #######################################
  1326. # Completely uninstalls MC, services, and firewall rules
  1327. #######################################
  1328. uninstall() {
  1329. debug "Running: ${FUNCNAME[0]}"
  1330. declare service unit f i
  1331. echo "Stopping and removing all Media Center services"
  1332. for service in $(compgen -A "function" "service"); do
  1333. service="${service##service_}"
  1334. for i in user system; do
  1335. setServiceVars "$service" "$i";
  1336. for unit in "$SERVICE_NAME" "$TIMER_NAME"; do
  1337. if execute "$IS_ACTIVE $unit" ||
  1338. execute "$IS_ENABLED $unit"; then
  1339. execute "$DISABLE $unit"
  1340. fi
  1341. done
  1342. for f in "$SERVICE_FNAME" "$TIMER_FNAME"; do
  1343. [[ -f "$f" ]] &&
  1344. execute "sudo rm -f $f"
  1345. done
  1346. execute "$RELOAD"
  1347. done
  1348. for f in /etc/systemd/system/jriver-*; do
  1349. execute "sudo rm -f $f"
  1350. done
  1351. done
  1352. echo "Removing repo files"
  1353. sudo rm -rf \
  1354. "/etc/yum.repos.d/jriver.repo" \
  1355. /etc/apt/sources.list.d/{jriver,mediacenter}*.list # also remove legacy repo files
  1356. if [[ "$ID" == "suse" ]]; then
  1357. execute "sudo zypper rr jriver"
  1358. fi
  1359. echo "Removing firewall rules"
  1360. if hash firewall-cmd 2>/dev/null; then
  1361. execute "sudo firewall-cmd --permanent --remove-service=jriver"
  1362. execute "sudo firewall-cmd --permanent --delete-service=jriver"
  1363. execute "sudo firewall-cmd --reload"
  1364. elif hash ufw 2>/dev/null; then
  1365. execute "sudo ufw delete allow jriver"
  1366. [[ -f "/etc/ufw/applications.d/jriver" ]] &&
  1367. execute "sudo rm -f /etc/ufw/applications.d/jriver"
  1368. fi
  1369. echo "Uninstalling JRiver Media Center package"
  1370. if execute "${PKG_REMOVE[@]}" "$MCPKG"; then
  1371. echo "JRiver Media Center has been completely uninstalled"
  1372. echo "To remove your library files, run: rm -rf $HOME/.jriver"
  1373. elif [[ $? -eq 100 ]]; then
  1374. err "JRiver Media Center package '$MCPKG' is not present and was not uninstalled"
  1375. else
  1376. err "Could not remove Media Center package"
  1377. fi
  1378. }
  1379. tests() {
  1380. # To test on Mint/16.04: sudo apt install -y spice-vdagent ca-certificates git; export GIT_SSL_NO_VERIFY=1
  1381. : # TODO
  1382. }
  1383. main() {
  1384. debug "Running: ${FUNCNAME[0]} $*"
  1385. init
  1386. parseInput "$@"
  1387. debug "Debugging on"
  1388. debug "installJRMC version: $SCRIPTVERSION"
  1389. if ((TEST_SWITCH)); then
  1390. echo "Running tests, all other options are skipped"
  1391. tests
  1392. exit
  1393. fi
  1394. setMCVersion
  1395. if (( UNINSTALL_SWITCH )); then
  1396. if askOk "Do you really want to uninstall JRiver Media Center?"; then
  1397. uninstall
  1398. else
  1399. echo "Uninstall canceled"
  1400. fi
  1401. exit
  1402. fi
  1403. # Install external repos
  1404. case "$ID" in
  1405. ubuntu)
  1406. if ! grep ^deb /etc/apt/sources.list|grep -q universe; then
  1407. echo "Adding universe repository"
  1408. if ! execute "sudo add-apt-repository -y universe"; then
  1409. err "Adding universe repository failed"
  1410. fi
  1411. fi
  1412. ;;
  1413. centos)
  1414. if ! hash dpkg &>/dev/null; then
  1415. echo "Adding EPEL repository"
  1416. installPackage epel-release
  1417. fi
  1418. if ! PKG_QUERY rpmfusion-free-release &>/dev/null; then
  1419. installPackage --no-install-check \
  1420. "https://download1.rpmfusion.org/free/el/rpmfusion-free-release-$VERSION_ID.noarch.rpm"
  1421. fi
  1422. ;;
  1423. fedora)
  1424. if ! PKG_QUERY rpmfusion-free-release &>/dev/null; then
  1425. installPackage --no-install-check \
  1426. "https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$VERSION_ID.noarch.rpm"
  1427. fi
  1428. ;;
  1429. esac
  1430. if (( REPO_INSTALL_SWITCH )); then
  1431. echo "Installing JRiver Media Center from remote repository"
  1432. if installMCFromRepo; then
  1433. echo "JRiver Media Center installed successfully from repo"
  1434. symlinkCerts
  1435. migrateLibrary
  1436. restoreLicense
  1437. openFirewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1438. disableCoW
  1439. else
  1440. err "JRiver Media Center installation from repo failed"
  1441. return 1
  1442. fi
  1443. fi
  1444. if (( BUILD_SWITCH )) && [[ $ID != "arch" ]]; then
  1445. installPackage "wget"
  1446. acquireDeb
  1447. if [[ "$TARGET" =~ (centos|fedora|suse) ]]; then
  1448. installPackage "dpkg" "rpm-build"
  1449. buildRPM
  1450. fi
  1451. fi
  1452. if (( LOCAL_INSTALL_SWITCH )); then
  1453. if PKG_INSTALL_LOCAL; then
  1454. echo "JRiver Media Center installed successfully from local package"
  1455. else
  1456. err "JRiver Media Center local package installation failed"
  1457. return 1
  1458. fi
  1459. symlinkCerts
  1460. migrateLibrary
  1461. restoreLicense
  1462. openFirewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1463. disableCoW
  1464. fi
  1465. if (( CREATEREPO_SWITCH )); then
  1466. if runCreaterepo; then
  1467. echo "Successfully updated repo"
  1468. else
  1469. err "Repo creation failed"
  1470. fi
  1471. fi
  1472. if [[ "${#SERVICES[@]}" -gt 0 ]]; then
  1473. declare service
  1474. for service in "${SERVICES[@]}"; do
  1475. if ! "service_$service"; then
  1476. if [[ $? -eq 127 ]]; then
  1477. err "Service $service does not exist, check service name"
  1478. else
  1479. err "Failed to create service: $service"
  1480. fi
  1481. fi
  1482. done
  1483. fi
  1484. # for _container in "${CONTAINERS[@]}"; do
  1485. # if ! "_container_$_container"; then
  1486. # if [[ $? -eq 127 ]]; then
  1487. # err "Container $_container does not exist, check container name"
  1488. # else
  1489. # err "Failed to create container: $_container"
  1490. # fi
  1491. # fi
  1492. # done
  1493. }
  1494. # Roughly turn debugging on, reparse in getInput() with getopt
  1495. [[ " $* " =~ ( --debug | -d ) ]] && declare -g DEBUG=1
  1496. main "$@"