installJRMC 59 KB

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