installJRMC 52 KB

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