installJRMC 54 KB

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