installJRMC 51 KB

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