installJRMC 54 KB

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