installJRMC 59 KB

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