installJRMC 54 KB

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