installJRMC 59 KB

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